I am reading from a database into a listview using a custom cursor adapter. The listview shows some details of each entry in the database. When I click on a list item, I want to open a details page that shows the other details of that record that are not displayed in the listview. I can’t figure it out.
I have attached the code snippets of the cursor creation and the adapter creation which are in MainActivity. I also have a separate customAdapter class and a class for the details page.
MainActivity:
public class MainActivity extends ListActivity {
....
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
...
}
private void fillList() {
ListView lv = getListView();
Cursor c = db.getData();
startManagingCursor(c);
CustomCursorAdapter adapter = new CustomCursorAdapter(getApplicationContext(), c);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Intent k = new Intent(MainActivity.this, ProductDetails.class);
}
});
}
}
CustomCursorAdapter:
public class CustomCursorAdapter extends CursorAdapter {
public CustomCursorAdapter(Context context, Cursor cursor) {
super(context, cursor);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.list_entry, parent, false);
bindView(v, context, cursor);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView txtvw1 = (TextView)view.findViewById(R.id.prodcode_entry);
txtvw1.setText(cursor.getString(cursor.getColumnIndex(ProdDB.KEY_PRODCODE)));
TextView txtvw2 = (TextView)view.findViewById(R.id.prodtype_entry);
String ptype = (cursor.getString(cursor.getColumnIndex(ProdDB.KEY_PRODTYPE)));
txtvw2.setText(ptype.substring(0, 1));
TextView txtvw3 = (TextView)view.findViewById(R.id.prodcat_entry);
String cat = (cursor.getString(cursor.getColumnIndex(ProdDB.PRODCAT)));
txtvw3.setText(cat.substring(0, 2));
TextView txtvw4 = (TextView)view.findViewById(R.id.prodcost_entry);
float price = (cursor.getFloat(cursor.getColumnIndex(ProdDB.PRODPRICE)));
txtvw4.setText(Float.toString(price));
TextView txtvw5 = (TextView)view.findViewById(R.id.prodSup_entry);
txtvw5.setText(cursor.getString(cursor.getColumnIndex(ProdDB.SUPPLIER)));
}
}
ProdDetails
public class ProductDetails extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_details);
}
}
db.getData() – from ProdDB
public Cursor getData() {
String[] columns = new String[] {KEY_ROWID,
KEY_PRODCODE,
KEY_PRODNAME,
KEY_PRODTYPE,
KEY_PRODCAT,
KEY_PRODPRICE,
KEY_PRODRRP,
KEY_PRODSUPPLIER,
KEY_NOTES};
return db.query(DB_TABLE, columns, null, null, null, null, null);
}
After this line:
pass also the id of the item:
Then, from within the
ProductDeftailsactivity, get the id again, query the database and retrieve its information:getLongExtrareceives the key of the extra to retrieve plus a default value. It is always worthy to check the returned value against the default value to make sure the activity actually received something.So, just remember this next time… creating an intent does not launch the activity it represents (you have to use either
startActivityorstartActivityForResultmethods). Also, you can pass data to an activity that is going to be launched using the Intent’s extras. It can be primitive types, Strings, String’s ArrayLists, or Parcelables (single objects, array lists or arrays).Details
To get the details of your product you can use something like this:
You can put that inside a new method called
getDetails(long id)(insideProdDBclass). Then, from within your activity you can do: