-1

我得到一个字符串 [] 数组并将其存储在一个字符串数组 [变量 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 新手,我不知道如何将数组设置为输出视图。任何答案都会有所帮助。

4

1 回答 1

0

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

于 2013-10-21T09:29:42.633 回答