List item repeating in android customized listview

前端 未结 4 1579
不知归路
不知归路 2020-12-03 12:38

In my customized list view items are repeating.position of item is same for all item. code is below

ListAdapter.java-

    public class ListAdapter ex         


        
4条回答
  •  渐次进展
    2020-12-03 13:18

    // try this
     public class ListAdapter extends BaseAdapter {
    
            private List mName;
            private List mIcon;
            private Context mContext;
    
            public ListAdapter(Context mContext, List Name, List Icon) {
                this.mContext=mContext;
                this.mName=Name;
                this.mIcon=Icon;
            }
    
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return mName.size();
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public View getView(final int position, View v, ViewGroup parent) {
    
                ViewHolder holder;
    
                if(v==null){
                    holder = new ViewHolder();
                    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v =(LinearLayout) inflater.inflate(R.layout.list_menu, null);
    
                    holder.mText=(TextView) v.findViewById(R.id.Name);
                    holder.mImage=(ImageView) v.findViewById(R.id.Icon);
                    holder.mCheckBox=(CheckBox) v.findViewById(R.id.mCheckbox);
    
                  v.setTag(holder);
                }
                else{
                   holder = (ViewHolder) v.getTag();
                }
                holder.mText.setText(mName.get(position));
                holder.mImage.setImageDrawable(mIcon.get(position));
    
                holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                    @Override
                    public void onCheckedChanged(CompoundButton check, boolean isChecked) {
                        if(check.isChecked()){
                            Toast.makeText(mContext, "..."+mName.get(position)+"..."+position, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                v.setTag(holder);
                return v;
            }
    
             class ViewHolder{
                TextView mText;
                ImageView mImage;
                CheckBox mCheckBox;
            }
    
        }
    

提交回复
热议问题