Reputation: 19
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);
}
Upvotes: 0
Views: 40
Reputation: 1557
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
Upvotes: 1
Reputation: 661
Starting an intent doesn't add data itself. You have to explicitly add the data into the intent.
This can be done by putExtra
method. This method requires two parameter first being a string which is usually a key. You will be retrieving the data back using this key. Other parameter is a value which can be String, int, long, boolean etc.
Here is an example where value is of string type.
in.putExtra("key","value");
In your case. In the onItemClick method extract the Contact object and pass its data along with the intent like
in.putExtra("id",contact.getId());
And in the List1 Activity in onCreate method you can get back the id like this
Intent in = getIntent();
int id = in.getIntExtra("id",-1);
Here -1 is the default value. Also note that we are using the same key used to put the data into the intent while retrieving it which is "id". Then you can query the data from the DB.
Upvotes: 0