我正在使用 ListAdapter 来填充 ListView,如下所示:
static final String[] PROBLEMS = new String[] {"one", "two", "three" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.my_problems, PROBLEMS));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
之后,我通过 AsyncTask 调用远程调用我的服务器以获取该列表的更多数据,当我从服务器取回数据时,我不知道如何填充和重置 ListView。到目前为止,我有这样的事情:
@Override
protected void onPostExecute(String result)
{
// Unwrap the stuff from the JSON string
String problem_title = null;
String problem_id = null;
try
{
JSONArray obj = new JSONArray(result);
JSONObject o = obj.getJSONObject(0);
Log.d( "Title: " , "" + o.getString("problem_title") );
Log.d( "id: " , "" + o.getString("problem_id") );
problem_title = o.getString("problem_title");
problem_id = o.getString("problem_id");
}
catch ( Exception e )
{
}
// Now not sure what to do :)
// How do I reset the list that I had set up above?
}
我可以将结果转化为适当的结构化数据以重置列表,但不确定如何完成。有人可以帮忙吗? :)
最佳答案
我是这样用的
values = new ArrayList<String>();
//put anything you want in values as start
adapter = new ArrayAdapter<String>(this,R.layout.notification, values);
setListAdapter(adapter);
然后
//change values array with your new data then update the adapter
adapter.notifyDataSetChanged();
然后listview的内容会在你执行这个函数的时候发生变化
关于android - 如何在获取数据后使用 ArrayAdapter 重置 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9647507/