Android-在Asynch调用之后无法更新ListAdapter

Android-在Asynch调用之后无法更新ListAdapter,第1张

概述我正在像这样设置一个ListView适配器:publicclassSeeAllQuestionsActivityextendsActivity{//ArrayAdapter<Question>adapter;SimpleAdaptermSchedule=null;ListViewlist=newListView(this);TextViewloading_questions=null;@Over

我正在像这样设置一个ListVIEw适配器:

public class SeeAllQuestionsActivity extends Activity{    //ArrayAdapter<Question> adapter;       SimpleAdapter mSchedule = null;    ListVIEw List = new ListVIEw (this);    TextVIEw loading_questions = null;    @OverrIDe    public voID onCreate(Bundle savedInstanceState)     {        super.onCreate(savedInstanceState);                setContentVIEw(R.layout.all_question_page);        TextVIEw loading_questions = (TextVIEw) findVIEwByID(R.ID.loading_questions);        List = (ListVIEw) findVIEwByID(R.ID.List);        ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();        HashMap<String, String> map = new HashMap<String, String>();        mSchedule = new SimpleAdapter(this, myList, R.layout.questions_List,                new String[] {"train", "from", "to"},                 new int[] {R.ID.TRAIN_CELL, R.ID.FROM_CELL, R.ID.TO_CELL});        List.setAdapter(mSchedule);        List.setTextFilterEnabled(true);        List.setonItemClickListener(new OnItemClickListener()         {            public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw,                int position, long ID)             {                            ...

然后进行远程Asynch调用以从数据库中获取列表,然后尝试在onPostExecute方法中执行以下 *** 作:

ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();                HashMap<String, String> map = new HashMap<String, String>();                    try                    {                        JsONArray obj = new JsONArray(result);                        if ( obj != null )                        {                            for ( int i = 0; i < obj.length(); i++ )                            {                                JsONObject o = obj.getJsONObject(i);                                map.put("train", "Business name");                                map.put("from", ">");                                map.put("to", ">");                                myList.add(map);                                map = new HashMap<String, String>();                                map.put("train", "103(x)");                                map.put("from", "6:35 AM");                                map.put("to", "7:45 AM");                                myList.add(map);                                                                                                }                        }                    }                    catch ( Exception e )                    {                    }                                       List.setAdapter(mSchedule);         

但是我在这行上得到了一个空指针异常:

ListVIEw List = new ListVIEw (this);

但是我认为一般来说,在postExecute方法中该如何完成尚不可行.非常感谢您提供有关如何正确执行此 *** 作的帮助.

解决方法:

在OnCreate中,您定义一个新的SimpleAdapter并将其附加到ListVIEw.这是对的.数据源(在您的情况下为myList)目前为空,因此您将在AsyncTask中填充它.

在onPostExecute中,您将创建一个新的ArrayList.根据您收到的结果,填写它.完成此 *** 作后,您将再次设置适配器.因为适配器没有数据,所以什么也不做..您要做的是将新的填充列表提供给Adapter,以便可以用数据填充ListVIEw.

解决方案1

onPostExecute {   // create a List to store your data   new ArrayList   // fill the new List with the data you received (result)   fillArrayList from JsON   // create an adapter and give the new List with it   mAdapter = new SimpleAdapter(.., ArrayList, ...)   ListVIEw.setAdapter(mAdapter);   }

这是您可以执行的一种方法,并且适合您当前的实现.

解决方案2

我会选择这种解决方案

onPostExecute {    // don't create a new arrayList but use the myList-object you created in the OnCreate    fill myList object with new data     // myList is the datasource of your adapter and it is Now changed.    // let the ListVIEw kNow his adapter received new information    mSchedule.notifyDataSetChanged}

UPDATE

签出this tutorial.我使用相同的布局和相同的来源来填充列表,但我对其进行了更改,因此与您的情况类似.祝好运 :)

主要活动

public class MainActivity extends Activity {    private List<HashMap<String, String>> fillMaps;    private SimpleAdapter adapter;    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        ListVIEw lv = (ListVIEw) findVIEwByID(R.ID.ListvIEw);        String[] from = new String[] { "rowID", "col_1", "col_2", "col_3" };        int[] to = new int[] { R.ID.item1, R.ID.item2, R.ID.item3, R.ID.item4 };        // My data        fillMaps = new ArrayList<HashMap<String, String>>();        // Create an adapter which will tell my ListVIEw what to show..        // fillMaps is still empty so at the moment it my ListVIEw will show        // nothing.        adapter = new SimpleAdapter(this, fillMaps, R.layout.row, from, to);        lv.setAdapter(adapter);        // Retreive data from somewhere        new UpdateListWithAsyncTask().execute();    }    private class UpdateListWithAsyncTask extends AsyncTask<VoID, VoID, VoID> {        protected VoID doInBackground(VoID... params) {            // do stuff            return null;        }        protected voID onPostExecute(VoID result) {            // Fill your datasource that your adapter has. In my case fillMaps            for (int i = 0; i < 10; i++) {                HashMap<String, String> map = new HashMap<String, String>();                map.put("rowID", "" + i);                map.put("col_1", "col_1_item_" + i);                map.put("col_2", "col_2_item_" + i);                map.put("col_3", "col_3_item_" + i);                fillMaps.add(map);            }            // my datasource is Now changed, I want my adapter to kNow this and            // update my ListVIEw            adapter.notifyDataSetChanged();        }    }}
总结

以上是内存溢出为你收集整理的Android-在Asynch调用之后无法更新ListAdapter全部内容,希望文章能够帮你解决Android-在Asynch调用之后无法更新ListAdapter所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1094187.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存