Vanisha Gupta Vanisha Gupta - 4 months ago 17
Android Question

Android : Listview data is not being passed in another activity

I have made a listview and inflated it from database. And i have set up an

onclicklistener
on listview item using:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_books);
listView = (ListView) findViewById(R.id.listView);
call=(Button)findViewById(R.id.call);
userDbHelper = new UserDbHelper(getApplicationContext());
sqLiteDatabase = userDbHelper.getReadableDatabase();
cursor = userDbHelper.getInformation(sqLiteDatabase);
listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
final TextView txt=(TextView) findViewById(R.id.pr);
listView.setAdapter(listDataAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent in=new Intent(Books.this,List1.class);
TextView txt=(TextView)findViewById(R.id.pr);
String getPrice=txt.getText().toString();
in.putExtra("getPrice",getPrice);
startActivity(in);
}
});

if (cursor.moveToFirst()) {
do {
id = cursor.getInt(0);
category = cursor.getString(1);
name = cursor.getString(2);
phone = cursor.getString(3);
price = cursor.getString(4);
desc = cursor.getString(5);
photo = cursor.getBlob(6);

//Bitmap bmp = BitmapFactory.decodeByteArray(photo, 0, photo.length);
DataProvider dataProvider = new DataProvider(id,category, name, phone, price, desc, photo);
listDataAdapter.add(dataProvider);
} while (cursor.moveToNext());
}
}


and in
List1.class
i am trying to retrieve id from database whose listview is clicked so that i can inflate other layout according to that. How can it be done?


List1.class

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list1);
TextView txt=(TextView)findViewById(R.id.pr);
String getPrice=txt.getText().toString();
userDbHelper = new UserDbHelper(getApplicationContext());
sqLiteDatabase = userDbHelper.getReadableDatabase();
ImageView img1=(ImageView)findViewById(R.id.image2);
Intent in=getIntent();
Cursor cursor=userDbHelper.getContact(getPrice,sqLiteDatabase);
if(cursor.moveToFirst())
{
String NAME=cursor.getString(0);
String PHONE=cursor.getString(1);
String DESC=cursor.getString(2);
byte[] PHOTO=cursor.getBlob(3);
Bitmap bmp = BitmapFactory.decodeByteArray(photo, 0, photo.length);
img1.setImageBitmap(bmp);
}
userDbHelper=new UserDbHelper(getApplicationContext());
sqLiteDatabase=userDbHelper.getReadableDatabase();
price1=(TextView)findViewById(R.id.Price);

}

Answer

Set the ListView ID/Position to be passed:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

       Intent in=new Intent(Books.this,List1.class);
         ///String text = (String) lv.getItemAtPosition(i); //Or you can get the text for the List item you clicked on. 
         in.putExtra("ID_VAR", i); /// ID clicked
       startActivity(in);
     }
 });

Get the data in your onCreate:

Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("ID_VAR", 0);
System.out.println("Debugging:" + intValue); ///Debugging