Reputation: 1922
Hello I have an odd issue regarding my custom adapter listview.
This is my code,.
DBHelper.class:
public Cursor getAllStockCardListings()
{
Cursor localCursor =
this.myDataBase.query(TBL_STOCKCARD, new String[] {
KEY_ID, KEY_ITEMCODE, KEY_LOCATIONCODE,
KEY_TRANSACTIONCODE, KEY_BEGINNINGBALANCE, KEY_ENDINGBALANCE },
null , null, null, null, null);
if (localCursor != null)
localCursor.moveToFirst();
return localCursor;
}
MainActivity.class:
class CustomAdapter extends CursorAdapter
{
LayoutInflater inflater;
@SuppressWarnings("deprecation")
public CustomAdapter(Context context, Cursor c) {
super(context, c);
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
//tvTransactionCode
tvID = (TextView) view.findViewById(R.id.tvSerialNumber);
tvItemCode = (TextView) view.findViewById(R.id.tvItemCode);
tvLocationCode = (TextView) view.findViewById(R.id.tvLocationCode);
tvTransactionCode = (TextView) view.findViewById(R.id.tvTransactionCode);
tvBeginningBalance = (TextView) view.findViewById(R.id.tvBeginningBalance);
tvEndingBalance = (TextView) view.findViewById(R.id.tvEndingBalance);
tvID.setText(cursor.getString(
cursor.getColumnIndex(dbHelper.KEY_ID)));
tvItemCode.setText(cursor.getString(
cursor.getColumnIndex(dbHelper.KEY_ITEMCODE)));
tvLocationCode.setText(cursor.getString(
cursor.getColumnIndex(dbHelper.KEY_LOCATIONCODE)));
tvTransactionCode.setText(cursor.getString(
cursor.getColumnIndex(dbHelper.KEY_TRANSACTIONCODE)));
tvBeginningBalance.setText(cursor.getString(
cursor.getColumnIndex(dbHelper.KEY_BEGINNINGBALANCE)));
tvEndingBalance.setText(cursor.getString(
cursor.getColumnIndex(dbHelper.KEY_ENDINGBALANCE)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.list_stockcard, parent, false);
}
}
@Override
protected void onDestroy() {
// TODO onDestroy
super.onDestroy();
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
// TODO onItemClick
dbHelper.close();
String selectedFromList = tvID.getText().toString().trim();
Log.d("ID", selectedFromList);
Intent i = new Intent(this, ViewStockCard.class);
i.putExtra("ID", selectedFromList);
startActivity(i);
}
I have records 6 records and IDs are 1,2,3...6
But the odd thing here is, whenever I click on the item that has an ID of 2, I checked the logcat and the ID is 1 instead of 2. And I tried clicking ID 5, it's giving 6. There are only two records that give the correct selected items 1 and 6 and the rest are only giving 1, 4, 6. What seems to be wrong in my code? I really need your help guys. Thanks a lot.
Upvotes: 0
Views: 456
Reputation: 1922
I followed @vipul mittal's answer
But I removed
cursor.move(pos+1);
This is my final code:
@Override
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
// TODO onItemClick
dbHelper.close();
String selectedFromList = cursor.getString(
cursor.getColumnIndex(dbHelper.KEY_ID));
Log.d("ID", selectedFromList);
Intent i = new Intent(this, ViewStockCard.class);
i.putExtra("ID", selectedFromList);
startActivity(i);
}
And also, getting the string value of long id makes it work as well such as this:
String selectedFromList = String.valueOf(id);
Upvotes: 1
Reputation: 17401
Get the cursor reference in your CustomAdapter
and get Id from the cursor
@Override
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
// TODO onItemClick
dbHelper.close();
cursor.move(pos+1);
String selectedFromList = cursor.getString(
cursor.getColumnIndex(dbHelper.KEY_ID));
Log.d("ID", selectedFromList);
Intent i = new Intent(this, ViewStockCard.class);
i.putExtra("ID", selectedFromList);
startActivity(i);
}
Upvotes: 0