Android-在AsyncTask之后更新listview [英] Android - update listview after AsyncTask

查看:92
本文介绍了Android-在AsyncTask之后更新listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的英语致歉,但已通过Google翻译.我需要我的应用程序帮助,我正在使用具有 3个不同组的ExpandableListView,具有 static 项的2个组以及具有应的项的组AsyncTask之后> update . 第一次运行应用程序时,包含动态项目的组为,而第二次为可见.如何实时更新动态项目? 我的应用程序是以这种方式构造的,我有一个实例化ExpandableListView的主要活动,另一个是执行AsyncTask的类,然后我有一个扩展BaseExpandableListAdapter的类.我试图在主要活动中使用方法notifyDataSetChanged(),但是它不起作用. 我希望我已经足够清楚,也希望答案也一样清晰,但不幸的是,我已经遍及整个互联网,但是找不到我想要的答案.在此先感谢那些会帮助我的人.

I apologize for my English but translated with google. I need help for my application, I'm using a ExpandableListView with 3 different groups, 2 groups with static items and a group with items that it should to update after a AsyncTask. When I run the application the first time, the group with dynamic items is empty, instead the second time is visible. How I do to update in real time dynamic items? My application is structured in this way, I have a main activity that instantiates the ExpandableListView, an other class that executes the AsyncTask and then I have a class that extends BaseExpandableListAdapter. I tried to use the method notifyDataSetChanged() in main activity, but it does not work. I hope I was clear enough and I hope that answers are equally clear, unfortunately I have been all over the internet but could not find the answer I was looking for. Thanks in advance to those who will help me.

MainActivity.java

public class MainActivity extends Activity {

private ExpandableListView expListView;
private ExpandableListAdapter listAdapter;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
static ArrayList<String> allResults = new ArrayList<String>(5);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        prepareListData();

        expListView = (ExpandableListView) findViewById(R.id.explist);
        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
        expListView.setAdapter(listAdapter);

    }

    private void prepareListData() {
      GetResults getResults = new GetResults();
      getResults.execute();
      allResults = getResults.getAllResults();

      listDataHeader = new ArrayList<String>();
      listDataChild = new HashMap<String, List<String>>();

      /** header visualization */
      listDataHeader.add(getString(R.string.drawer_title_advanced));
      listDataHeader.add(getString(R.string.drawer_title_visualization));
      listDataHeader.add(getString(R.string.drawer_title_route));

      // Adding child data
      List<String> settings = new ArrayList<String>();
      List<String> visualization = new ArrayList<String>();
      List<String> routes = new ArrayList<String>();

      items1.add("Group 1");
      items2.add("Group 2");

      for (int i = 0; i < allResults.size(); i++) {
          items3.add(allResults.get(i));
      }

      listDataChild.put(listDataHeader.get(0), items1);
      listDataChild.put(listDataHeader.get(1), items2);
      listDataChild.put(listDataHeader.get(2), items3);
    }
}

GetResults.java

    public class GetResults extends AsyncTask<String, Void, String> {

    private static ArrayList<String> allResults = new ArrayList<String>(5);

        @Override
        protected String doInBackground(String... params) {
           //
        }

        @Override
        protected void onPostExecute(String res) {
                JSONArray JsonArrayResult = new JSONArray(res);
                for (int i = 0; i < JsonArrayResult.length(); i++) {
                     JSONObject JsonObjectResult = JsonArrayResult.getJSONObject(i);
                     allResults.add(JsonObjectResult.getString("results"););

                }
        }

        public ArrayList<String> getAllResults() {
           return allResults;
        }
   }

ExpandableListAdapter.java

    public class ExpandableListAdapter extends BaseExpandableListAdapter {
        private Context _context;
        private List<String> _listDataHeader; // header titles
        private HashMap<String, List<String>> _listDataChild;
        private LayoutInflater infalInflater;

     public ExpandableListAdapter(Context context,
                List<String> listDataHeader,
                HashMap<String, List<String>> listChildData) {
            this._context = context;
            this._listDataHeader = listDataHeader;
            this._listDataChild = listChildData;
        }
    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return this._listDataHeader.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return 3;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    View v = null;
    if (convertView != null)
        v = convertView;
    else {
        // if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = infalInflater.inflate(R.layout.list_group1, null);
    }

    TextView lblListHeader = (TextView) v.findViewById(R.id.lblListHeader);
    // lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return v;
}

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        View v = null;
        infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            if (groupPosition == 0) {
                v = infalInflater.inflate(R.layout.adv_item, null);
            }
            if (groupPosition == 1) {
                v = infalInflater.inflate(R.layout.visual_item, null);
            }
            if (groupPosition == 2) {
                v = infalInflater.inflate(R.layout.routes_item, null);
            }
        } else {
            v = convertView;
        }
        return v;
    }
 }

推荐答案

您需要在AsyncTaskonPostExecute()方法中调用notifyDataSetChanged().

You need to call notifyDataSetChanged() in the onPostExecute() method of your AsyncTask.

这篇关于Android-在AsyncTask之后更新listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆