Question

I am getting trouble on itemclick listener in android using Sqlite ,i am getting the data from sqlite data base and i need to pass the same arraylist to next activity using onitemclick listener.,But its not working .

 contactList.clear();


          String query = "SELECT  * FROM MessageTable  WHERE userID = '" + userid +"'"; 

          System.out.println("queryinsert="+query);

          Cursor c1 = sqlHandler.selectQuery(query);
          if (c1 != null && c1.getCount() != 0) {
           if (c1.moveToFirst()) {
           do {
             ContactListItems contactListItems = new ContactListItems();

             contactListItems.setSlno(c1.getString(c1.getColumnIndex("ID")));
                         contactListItems.setmessage(c1.getString(c1.getColumnIndex("message")));

             contactList.add(contactListItems);

            } while (c1.moveToNext());
           }
          }
          c1.close();
         }

here is the arraylist ArrayList<ContactListItems> contactListwhere i am storing the sqlite data , i need to pass the data to next on eachitem click ,could anybody guide me .@Thanks

Was it helpful?

Solution

try using this one. You can pass ArrayList of class using Serializable interface. check below code.

1) you have to implement Serializable interface in your ContactListItems class like 

public class ContactListItems implements Serializable {

}

2) Pass arraylist of ContactListItems via intent:

Intent intent = new Intent(YourCurrentActivity.this, DestinationActivity.class);
intent.putExtra("ContactListData", arrayListData); //  here arrayListData is the object of ArrayList<ContactListItems>
startActivity(intent);

3) get Intent of ArrayList containing ContactListItems class:

ArrayList<ContactListItems> contactListData;

Bundle extras = getIntent().getExtras();
   if (extras != null) {
    contactListData = (ArrayList<ContactListItems>) extras
                    .getSerializable("ContactListData");

   }

Hope it will help you.

OTHER TIPS

overide onitem click listener for ListView http://www.mkyong.com/android/android-listview-example/

you can use bundle and also you can use another class which have ArrayList contactList variable and in listview click retrive id of row and pass that id using bundle and there you go

If you want to fill your entity class with your Cursor you should use a for statement,now your are getting just the first row in your Cursor and reinstantiate your class

for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) 
{
}

and for your list listener use :

YourList.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
    }});

Edit : this is the code how you put an arraylist to the intent

intent.putStringArrayListExtra("Tets",YourArrayList);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top