2

I have a ListView populated using a custom CursorAdapter that reads my database to get the data.

The ListItem contains one TextView which has the data read above and two EditTexts which I want to read the data from, and update the database with the values in them.

Here is the updater_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <TextView
            android:id="@+id/good_name"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/height_of_list_item"
            android:paddingLeft="@dimen/padding"
            android:gravity="center"
            tools:text="Frozen Chicken"/>

        <EditText
            android:id="@+id/sold_edit_text"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/height_of_list_item"
            android:paddingLeft="@dimen/padding"
            android:gravity="center"
            android:inputType="number"
            android:hint="@string/hint_remove_quantity"/>

        <EditText
            android:id="@+id/received_edit_text"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/height_of_list_item"
            android:paddingLeft="@dimen/padding"
            android:gravity="center"
            android:inputType="number"
            android:hint="@string/hint_add_quantity"/>

</LinearLayout>

OnCreate:

    // Find the views
    ListView list = (ListView) findViewById(R.id.list_updater);
    TextView textView = (TextView) findViewById(R.id.updater_empty_view);

    // Set the empty view
    list.setEmptyView(textView);

    // Set the ListView to the custom CursorAdapter
    mAdapter = new UpdaterCursorAdapter(this, null);
    list.setAdapter(mAdapter);

    // the loader requests the data from the database and add it to the adapter
    getLoaderManager().initLoader(STORE_LOADER, null, this);

Edit: Here is code for the custom CursorAdapter

public class UpdaterCursorAdapter extends CursorAdapter {

    public UpdaterCursorAdapter(Context context, Cursor c) {
        super(context, c, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.updater_list_item, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
    TextView nameTextView = (TextView) view.findViewById(R.id.good_name);
    EditText soldEditText = (EditText) view.findViewById(R.id.sold_edit_text);
    EditText receivedEditText = (EditText) view.findViewById(R.id.received_edit_text);

    int nameColumnIndex = cursor.getColumnIndex(StoreEntry.COLUMN_GOOD_NAME);

    String goodName = cursor.getString(nameColumnIndex);

    nameTextView.setText(goodName);
    }
}

How to get the data from the two EditTexts for each Item in the ListView?!

Note: I want the data to be saved on my database when I click on a MenuItem so onItemClickListener or addTextChangedListener won't do the job.

  • It will be in your adapter. Post your adapter code and we'll be able to help you. – JDenais Nov 01 '16 at 11:28
  • I did post it.. –  Nov 01 '16 at 11:36
  • http://stackoverflow.com/questions/23716432/cursoradapter-in-listview?rq=1 – Secret_Volcano Nov 01 '16 at 11:47
  • I had same problem you can checkout this - http://stackoverflow.com/questions/26286006/how-to-preserve-edittext-value-in-custom-cursor-adapter – Wasim K. Memon Nov 01 '16 at 12:07
  • you can't do that directly because of view recycling. you need to store each individual edited text in an intermediary object and then save that object. Because your listview does not give you access to the state of the views that are not displayed. – njzk2 Nov 01 '16 at 13:42
  • alright give me the indirect way @njzk2 –  Nov 01 '16 at 13:44

1 Answers1

0

You just need to add your edit text to you adapter, and then add a textWatcher and update the database as you always would.

public class UpdaterCursorAdapter extends CursorAdapter {
    public UpdaterCursorAdapter(Context context, Cursor c) {
        super(context, c, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.updater_list_item, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView nameTextView = (TextView) view.findViewById(R.id.good_name);
        int nameColumnIndex = cursor.getColumnIndex(StoreContract.StoreEntry.COLUMN_GOOD_NAME);
        String goodName = cursor.getString(nameColumnIndex);
        nameTextView.setText(goodName);

        EditText soldEditText = (EditText) view.findViewById(R.id.sold_edit_text);
        //Set the soldEditText to whatever column your data is in the database same as text view.
        soldEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

               // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
              //This is where you would save the new text your database just
            }
        });
    }
}
Ben
  • 1,285
  • 1
  • 9
  • 15
  • This doesn't help I want the whole database to update when I click on the Save `MenuItem`. –  Nov 01 '16 at 13:14