Hello! I populate the listView from the SQLite database as follows:

SQLiteDatabase database = dbHelper.getReadableDatabase(); Cursor cursor = database.query(DBHelper.TABLE_ONE, null, null, null, null, null, null); String[] from = new String[]{DBHelper.COLUMN_ONE, DBHelper.COLUMN_TWO}; int[] to = new int[]{R.id.textView1, R.id.textView2}; SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to, 0); listView.setAdapter(cursorAdapter); 

Got a question. Suppose I have a column COLUMN_ONE, in which there may be some text, or "Absent". So, how to make sure that if in COLUMN_ONE is "Missing", then in R.id.textView1 nothing is displayed (""), and not "Missing"?

Question number two. COLUMN_TWO type integer. On R.layout.list_item, besides textView1 there is an imageView. How to digitally display a picture in the list item? For example, if the digit is 0, then the picture will be one, if 1, then the other, etc.

  • one
    Store in the database an empty string instead of "absent" or in the bindView() adapter, check for the contents of the string and if = "absent" in TextView, find an empty string. For the second, store not abstract numbers, but the ID of the image you want to display, then by this ID, put a picture in the widget. You will not have the condition, because there are left numbers in the database, and the comparison is with the ID of the picture - pavlofff
  • If you independently found a solution to a problem in a question, you need to issue it with an answer, and not write it in a question - pavlofff
  • @pavlofff, well, how independently, you wrote about bindView, I googled, found an example and changed it to fit my needs. I marked the comment as adding useful information, and when I tried to answer my question I received "Leave a comment if you want to comment on the answer. Edit your question if you want to add additional information. " all right - badadin
  • Click "answer your question" and post the answer there. The question should not be a solution. - pavlofff

2 answers 2

Made through bindView

 public class MySimpleCursorAdapter extends SimpleCursorAdapter { public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) { super(context, layout, c, from, to, flags); } @Override public void bindView(View view, Context context, Cursor cursor) { TextView text = (TextView) view.findViewById(R.id.textView); ImageView image = (ImageView) view.findViewById(R.id.imageView); String one = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ONE)); int two = cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_TWO)); if (dateStart.equals(context.getResources().getString(R.string.NotSet))) text.setText(""); else text.setText(one); if (two == 1) image.setImageResource(R.drawable.ico1); else image.setImageResource(R.drawable.ico2); } } 

    You need to create your own adapter class, inherited from SimpleCursorAdapter and write your implementation of displaying information in the View components in it. Like this:

     public class MySimpleCursorAdapter extends SimpleCursorAdapter { public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) { super(context, layout, c, from, to, flags); } @Override public void setViewImage(ImageView v, String value) { if (Integer.parseInt(value) == 0) v.setImageResource(R.drawable.image1); if (Integer.parseInt(value) == 1) v.setImageResource(R.drawable.image2); } @Override public void setViewText(TextView v, String text) { if (text.equals("ะžั‚ััƒั‚ัั‚ะฒัƒะตั‚") v.setText(""); } } 
    • And what about the fact that my list_item is a separate xml, and imageView = (ImageView) findViewById(R.id.imageView); takes place in the MainActivity class. Accordingly, if I call cursorAdapter.setViewImage(imageView, "0"); , then it does not find such an id and throw a NullPointerException? - badadin
    • @badadin you do not need to call the setViewImage method setViewImage . It is necessary just in this line int[] to = new int[]{R.id.textView1, R.id.textView2}; , change R.id.textView2 , to R.id.imageView1 , which should be in the list_item markup. That is, the setViewImage method setViewImage called when the ImageView component is located by ID, and the value will be passed a value from the database in the COLUMN_TWO column specified in the from array. - Frozik6k
    • @pavlofff I'm still interested, and the worse setViewImage and setViewText than bindView ? - Frozik6k
    • @MaksimPonomarev There is an established practice that filling an item with content occurs in the bindView() method. In general, the methods you specify are invoked by bindView() itself bindView() when implementing a default binding. Implementing our logic by redefining bindView() we have more freedom to implement our plans. I here, before your answer, have never met such a way to bind in an item and why this method is not popular for me to look for laziness, but probably there are reasons. - pavlofff
    • I think these methods work only with the default markup of the item, so they are not popular, but Iโ€™m too lazy to understand. - pavlofff