简体   繁体   中英

when to call notifyDataChanged() on childview in ExpandableListview android

In ExpandableListview I using multiple Group Header with multiple Child view

The first Group Header text "Shape" that has childview containing imageview and checkbox

Initially I get 6 fresh shape names in childview when I scroll this ,the name repeats (not getting remaining 6 fresh names)

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

You are not setting the data for the ChildViews in case the convertView is not NULL.Thats why when you scroll the childViews have repetative data.

try setting the data like below.

 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

Also do not create a new ExpandableListAdapter in onPostExecute

1.Create a new Adapter in onCreate of you activity. 2.You can modify the data of your ExpandableListAdapter , ie listDataHeader and listDataChild by clearing the list and adding the appropriate data items to them. 3.Then you can call notifyDataSetChange() in your onPostExecute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM