如何在android列表视图中使用复选框将字符串数组设置为数组适配器?

时间:2013-10-21 09:18:33

标签: java android arrays android-arrayadapter

我得到一个字符串[]数组并将其存储在一个String数组[变量mystrings]中,现在我希望这个数组在列表视图中显示它并带有复选框。

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;

          }

         }

    }

我得到的输出是......

例如:如果String [] a = {002,111,122}我想在带有复选框的列表视图中显示它,我必须得到所选的CheckBox文本。但是我的输出是这样的

号码

号码

号码

号码

但我希望输出像这样,

123

112

123

因为,我是android的新手,我不知道如何将数组设置为输出视图。任何答案都会有所帮助。

1 个答案:

答案 0 :(得分:0)

粘贴完整的xml代码。 xml中没有按钮,缺少一些属性。