When I click an item in a ListView, I want to populate the EditText with my values ​​for the update in the SQLite database

I am opening a new action that uses CursorLoaderto get data from a local SQLitedb. Whenever I click on listitem, for some reason it always populates the values ​​from the first element. I want to fill in the values ​​for the selected list item that I clicked. For example, if I click the watch button, it still fills in the values ​​of the book Edittext. Am I missing something here?

Application screenshot

Here is my code for 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);

}
+4
source share
2 answers

, Cursor ListView AnyView. Cursor, onItemClickListener ListView AnyView:

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!
}
0

, , . , .

.

Solution1:

, item .

- : [http://www.viralandroid.com/2016/04/start-new-activity-from-android-listview-onitem-clicked.html][1]

Solution2:

, . , , .

// Here I am storing passed id in one variable, you have to set selected_item_id in intent while starting new activity
 int selectedId = getIntent().getExtras().getInt("selected_item_id",0);

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

 String selection = ItemContract.ItemEntry._ID + "=?";
 String[] selectionArgs = { String.valueOf(selectedId) };

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

.

, selectedId.

, .

0

Source: https://habr.com/ru/post/1694509/