Getting handles to multiple EditTexts in a ListView


Last Updated:

  1. ianww

    ianww Member This Topic's Starter

    Joined:
    Jun 11, 2010
    Messages:
    5
    Likes Received:
    0
    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.
     

    Advertisement
  2. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    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.
     
  3. ianww

    ianww Member This Topic's Starter

    Joined:
    Jun 11, 2010
    Messages:
    5
    Likes Received:
    0
    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.
     
  4. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    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.
     
  5. ianww

    ianww Member This Topic's Starter

    Joined:
    Jun 11, 2010
    Messages:
    5
    Likes Received:
    0
    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}
    }
     

Share This Page

Loading...