Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding checked items in a custom list view to array list in android
    primarykey
    data
    text
    <p>Hi i am writing a program using custom listview with checkbox. My intention is to start a new activity when clicking a button (<strong>Add</strong>) and the new activity displays the selected values from the listview.I am getting error when i try to add the values</p> <p>the error occuring line is</p> <blockquote> <p>selectedItems.add(adapter.getItem(position));</p> </blockquote> <p>if i comment this line then i can check the box but it shows eror when i click <strong>"Add"</strong> button.</p> <p>here is my complete code.</p> <p>My mainactivity</p> <p><strong>HomePage.java</strong></p> <pre><code>public class HomePage extends Activity { private ListView listView1; ListAdapter adapter; Button btn; SparseBooleanArray checkedValue; ArrayList&lt;List&gt; selectedItems; String name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home_page); List device_data[] = new List[] { new List(R.drawable.ic_pic , "Actvity1"), new List(R.drawable.ic_pic, "Actvity2"), new List(R.drawable.ic_pic, "Actvity3"), new List(R.drawable.ic_pic, "Actvity4"), new List(R.drawable.ic_pic, "Actvity5") }; ListAdapter adapter = new ListAdapter(this, R.layout.list_viewrow, device_data); listView1 = (ListView)findViewById(R.id.listView1); View header = (View)getLayoutInflater().inflate(R.layout.listview_header, null); listView1.addHeaderView(header); listView1.setAdapter(adapter); Log.i("check box status", ""+ListAdapter.ListHolder.class); btn= (Button)findViewById(R.id.add); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String[] outputStrArr = new String[selectedItems.size()]; checkedValue= listView1.getCheckedItemPositions(); for (int i = 0; i &lt; selectedItems.size(); i++) { outputStrArr[i] = selectedItems.get(i).toString(); } Intent intent = new Intent(getApplicationContext(), CheckedValues.class); Bundle b = new Bundle(); b.putStringArray("selectedItems", outputStrArr); // Add the bundle to the intent. intent.putExtras(b); startActivity(intent); } }); } private class ListAdapter extends ArrayAdapter&lt;List&gt; { Context context; int layoutResourceId; boolean checkvalue; List data[] = null; public ListAdapter(Context context, int layoutResourceId, List[] data) { super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View row = convertView; final ListHolder holder; if(row == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new ListHolder(); holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon); holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle); holder.checkbox=(CheckBox) row.findViewById(R.id.check); holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { selectedItems.add(adapter.getItem(position)); } }); row.setTag(holder); } else { holder = (ListHolder)row.getTag(); } List list = data[position]; holder.txtTitle.setText(list.title); holder.imgIcon.setImageResource(list.icon); return row; } class ListHolder { ImageView imgIcon; TextView txtTitle; CheckBox checkbox; } } } </code></pre> <p>and my second activity</p> <p><strong>CheckedValues.java</strong></p> <pre><code>public class CheckedValues extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_checked_values); Bundle b = getIntent().getExtras(); String[] resultArr = b.getStringArray("selectedItems"); ListView lv = (ListView) findViewById(R.id.outputList); ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, resultArr); lv.setAdapter(adapter); } </code></pre> <p>}</p> <p>thanks in advance..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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