lundi 7 mars 2016

Change the background of a ListView programatically

What I am currently trying to do is, I'm trying to add each row of the ListView a specific background, I'm loading all the information from SQLite. I can load the picture next to the text, but I cannot put the picture I use in the specific row as a Background

 ForthActivity.java




public class ForthActivty extends AppCompatActivity implements
     View.OnClickListener   {

     private EditText  pastetext;
     private ClipboardManager myClipboard;


     private ProgressDialog loading;
     protected ListView lv;
     protected ListAdapter adapter;
     SQLiteDatabase db;
     Cursor cursor;
     EditText et_db;

     @SuppressWarnings("deprecation")
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         setContentView(R.layout.activity_forth_activty);
         myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
         pastetext = (EditText) findViewById(R.id.textView1);


         db = (new DB_Resep(this)).getWritableDatabase();
         lv = (ListView) findViewById(R.id.lv);
         et_db = (EditText) findViewById(R.id.et);

         try {
             cursor = db.rawQuery("SELECT * FROM resep ORDER BY nama ASC", null);
             adapter = new SimpleCursorAdapter(this, R.layout.isi_lv, cursor,
                     new String[] { "nama", "bahan", "img" }, new int[] {
                     R.id.tv_nama, R.id.tvBahan, R.id.imV });
             lv.setAdapter(adapter);
             lv.setTextFilterEnabled(true);
             lv.setOnItemClickListener(new OnItemClickListener() {

                 @Override
                 public void onItemClick(AdapterView<?> parent, View v,
                                         int position, long id) {
                     detail(position);

                 }
             });
         } catch (Exception e) {
             e.printStackTrace();
         }

     }

     @SuppressWarnings("deprecation")
     public void search_db(View v) {
         String edit_db = et_db.getText().toString();
         if (!edit_db.equals("")) {
             try {
                 cursor = db.rawQuery("SELECT * FROM resep WHERE nama LIKE ?",
                         new String[] { "%" + edit_db + "%" });
                 adapter = new SimpleCursorAdapter(
                         this,
                         R.layout.isi_lv,
                         cursor,
                         new String[] { "nama", "bahan", "img" },
                         new int[] { R.id.tv_nama, R.id.tvBahan, R.id.imV });
                 if (adapter.getCount() == 0) {
                     Toast.makeText(
                             this,
                             "Nu am putut gasi reteta dumneavoastra " + edit_db
                                     + "", Toast.LENGTH_SHORT).show();
                 } else {
                     lv.setAdapter(adapter);
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             }
         } else {
             try {
                 cursor = db.rawQuery("SELECT * FROM resep ORDER BY nama ASC",
                         null);
                 adapter = new SimpleCursorAdapter(
                         this,
                         R.layout.isi_lv,
                         cursor,
                         new String[] { "nama", "bahan", "img" },
                         new int[] { R.id.tv_nama, R.id.tvBahan, R.id.imV });
                 lv.setAdapter(adapter);
                 lv.setTextFilterEnabled(true);
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
     }

     @SuppressLint("NewApi")
     public void paste(View view) {
         ClipData cp = myClipboard.getPrimaryClip();
         ClipData.Item item = cp.getItemAt(0);
         String text = item.getText().toString();
         pastetext.setText(text);
         Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);}




     public void detail(int position) {
         int im = 0;
         String _id = "";
         String nama = "";
         String bahan = "";
         String cara = "";
         if (cursor.moveToFirst()) {
             cursor.moveToPosition(position);
             im = cursor.getInt(cursor.getColumnIndex("img"));
             nama = cursor.getString(cursor.getColumnIndex("nama"));
             bahan = cursor.getString(cursor.getColumnIndex("bahan"));
             cara = cursor.getString(cursor.getColumnIndex("cara"));
         }

         Intent iIntent = new Intent(this, DB_Parse.class);
         iIntent.putExtra("dataIM", im);
         lv.setBackground(R.drawable.+ "im");
         iIntent.putExtra("dataNama", nama);
         iIntent.putExtra("dataBahan", bahan);
         iIntent.putExtra("dataCara", cara);
         setResult(RESULT_OK, iIntent);
         startActivityForResult(iIntent, 99);
     }


     @Override
     public void onClick(View v) {

     } }

DB_Resep.java

 ContentValues values = new ContentValues();
         values.put("_id", "1");
         values.put("nama", "Recipe Name");
         values.put("bahan", "Random Recipe");
         values.put("img", R.drawable.test);
         db.insert("resep", "_id", values);

Aucun commentaire:

Enregistrer un commentaire