AsyncTask的更新后expandablelistviewAsyncTask、expandablelistview

2023-09-07 09:47:18 作者:齐眉念别离

我为我的英语道歉,但与谷歌的翻译。我需要帮助我的应用程序,我使用的是 ExpandableListView 是 3种不同的组,2组是静态项目并与项目组,它应该为更新之后的AsyncTask 。当我运行应用程序的第一次,具有动态项目组的空,而不是第二次为可见。我该怎么办,实时动态的项目更新?我的应用程序是这样构成的,我有一个实例化一个主要活动的 ExpandableListView ,执行的其他类的AsyncTask 然后我有扩展 BaseExpandableListAdapter A类。我试图用在主要活动中的方法 notifyDataSetChanged(),但它不工作。我希望我是不够清楚,我希望答案是同样明显,可惜我一直都在互联网上,但无法找到我一直在寻找答案。在此先感谢那些谁帮我。

MainActivity.java

 公共类MainActivity延伸活动{私人ExpandableListView expListView;私人ExpandableListAdapter listAdapter;清单<串GT; listDataHeader;HashMap的<字符串列表<串GT;> listDataChild;静态的ArrayList<串GT; allResults =新的ArrayList<串GT;(5);    @覆盖    公共无效的onCreate(捆绑savedInstanceState){        // TODO自动生成方法存根        super.onCreate(savedInstanceState);        的setContentView(R.layout.activity_main);        prepareListData();        expListView =(ExpandableListView)findViewById(R.id.explist);        listAdapter =新ExpandableListAdapter(这一点,listDataHeader,listDataChild);        expListView.setAdapter(listAdapter);    }    私人无效prepareListData(){      GetResults getResults =新GetResults();      getResults.execute();      allResults = getResults.getAllResults();      listDataHeader =新的ArrayList<串GT;();      listDataChild =新的HashMap<字符串列表<串GT;>();      / **头的可视化* /      listDataHeader.add(的getString(R.string.drawer_title_advanced));      listDataHeader.add(的getString(R.string.drawer_title_visualization));      listDataHeader.add(的getString(R.string.drawer_title_route));      //添加子数据      清单<串GT;设置=新的ArrayList<串GT;();      清单<串GT;可视化=新的ArrayList<串GT;();      清单<串GT;路线=新的ArrayList<串GT;();      items1.add(组1);      items2.add(组2);      的for(int i = 0; I< allResults.size();我++){          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

 公共类GetResults延伸的AsyncTask<弦乐,太虚,字符串> {    私有静态的ArrayList<串GT; allResults =新的ArrayList<串GT;(5);        @覆盖        保护字符串doInBackground(字符串... PARAMS){           //        }        @覆盖        保护无效onPostExecute(字符串RES){                JSONArray JsonArrayResult =新JSONArray(RES);                的for(int i = 0; I< JsonArrayResult.length();我++){                     JSONObject的JsonObjectResult = JsonArrayResult.getJSONObject(I)                     allResults.add(JsonObjectResult.getString(结果););                }        }        公众的ArrayList<串GT; getAllResults(){           返回allResults;        }   } 

ExpandableListAdapter.java

 公共类ExpandableListAdapter扩展BaseExpandableListAdapter {        私人语境_context;        私人列表<串GT; _listDataHeader; //头标题        私人的HashMap<字符串列表<串GT;> _listDataChild;        私人LayoutInflater infalInflater;     公共ExpandableListAdapter(上下文的背景下,                清单<串GT; listDataHeader,                HashMap的<字符串列表<串GT;> listChildData){            this._context =背景;            this._listDataHeader = listDataHeader;            this._listDataChild = listChildData;        }    @覆盖    公众诠释getGroupCount(){        // TODO自动生成方法存根        返回this._listDataHeader.size();    }    @覆盖    公众诠释getChildrenCount(INT groupPosition){        // TODO自动生成方法存根        返回this._listDataChild.get(this._listDataHeader.get(groupPosition))                。尺寸()​​;    }    @覆盖    公共对象getGroup(INT groupPosition){        // TODO自动生成方法存根        返回this._listDataHeader.get(groupPosition);    }    @覆盖    公共对象getChild(INT groupPosition,诠释childPosition){        返回this._listDataChild.get(this._listDataHeader.get(groupPosition))                获得(childPosition);    }    @覆盖    众长getGroupId(INT groupPosition){        // TODO自动生成方法存根        返回3;    }    @覆盖    众长getChildId(INT groupPosition,诠释childPosition){        // TODO自动生成方法存根        返回childPosition;    }    @覆盖    公共布尔hasStableIds(){        // TODO自动生成方法存根        返回false;    }@覆盖公共查看getGroupView(INT groupPosition,布尔isExpanded,        查看convertView,父母的ViewGroup){    字符串headerTitle =(字符串)getGroup(groupPosition);    视图V = NULL;    如果(convertView!= NULL)        V = convertView;    其他{        //如果(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);    返回伏;}    @覆盖    公共查看getChildView(INT groupPosition,诠释childPosition,            布尔isLastChild,查看convertView,父母的ViewGroup){        视图V = NULL;        infalInflater =(LayoutInflater)this._context                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);        如果(convertView == NULL){            如果(groupPosition == 0){                V = infalInflater.inflate(R.layout.adv_item,NULL);            }            如果(groupPosition == 1){                V = infalInflater.inflate(R.layout.visual_item,NULL);            }            如果(groupPosition == 2){                V = infalInflater.inflate(R.layout.routes_item,NULL);            }        }其他{            V = convertView;        }        返回伏;    } } 
AsyncTask研究 以Android 10.0为准

解决方案

您需要调用 notifyDataSetChanged() onPostExecute()的方法,你的的AsyncTask

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

解决方案

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