Change getView
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mLayout=new View(mContext); mLayout=(LinearLayout) inflater.inflate(R.layout.list_menu, null);
Initialize the inflatable element in your constructor. Delete this mLayout=new View(mContext) coz, which you mLayout=(LinearLayout) inflater.inflate(R.layout.list_menu, null) layout with mLayout=(LinearLayout) inflater.inflate(R.layout.list_menu, null)
In your constructor
LayoutInflater inflater; public ListAdapter(Context mContext, List<String> Name, List<Drawable> Icon) { this.inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.mContext=mContext; this.mName=Name; this.mIcon=Icon; }
Use the View holder for smooth scrolling and performance.
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
@Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder vh; if(convertView==null){ vh = new ViewHolder(); convertView =(LinearLayout) inflater.inflate(R.layout.list_menu, null); vh.mText=(TextView) convertView.findViewById(R.id.Name); vh.mImage=(ImageView) convertView.findViewById(R.id.Icon); vh.mCheckBox=(CheckBox) convertView.findViewById(R.id.mCheckbox); convertView.setTag(vh); } else { vh = (ViewHolder) convertView.getTag(); } vh.mText.setText(mName.get(position)); vh.mImage.setImageDrawable(mIcon.get(position)); vh.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton check, boolean isChecked) { if(check.isChecked()){ Toast.makeText(mContext, "..."+mName.get(position)+"..."+position, Toast.LENGTH_SHORT).show(); } } }); return convertView; } static class ViewHolder { TextView mText; ImageView mImage; CheckBox mCheckBox; }
source share