何时在ExpandableListview android中调用childview上的notifyDataChanged()

时间:2015-03-02 04:14:07

标签: android expandablelistview

ExpandableListview我使用多个Group Header多个Child view

包含Group Headerimageview

的子视图的第一个checkbox文字“形状”

最初我在shape names中获得6个新childview当我滚动它时,名称重复(不会剩下6个新名字)

ExpandableListviewActivity.java

listDataChild.put(listDataHeader.get(0), shape_list);
Log.e("details", "karjeev112 "+shape_list);

@Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            if (error_flag==1) { // If no network
                Toast.makeText(SearchActivity.this,"Network Error",Toast.LENGTH_SHORT).show();
            }

            else{
                listAdapter=new ExpandableListAdapter(SearchActivity.this, listDataHeader, listDataChild);                      
                expListView.setAdapter(listAdapter);
                expListView.setItemChecked(0, true);
                expListView.setSelected(true);
                listAdapter.notifyDataSetChanged();
                expListView.invalidateViews();
                expListView.requestLayout();

            }

            if (progressDialog!=null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }

ExpandableListAdapter.java

@Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        String childText = (String) getChild(groupPosition, childPosition);  
        Log.e("_childText", "karjeevch "+childText);


        int itemType = getChildType(groupPosition,childPosition);      


        ViewHolder viewHolder = null;
        switch (itemType) {

        case 0:
            viewHolder = null;
            if (convertView==null) {

                viewHolder=new ViewHolder();                
                convertView = infalInflater.inflate(R.layout.list_child_shape, null);
                viewHolder.shape_name = (CheckBox) convertView.findViewById(R.id.shape_chk_box);
                viewHolder.img_shape_icon=(ImageView)convertView.findViewById(R.id.img_shape);                


                imageLoader.DisplayImage("http://rosycontact.com/shashvat/images/"+childText.toLowerCase()+".png", viewHolder.img_shape_icon);                
                Log.e("shape", "karjeevshp "+childText);
                viewHolder.shape_name.setText(childText);
                convertView.setTag(viewHolder);               

               final CheckBox shape_name_temp=viewHolder.shape_name;                
               viewHolder.shape_name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        // TODO Auto-generated method stub

                        int id=buttonView.getId();
                        if (id==R.id.shape_chk_box) {

                            if (shape_name_temp.isChecked()==true) {

                                String shape_str=shape_name_temp.getText().toString();
                                All_link.SHAPE_LIST.add(shape_str);
                                Toast.makeText(_context, shape_name_temp.getText().toString(), Toast.LENGTH_SHORT).show();
                                Log.e("chk_shape", "karjeevch "+shape_name_temp.getText().toString());
                            }
                            else{
                                String shape_str=shape_name_temp.getText().toString();
                                All_link.SHAPE_LIST.remove(shape_str);
                            }
                        }                                                                                           
                    }
                });                                          
            }
            else{
                viewHolder=(ViewHolder)convertView.getTag();
            }
            return convertView;
}

2 个答案:

答案 0 :(得分:1)

如果convertView不为NULL,则不设置ChildViews的数据。当滚动childViews时,为什么会有重复数据。

尝试设置如下数据。

 if (convertView==null) {
    //Inflate a new View only if its null

                    viewHolder=new ViewHolder();                
                    convertView = infalInflater.inflate(R.layout.list_child_shape, null);
                    viewHolder.shape_name = (CheckBox) convertView.findViewById(R.id.shape_chk_box);
                    viewHolder.img_shape_icon=(ImageView)convertView.findViewById(R.id.img_shape);                
    ......}
    ......
    //Put the appropriate data in your Views.

     viewHolder.shape_name.setText(childText);
     ......
    //more code

答案 1 :(得分:0)

也不要在onPostExecute

中创建新的ExpandableListAdapter

1.在onCreate of activity中创建一个新的Adapter。     2.您可以通过清除列表并添加以下内容来修改ExpandableListAdapter的数据,即listDataHeader和listDataChild。     适当的数据项给他们。     3.然后你可以在你的onPostExecute中调用notifyDataSetChange()。