Omar Abdelhafiz Omar Abdelhafiz - 1 month ago 27
Java Question

How to read data from EditTexts in a ListView populated using a custom CursorAdapter?

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);
int nameColumnIndex = cursor.getColumnIndex(StoreContract.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
won't do the job.

Ben Ben
Answer

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