Android - 在AsyncTask之后更新listview

时间:2015-02-25 14:09:33

标签: android listview android-asynctask expandablelistview android-adapter

我为我的英语道歉但是用谷歌翻译过来。我的应用程序需要帮助,我使用ExpandableListView 3个不同的组,2个组使用静态项目,并使用包含项目的组应该在AsyncTask之后更新。 当我第一次运行应用程序时,具有动态项目的组为空,而第二次是可见。我如何实时更新动态项目? 我的应用程序是以这种方式构建的,我有一个实例化ExpandableListView的主要活动,另一个执行AsyncTask的类,然后我有一个扩展BaseExpandableListAdapter的类。我尝试在主要活动中使用方法notifyDataSetChanged(),但它不起作用。 我希望我足够清楚,我希望答案同样清楚,不幸的是我一直在互联网上,但找不到我想要的答案。在此先感谢那些能帮助我的人。

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;
    }
 }

1 个答案:

答案 0 :(得分:3)

您需要使用notifyDataSetChanged()的{​​{1}}方法拨打onPostExecute()