I created one sample example for a checklist using the Array adapter.
ListView with CheckBox using ArrayAdatpter in Android
You can also find sample code.
It will be displayed
Here is the code as I do
I have an Activity List List
public class MainActivity extends Activity { private ListView mainListView = null; private Planet[] planets = null; private ArrayAdapter<Planet> listAdapter = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mainListView = (ListView) findViewById(R.id.mainListView); // When item is tapped, toggle checked properties of CheckBox and // Planet. mainListView .setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View item, int position, long id) { Planet planet = listAdapter.getItem(position); planet.toggleChecked(); PlanetViewHolder viewHolder = (PlanetViewHolder) item .getTag(); viewHolder.getCheckBox().setChecked(planet.isChecked()); } }); // Create and populate planets. planets = (Planet[]) getLastNonConfigurationInstance(); if (planets == null) { planets = new Planet[] { new Planet("Mercury"), new Planet("Venus"), new Planet("Earth"), new Planet("Mars"), new Planet("Jupiter"), new Planet("Saturn"), new Planet("Uranus"), new Planet("Neptune"), new Planet("Ceres"), new Planet("Pluto"), new Planet("Haumea"), new Planet("Makemake"), new Planet("Eris") }; } ArrayList<Planet> planetList = new ArrayList<Planet>(); planetList.addAll(Arrays.asList(planets)); // Set our custom array adapter as the ListView adapter. listAdapter = new PlanetArrayAdapter(this, planetList); mainListView.setAdapter(listAdapter); } public Object onRetainNonConfigurationInstance() { return planets; } }
And here is the custom array adapter where I process the checkbox.
public class PlanetArrayAdapter extends ArrayAdapter<Planet> { private LayoutInflater inflater; public PlanetArrayAdapter(Context context, List<Planet> planetList) { super(context, R.layout.simplerow, R.id.rowTextView, planetList);
You are welcome! let me know if you have a problem with this.
source share