Closed. This question needs debugging details。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

2年前关闭。



Improve this question




我正在打开一个新的 Activity ,该 Activity 使用CursorLoader从本地SQLite数据库获取数据。每当我单击列表项时,由于某种原因,它总是填充第一项中的值。我想填充我单击的特定列表项的值。例如:如果我单击“监视”项,它仍会在Edittext中填充“书”值。我在这里想念什么吗?

java - 当我单击ListView中的项目时,我想用其值填充EditText以便在SQLite数据库中更新-LMLPHP

这是我的CursorLoader代码:
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {

    String[] projection = {
            ItemContract.ItemEntry._ID,
            ItemContract.ItemEntry.COLUMN_ITEM_NAME,
            ItemContract.ItemEntry.COLUMN_ITEM_QUANTITY,
            ItemContract.ItemEntry.COLUMN_ITEM_PRICE,
            ItemContract.ItemEntry.COLUMN_ITEM_IMAGE
    };

    return new CursorLoader(this, ItemContract.ItemEntry.CONTENT_URI,projection,null,null,null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

    if (cursor == null || cursor.getCount() < 1) {
        return;
    }

    if (cursor.moveToFirst()) {

        int nameColumnIndex = cursor.getColumnIndex(ItemContract.ItemEntry.COLUMN_ITEM_NAME);
        int quantityColumnIndex = cursor.getColumnIndex(ItemContract.ItemEntry.COLUMN_ITEM_QUANTITY);
        int priceColumnIndex = cursor.getColumnIndex(ItemContract.ItemEntry.COLUMN_ITEM_PRICE);
        int imageColumnIndex = cursor.getColumnIndex(ItemContract.ItemEntry.COLUMN_ITEM_IMAGE);


        String editCurrentName = cursor.getString(nameColumnIndex);
        int editCurrentQuantity = cursor.getInt(quantityColumnIndex);
        Float editCurrentPrice = cursor.getFloat(priceColumnIndex);
        String editCurrentImage = cursor.getString(imageColumnIndex);


        if(editCurrentImage != null) {
            editImage.setVisibility(View.VISIBLE);
            editImage.setImageURI(Uri.parse(editCurrentImage));

        }
        else {
            editImage.setVisibility(View.GONE);
        }
        editName.setText(editCurrentName);
        editQuantity.setText(Integer.toString(editCurrentQuantity));
        editPrice.setText(Float.toString(editCurrentPrice));

    }

}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    Log.e("EDITACTIVITY", "onLoaderReset: ");
    editName.setText("");
    editQuantity.setText("");
    editPrice.setText("");
    editImage.setImageDrawable(null);

}

最佳答案

这将是您如何从Cursor或AnyView的位置检索ListView的问题。从Cursor中的onItemClickListener或AnyView本身获取的ListView中检索值的方法:

public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
        //Get the cursor, positioned to the corresponding row in the result set
        Cursor cursor = (Cursor) listView.getItemAtPosition(position);
       // Use the Cursor to retrieve the value from here!
}

09-25 22:15