Reputation: 142
I am taking data from a activity, saving it to sqlite db and showing it in listview. But I want it to show the list in reverse order . This is my list view viewing activity. I am not able to understand what should I add for reverse order listview
Edit: where should I add Collection.reverse(arraylist);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_sqlite_data);
LISTVIEW = (ListView) findViewById(R.id.listView1);
ID_Array = new ArrayList<String>();
NAME_Array = new ArrayList<String>();
PHONE_NUMBER_Array = new ArrayList<String>();
sqLiteHelper = new SQLiteHelper(this);
LISTVIEW.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),ShowSingleRecordActivity.class);
intent.putExtra("ListViewClickedItemValue", ListViewClickItemArray.get(position).toString());
startActivity(intent);
}
});
}
@Override
protected void onResume() {
ShowSQLiteDBdata() ;
super.onResume();
}
private void ShowSQLiteDBdata() {
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
cursor = sqLiteDatabase.rawQuery("SELECT * FROM "+SQLiteHelper.TABLE_NAME+"", null);
ID_Array.clear();
NAME_Array.clear();
PHONE_NUMBER_Array.clear();
if (cursor.moveToFirst()) {
do {
ID_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table_Column_ID)));
//Inserting Column ID into Array to Use at ListView Click Listener Method.
ListViewClickItemArray.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table_Column_ID)));
NAME_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table_Column_1_Name)));
PHONE_NUMBER_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table_Column_2_PhoneNumber)));
} while (cursor.moveToNext());
}
listAdapter = new ListAdapter(DisplaySQLiteDataActivity.this,
ID_Array,
NAME_Array,
PHONE_NUMBER_Array
);
LISTVIEW.setAdapter(listAdapter);
cursor.close();
}
}
Upvotes: 1
Views: 1678
Reputation: 6363
Just Modify your code as below:
Collections.reverse(ID_Array);
Collections.reverse(NAME_Array);
Collections.reverse(PHONE_NUMBER_Array);
listAdapter = new ListAdapter(DisplaySQLiteDataActivity.this,
ID_Array,
NAME_Array,
PHONE_NUMBER_Array
);
LISTVIEW.setAdapter(listAdapter);
This is what you need, this way your lists will be reversed even before initializing the adapter and your adapter will then have the reversed values which basically means a reversed adapter.
Upvotes: 3