0

This is a follow on from an earlier question: ImageButton within row of ListView android not working But after suggestions from SO gurus it has been suggested I post a new question. The issue is that I have a custom adapter that is not showing any data. I have looked into other questions, but it didn't provide a solution.

In my Main Activity I have a couple of buttons, one of them: ToDo, should create a row that displays data from a SQLite database, and depending on some factors (dates mainly), it shows a type of traffic light that is stored as a drawable.

Part of the Items in this Row is an Image Button that I want the user to be able to click and the image should change. The user should be able also to click on the actual row and a new activity starts.

The issue I have is that NO DATA is being displayed.

So, here is my code:

  public class MainActivity extends Activity {
  // definitions etc ...
  @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       // definitions etc ...
    }

    public void ToDo(View v){  // the user has clicked in the ToDo button
    IgroDatabaseHelper helper = new IgroDatabaseHelper(getBaseContext()); // create instance of SQLIte database
    numRows = helper.NumEntries("ToDo"); // Get the number of rows in table
    int i = 1;
    ArrayList<RowItem> rowItems = new ArrayList<>();
    RowItem myItem1;
    while (i <= numRows){
       // get items from database
       // depending on value select different drawable
       // put data into List Array of RowItem
       myItem1 = new RowItem(TheWhat, R.drawable.teamworka, R.drawable.redtrafficlight, R.drawable.checkbox, TheWhenBy);
                    rowItems.add(myItem1);
       //
       i = i+ 1;
    }

   ListView yourListView = (ListView) findViewById(R.id.list);
   CustomListViewAdapter customAdapter = new CustomListViewAdapter(this, R.layout.todo_row, rowItems);
   yourListView.setAdapter(customAdapter);
 }

The CustomListViewAdapter looks like this:

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

Context context;
ArrayList<RowItem> _rowItems;

public CustomListViewAdapter(Context context, int resourceId,
        ArrayList<RowItem> rowItems) {

    super(context, resourceId);
    this.context = context;
    _rowItems = rowItems;
    System.out.println("I am in the custom Adapter class "+ _rowItems);
}


@Override
public View getView(int position, View convertView, ViewGroup parent){
    System.out.println("This is the get view");
    View row = convertView;
    RowItem item = _rowItems.get(position);

    // you can now get your string and drawable from the item
    // which you can use however you want in your list
    String columnName = item.getColumnName();
    int drawable = item.getDrawable();
    if (row == null) {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       row = mInflater.inflate(R.layout.todo_row, parent, false);

    }

    ImageButton chkDone = (ImageButton) row.findViewById(R.id.chkDone);
    chkDone.setOnClickListener(new View.OnClickListener() {              
          @Override
          public void onClick(View v) {
                View parentRow = (View) v.getParent();
                ListView listView = (ListView) parentRow.getParent();
                final int position =   listView.getPositionForView(parentRow);
                System.out.println("I am in position "+ position);
          }
     });

    return row;
}
}

The RowItem Class looks like:

public class RowItem {
   private String _heading;
    private int _icon;
    private int _lights;
    private int _chkdone;
    private String _date;


    public RowItem(String heading, int icon, int lights, int chkDone, String date) {
         _heading = heading;
         _icon = icon;
         _lights = lights;
         _chkdone = chkDone;
         _date = date;

        System.out.println("adding stuff to my rows");
        System.out.println("my column Name is " + heading);
        System.out.println("My drawable int is "+ icon);

    }

    public String getColumnName() {
        System.out.println("column Names is "+ _heading);
        return _heading;
    }

    public int getDrawable() {
        return _icon;
    }

    public int getLights(){
        return _lights;
    }

    public int getchkDone(){
        return _chkdone;
    }

    public String getDate(){
        return _date;
    }
}

I am obviously missing something, as I mentioned earlier, no data gets shown. I know that there are 2 row items that get passed to the CustomListViewAdapter. But I also know that the View getView inside the CustomListViewAdapter does not actually get called.

I hope I have put enough information/code, but if you feel I need to explain something further, please say.

Thanking all very much in advance!

Community
  • 1
  • 1
user3079872
  • 191
  • 1
  • 5
  • 15
  • 1
    Whats your getCount() do? – r2DoesInc Apr 23 '15 at 16:25
  • I don't think I have a getCount() ... – user3079872 Apr 23 '15 at 16:27
  • 1
    try adding it to your custom adapter then then: `@Override public int getCount() { return _rowItems.size(); } @Override public Object getItem(int i) { return _rowItems.get(i); } @Override public long getItemId(int i) { return i; }` not sure if the rest is needed, but shouldn't hurt to add them as well :) – Klotor Apr 23 '15 at 16:30
  • @Klotor, I just added your code, and I get an error on the public Object getItem method: 'The return type is incompatible with ArrayAdapter.getItem(int)' – user3079872 Apr 23 '15 at 16:34
  • Wow!! I actually get something now ... It is not showing the text or images. When I try to click on a row it crashes. Within the same Main Activity I have another ListView that shows different items. It thinks I am selecting from this list ... Do I need to clear the ListView once it is not needed? – user3079872 Apr 23 '15 at 16:48

2 Answers2

1

I don't see a getCount() method. You should be overriding it like this:

@Override
    public int getCount() {
        return _rowItems.getCount();
    }

Alternatively, calling super(context, resourceId, rowItems); should also fix it.

Zamereon
  • 376
  • 1
  • 2
  • 17
  • your last comment about calling 'super (context, resourceId, rowItems);' means it does not crash. I think I will need to post yet another question, because what is happening now is that when I click on a row it thinks it is a row from an array built when the class gets called. So the Main Activity has a number of ListArrays being created, one of them is created when Main Activity gets created. – user3079872 Apr 23 '15 at 17:20
0

Your ListView thinks there are no items to display. If you are using your own array, you must override the getCount() method to indicate the number of items you want to display.

Tamas
  • 221
  • 1
  • 8