Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set an String Array to an Array Adapter in android List view with check box?
    primarykey
    data
    text
    <p>I am getting a string[] Array and storing it in an String array [variable mystrings],Now i want this array to display it in a list view with check box .</p> <p>ec_checkbox_number.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="26dp" android:text="number" /&gt; &lt;TextView android:id="@+id/checkboxtextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/checkBox" android:layout_alignBottom="@+id/checkBox" android:layout_alignParentLeft="true" android:layout_marginLeft="107dp" android:text="TextView" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>ec_number_selection.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/meetingprofilename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="20dp" android:text="Profile Name:" android:textAppearance="?android:attr/textAppearanceMedium" /&gt; &lt;EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/meetingprofilename" android:layout_alignBottom="@+id/meetingprofilename" android:layout_alignParentRight="true" android:layout_toRightOf="@+id/meetingprofilename" android:ems="10" &gt; &lt;requestFocus /&gt; &lt;/EditText&gt; &lt;Button android:id="@+id/cancelnumberbutton" android:layout_width="158dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/editText2" android:text="Cancel" /&gt; &lt;Button android:id="@+id/donenumberbutton" android:layout_width="158dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_toRightOf="@+id/cancelnumberbutton" android:text="Done" /&gt; &lt;ListView android:id="@+id/numberselectionlistView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/cancelnumberbutton" android:layout_alignParentLeft="true" android:layout_below="@+id/editText1" &gt; &lt;/ListView&gt; &lt;/RelativeLayout&gt; </code></pre> <p>CheckBoxData.java</p> <pre><code>public class CheckBoxData { private String[] number; public String[] getNumber() { return number; } public void setNumber(String[] number) { this.number = number; } } </code></pre> <p>MyConferenceNumber.java</p> <pre><code>public class EcConferenceNumber extends Activity{ ListView checkBoxNumberListView; ConferenceAdapter adapter; Button doneBtn,cancelBtn; EditText profileName; MyCustomAdapter dataAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ec_number_selection); adapter = new ConferenceAdapter(this); checkBoxNumberListView = (ListView) findViewById(R.id.numberselectionlistView); doneBtn=(Button) findViewById(R.id.donenumberbutton); cancelBtn=(Button) findViewById(R.id.cancelnumberbutton); Intent intent = getIntent(); String[] myStrings = intent.getStringArrayExtra("strings"); List&lt;String&gt; strings = new ArrayList&lt;String&gt;(Arrays.asList(myStrings)); System.out.println(""+strings); dataAdapter = new MyCustomAdapter(this, R.layout.ec_checkbox_number, strings); checkBoxNumberListView.setAdapter(dataAdapter); checkBoxNumberListView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) { // When clicked, show a toast with the TextView text CheckBoxData country = (CheckBoxData) parent.getItemAtPosition(position); Toast.makeText(getApplicationContext(), "Clicked on Row: " + country.getNumber(), Toast.LENGTH_LONG).show(); } }); } class MyCustomAdapter extends ArrayAdapter&lt;String&gt;{ private List&lt;String&gt; strings; public MyCustomAdapter(Context context, int textViewResourceId, List&lt;String&gt; strings) { super(context, textViewResourceId,strings); this.strings=new ArrayList&lt;String&gt;(); this.strings.addAll(this.strings); // TODO Auto-generated constructor stub } private class ViewHolder { TextView code; CheckBox name; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; Log.v("ConvertView", String.valueOf(position)); if (convertView == null) { LayoutInflater vi = (LayoutInflater)getSystemService( Context.LAYOUT_INFLATER_SERVICE); convertView = vi.inflate(R.layout.ec_checkbox_number, null); holder = new ViewHolder(); holder.code = (TextView) convertView.findViewById(R.id.checkboxtextView); holder.name = (CheckBox) convertView.findViewById(R.id.checkBox); convertView.setTag(holder); holder.name.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { CheckBox cb = (CheckBox) v ; CheckBoxData country = (CheckBoxData) cb.getTag(); Toast.makeText(getApplicationContext(), "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show(); } }); } else { holder = (ViewHolder) convertView.getTag(); } return convertView; } } } </code></pre> <p>The output what i am getting is..</p> <p>For example:If String[] a={002,111,122} i want to display it in a listview with checkbox and i have to get the selected CheckBox text.But my output is displying like this </p> <p>number </p> <p>number </p> <p>number </p> <p>number </p> <p>But i want the output like this ,</p> <p>123</p> <p>112</p> <p>123</p> <p>Since ,I am new to android i did not know how to set the array to output view.Any answers will be helpfull.</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.
    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