Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 .

ec_checkbox_number.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <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" />

    <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" />

</RelativeLayout>

ec_number_selection.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <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" />

    <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" >

        <requestFocus />
    </EditText>

    <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" />

    <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" />

    <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" >

    </ListView>

</RelativeLayout>

CheckBoxData.java

public class CheckBoxData {

    private String[] number;

    public String[] getNumber() {
        return number;
    }

    public void setNumber(String[] number) {
        this.number = number;
    }



}

MyConferenceNumber.java

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<String> strings = 
                 new ArrayList<String>(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<String>{

        private List<String> strings;

        public MyCustomAdapter(Context context, int textViewResourceId, List<String> strings) {
            super(context, textViewResourceId,strings);
            this.strings=new ArrayList<String>();
            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;

          }

         }

    }

The output what i am getting is..

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

number

number

number

number

But i want the output like this ,

123

112

123

Since ,I am new to android i did not know how to set the array to output view.Any answers will be helpfull.

share|improve this question
add comment

1 Answer

Paste your full xml code. There is no button in xml and some properties are missing.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.