-1

UPDATE: PROBLEM FIXED - The ActionBar was covering the first item on the list.

SOLUTION: Android AppBarLayout overlaps listview

In my program, I am retrieving data from the database and displaying it using List View. However, the first row elements are always skipped in the process and the display begins from the second row.

public void displaydata(){
    Cursor res = myDb.getAllData();
    lv = (ListView) findViewById(R.id.idListView);
    if(res.getCount() == 0){
        //show message
        return;
    }
    ArrayList<String> buffer = new ArrayList<>();
    while(res.moveToNext()){
        buffer.add(res.getString(1));
    };
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,buffer);
    lv.setAdapter(adapter);
}

How do I make it display from the first row? Any help is appreciated, thanks.

EDIT: I have tried all suggested answers of using a 'do-while' and a 'for loop', all of which give the same result.

Community
  • 1
  • 1
  • I believe its because `res.moveToNext()` goes to the next row so index 0 gets skipped – Derek Apr 10 '17 at 20:17
  • Related: http://stackoverflow.com/questions/10723770/whats-the-best-way-to-iterate-an-android-cursor – JBC Apr 10 '17 at 20:21
  • Possible duplicate of [What's the best way to iterate an Android Cursor?](http://stackoverflow.com/questions/10723770/whats-the-best-way-to-iterate-an-android-cursor) – OneCricketeer Apr 10 '17 at 20:26
  • You really should be using a CursorAdapter instead of trying to load everything into an ArrayList / ArrayAdapter – OneCricketeer Apr 10 '17 at 20:26
  • Possible duplicate of [Android AppBarLayout overlaps listview](https://stackoverflow.com/questions/32653767/android-appbarlayout-overlaps-listview) – Bertram Gilfoyle Jul 03 '18 at 08:08

3 Answers3

1

Try changing

while(res.moveToNext()){
        buffer.add(res.getString(1));
    };

to

Edit: change the while so it increments after:

do {
    buffer.add(res.getString(1));
} while (cursor.moveToNext());
Derek
  • 2,927
  • 3
  • 20
  • 33
0

Personally, I would recommend a CursorAdapter when using a database.

lv = (ListView) findViewById(R.id.idListView);

String from = { COLUMN_NAME };
int[] to = { android.R.id.text1 };

SimpleCursorAdapter adapter = 
    new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, 
          myDb.getAllData(), 
          from, to);
lv.setAdapter(adapter);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Try out this this code may be useful for fetching data from db using cursor.

  public ArrayList<BasicInfo> getFetchBasicInfo() {

    ArrayList<BasicInfo> data = new ArrayList<BasicInfo>();

    String sql = "select * from basic_info;

    Cursor c = fetchData(sql);

    if (c != null) {
        while (c.moveToNext()) {

            String FirstName = c.getString(c.getColumnIndex("first_name"));

            String LastName = c.getString(c.getColumnIndex("last_name"));
            String Sabcription = c.getString(c
                    .getColumnIndex("salutation_id"));
            data.add(new BasicInfo(FirstName, LastName));
        }
        c.close();
    }
    return data;
}
ashish
  • 848
  • 6
  • 16