0

I have a programming hurdle that I would appreciate your assistance. I used a tutorial to create my database and I am having an issue as I do not properly understand the Cursor function and its application. I have a database that stores contacts and contact conversations in two different tables. In my activity, I have ordered the contacts in a DESC manner. When I click on a contact, it should open the conversations for the contact (ordered in an asc manner) but instead it opens for a different contact. After further investigations, I discovered that as the contacts are displayed descending, the contact_id are mapped ascending such that the last contact is assigned the contact_id of the first contact and thus when the last contact is clicked, it will display the conversations of the first contact. My issue is getting the contact_id to be ordered descending for proper mapping. My code is as below. Thanks in advance.

public Cursor getData(int id){ 
SQLiteDatabase db = this.getReadableDatabase();
   Cursor res = db.rawQuery( "select * from contacts where id="+id+"", null );
   return res; 
} 

public Cursor getConvData(int id){ 
SQLiteDatabase db = this.getReadableDatabase();
   Cursor res = db.rawQuery( "select * from conversation where id="+id+"", null );
   return res; 
} 

 public ArrayList<String> getAllCotacts() { 
  ArrayList<String> array_list = new ArrayList<String>();
  //hp = new HashMap(); 
 SQLiteDatabase db = this.getReadableDatabase();
 Cursor res = db.rawQuery( "select * from contacts order by id desc", null );
 res.moveToFirst();
 while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME))); 
 res.moveToNext(); 
} return array_list;
}

 public ArrayList<String> getAllMsgs(Integer id) { //get all messages for a particular contact
  ArrayList<String> array_list = new ArrayList<String>();
  //hp = new HashMap(); 
 SQLiteDatabase db = this.getReadableDatabase();
 Cursor res = db.rawQuery( "select * from conversation where contact_id= " + id + " order by id asc", null );
 res.moveToFirst();
 while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(CONVERSATION_COLUMN_MESSAGE))); 
 res.moveToNext(); 
} return array_list;
}
Biko
  • 332
  • 3
  • 15
  • 1
    dont use `ArrayAdapter`, your data model is sqlite db so use `SimpleCursorAdapter` – pskink Aug 17 '15 at 06:36
  • Will `SimpleCursorAdapter` ensure that both contact number and id are fetched from the database descending as opposed to having the contacts descending and their corresponding id values ascending – Biko Aug 17 '15 at 08:22
  • 1
    did you try `SimpleCursorAdapter`? – pskink Aug 17 '15 at 08:27
  • I solved the problem in the activity. Thanks for your ideas. – Biko Aug 18 '15 at 23:25

0 Answers0