开发者

Android ListAdapter or Possibly ListView Updating Text at Runtime

开发者 https://www.devze.com 2023-04-09 17:07 出处:网络
Hello folkes I have this l开发者_如何转开发ittle problem for which I cannot find a suitable answer looking around the web and on these forums. Please don\'t direct me to articles in which people have

Hello folkes I have this l开发者_如何转开发ittle problem for which I cannot find a suitable answer looking around the web and on these forums. Please don't direct me to articles in which people have requested list view text color changes at run time, as I read lots of them and not found one to help me out.

I have a simple ListView that displays an array of String objects via the use of a ListAdapter.

I need to update some of ListView Strings at run time, based on their contents. Using a global reference to the list adapter used in the lists views creation I can get the contents of each list view String using following code below.

However, in addition to retrieval I'd like to be able to modify each string in turn, then put it back in the same index position and have the list view reflect the changes. How?

        for (int x = 0; x <= listAdapter.getCount();x++)
        {
            Object o = this.listAdapter.getItem(x);

            if (o.getClass().getSimpleName().equals("String"))
            {
                String s = (String) o;

                  s = modifyString(s);

                //s is the string I want to modify then put back in the same place.

            }//end if
        }//end for


As far as I know you cannot change the items in an Adapter - unless you are using a custom Adapter (by extending a BaseAdapter etc...)

So, I think you will have to:

  1. make sure you Adapter's constructor takes in the data structure that holds your strings
  2. make sure your data structure is global
  3. make the changes in that data structure whenever you need to
  4. call myAdapter.notifyDataSetChanged();

This will tell adapter that there were changes done in the list and listview should be recreated.

And after your listview is renewed you can even take the user back to the index by:

list.setSelection(positionWhereTheUserClicked);

I hope this helps, let me know if you need more code references.

Here is some code

private ArrayList<String> results = new ArrayList<String>();  //global
private BaseAdapter searchAdapter = new BaseAdapter (results, this);  //global

private void updateResults(final ArrayList<String> updatedList){
    results = updatedList;
    final ListView list = (ListView)findViewById(R.id.search_results);
    list.setAdapter(searchAdapter);
    list.setOnItemClickListener(new ListView.OnItemClickListener(){
        // implementation of what happens when you click on an item //
    });
    searchAdapter.notifyDataSetChanged();
}

This code works just fine on my end, I hope it helps.


Just stumbled on this problem and found a solution. I'm using a

m_ListAdapter = new SimpleAdapter(this, m_List, R.layout.option_list_row, columns, renderTo);

Each item in my listView is a manu option causing a dialog to show, once data is received through the dialog, all I have to do is just create a new SimpleAdapter with an updated ArrayList that includes the new data, then just setAdapter to the new adapter. The ListView will update instantly.

0

精彩评论

暂无评论...
验证码 换一张
取 消