Android从自定义适配器列表视图中选择项目

时间:2014-01-03 12:04:45

标签: android listview android-listview

您好我的自定义适配器列表视图有一个奇怪的问题。

这是我的代码,。

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);

}

我有6条记录,ID是1,2,3 ... 6

但奇怪的是,每当我点击ID为2的项目时,我检查了logcat,ID为1而不是2.我尝试点击ID 5,它给出了6.只有两条记录给出了正确选择的项目1和6,其余的只给出了1,4,6。我的代码中出现了什么错误?我真的需要你的帮助。非常感谢。

2 个答案:

答案 0 :(得分:1)

我关注了@vipul mittal的回答

但我删除了

  cursor.move(pos+1);

这是我的最终代码:

 @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);

}

而且,获取long id的字符串值也可以使其正常工作:

 String selectedFromList = String.valueOf(id);

答案 1 :(得分:0)

CustomAdapter中获取游标参考,并从光标

获取Id
@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);


}
相关问题