Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>Try this.</em></p> <pre><code>public class MainActivity extends Activity implements OnClickListener { public class LVSample3Adapter extends BaseAdapter{ private Context context; private List&lt;LVSample3Item&gt; itemList; public LVSample3Adapter(List&lt;LVSample3Item&gt; lstItems, MainActivity mainActivity) { // TODO Auto-generated constructor stub this.context=mainActivity; this.itemList=lstItems; } @Override public int getCount() { // TODO Auto-generated method stub return itemList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return itemList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Log.e("lstItems1:", String.valueOf(position)); LVSample3Item item = itemList.get(position); convertView =LayoutInflater.from(context).inflate(R.layout.list, parent, false); TextView t1=(TextView)convertView.findViewById(R.id.textView1); t1.setText(item.getTitle()); CheckBox chb1=(CheckBox)convertView.findViewById(R.id.checkBox1); chb1.setChecked(item.getstate()); return convertView; } } /** Called when the activity is first created. */ private ListView lv; private ListAdapter adapter; private Button btn1,btn2; private List&lt;LVSample3Item&gt; lstItems; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn1=(Button)findViewById(R.id.button1); btn2=(Button)findViewById(R.id.button2); btn1.setOnClickListener(this); btn2.setOnClickListener(this); lstItems = new ArrayList&lt;LVSample3Item&gt;(); LVSample3Item item = new LVSample3Item("drinks",false); lstItems.add(item); item = new LVSample3Item("chat",false); lstItems.add(item); item = new LVSample3Item("chat1",true); lstItems.add(item); item = new LVSample3Item("chat2",false); lstItems.add(item); adapter = new LVSample3Adapter(lstItems, this); lv=(ListView)findViewById(R.id.listView1); lv.setAdapter(adapter); } @Override public void onClick(View v) { // TODO Auto-generated method stub if(v.getId()==R.id.button1){ Log.e("lstItems:", String.valueOf(lstItems.size())); for(int i=0;i&lt;lstItems.size();i++){ LVSample3Item item=lstItems.get(i); if(!item.getstate()){ item.setpath(true); } } ((BaseAdapter) adapter).notifyDataSetChanged(); }else if(v.getId()==R.id.button2){ for(int i=0;i&lt;lstItems.size();i++){ LVSample3Item item=lstItems.get(i); if(item.getstate()){ item.setpath(false); } } ((BaseAdapter) adapter).notifyDataSetChanged(); } } </code></pre> <p>}</p> <pre><code>public class LVSample3Item implements Serializable { private String title; private boolean state; public LVSample3Item(String title,boolean imagepath) { this.title = title; this.state=imagepath; } public String getTitle() { return title; } public boolean getstate() { return state; } public void setTitle(String title) { this.title = title; } public void setpath(boolean imagepath) { this.state = imagepath; } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload