• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Getting handles to multiple EditTexts in a ListView

ianww

Lurker
I have a dynamic length ListView of multiple EditText items, ie. it can have anything from 3 to 27 EditTexts in a scrolling vertical list. I'm using a SimpleCursorAdapter.

The user can make changes to any number of those EditTexts, then click either an Accept button or a Reject button for the whole list.

Can someone please suggest a way that I could interrogate the ListView at run time after an Accept click to get a handle to each of the EditText items contained in the ListView one by one, so that I can save the values in them back into my SQLite database?

Thanks.
 
If you create these edittext items dynamically you should also be able to save a reference to each item when you add it to your listview.
 
Thanks for the response - I wonder how I might do that? Here are some of the relevant bits of my code:

//This is my parent ListView
setContentView(R.layout.weight_edit);
...

//Here's my cursor, which grabs rows made up of 3 columns (+ the id) from my SQLite database
cursor = db.rawQuery("SELECT _id, criterion, localweight, globalweight FROM "+ dbTableName + " ORDER BY criterion", null);
...

//Here's my SimpleCursorAdapter. The weight_edit_items xml is a row
//with 3 edit texts to take the 3 columns of the cursor row
mAdapter = new SimpleCursorAdapter(this, R.layout.weight_edit_items, cursor, new String[]{"criterion","localweight","globalweight"}, new int[]{R.id.criterion_edit, R.id.localweight_edit,R.id.globalweight_edit});
...

//I then set the ListAdapter. Depending on a user option, this will display
//between 3 and 27 rows of EditText fields. I presume it does this by iterating through the weight_edit_items xml between 3 and 27 times to create each row of the display
this.setListAdapter(mAdapter);

To get references to the EditText fields I guess I'd have to get inside the calls to the weight_edit_items xml somehow?

Alternatively, is there a method in the SimpleCursorAdapter class through which I can get a handle to all the EditText items that it binds? (I've looked but nothing's jumping out at me.)

I'd appreciate any advice!

Thanks.
 
in stead of adding you array of items directly you could add each item in a loop.

for(int i = 0; i < arraySize; i++)
{
EditText item = new EditText();
myEditTextArrayList.add(item);
mAdapter.add(item);
}

Something like this.
 
I've found a way, without having to abandon my SimpleCursorAdapter. :)

I get the number of items in my SimpleCursorAdapter's variable length list using mAdapter.getCount().

I then work through each of those item views one by one, getting from each the EditText field that I want and then extracting the text from that.

Code is:

private void saveUpdatedValues()
{
for (int i = 0; i < mAdapter.getCount(); i++){
EditText v = (EditText) mAdapter.getView(i, null, null).findViewById(R.id.localweight_edit);
String myString = v.getText().toString();
//then more code to save that string to my database as an update for database row i}
}
 
Back
Top Bottom