简体   繁体   中英

Android sqlite database - how to show listview adding “Load more items” at the end of scroll

I have a Listview in Android Java app which is fetching data from Sqlite database. I want to show only 10 items in MainActivity and add "Load more items" to display 10 items at the end of each scroll up or scroll down. How to do it?

This is the code to get all data in DatabaseHelper.java:

public List<String> getAllData () {
    SQLiteDatabase db = this.getReadableDatabase();

    List<String> arrayList = new ArrayList<String>();

    Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " order by ID desc", null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {

                arrayList.add(
                        cursor.getString(cursor.getColumnIndex("ID"))+ ") " +
                        cursor.getString(cursor.getColumnIndex("TIME"))+ " | " +
                        cursor.getString(cursor.getColumnIndex("PHONE"))+ "\n" +
                        cursor.getString(cursor.getColumnIndex("SMS"))
                );

        cursor.moveToNext();
    }
    return arrayList;
}

This is my MainActivity.java:

public class MainActivity extends AppCompatActivity {
DatabaseHelper myDB;
ArrayAdapter arrayAdapter;
ListView txtLogs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myDB = new DatabaseHelper(MainActivity.this);
    arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,myDB.getAllData());

    txtLogs = findViewById(R.id.txtLogs);
    txtLogs.setAdapter(arrayAdapter);

}

}

Hi you can follow some tutorial for this. Here is the tutorial link which can help you to achieve that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM