10

您好,我创建了一个带有复选框的列表视图...但我不知道如何获取选中的复选框文本.. 这是 activity_main.xml 的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="top"
    android:orientation="vertical"
    tools:context=".MygamesActivity" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="20dp" />

</LinearLayout>

另一个布局,其中包含要在列表视图 main.list_item.xml 中显示的复选框

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox"
         />
</LinearLayout>

这是扩展 arrayadapter 的类

package com.wasiff.listview;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;


public class CheckboxAdapter extends ArrayAdapter<String> {
    private LayoutInflater mInflater;

    private String[] mStrings;
    private TypedArray mIcons;
    private int mViewResourceId;

    public CheckboxAdapter(Context ctx,int viewResourceId,String[] strings){
        super(ctx,viewResourceId,strings);

        mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mStrings = strings;

        mViewResourceId = viewResourceId;
    }

    public int getCount(){
        return mStrings.length;
    }

    public String getItem(int position){
        return mStrings[position];
    }

    public long getItemId(int position){
        return 0;
    }

    public View getView(int position,View convertView,ViewGroup parent){
        convertView = mInflater.inflate(mViewResourceId, null);

        CheckBox tv = (CheckBox)convertView.findViewById(R.id.checkBox1);
        tv.setText(mStrings[position]);

        return convertView;
    }
}

这是我的 mainActivity 类

package com.wasiff.listview;

import android.app.ListActivity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Context ctx = getApplicationContext();
        Resources res = ctx.getResources();

        String[] options = res.getStringArray(R.array.countrynames);

        setListAdapter((ListAdapter) new CheckboxAdapter(ctx,R.layout.main_list_item,options));

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

最后,我将所有国家/地区保存在 values 文件夹中的 countries.xml 文件中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="countrynames" translatable="false">
        <item>Bhutan</item>
        <item>Colombia</item>
        <item>India</item>
        <item>Pakistan</item>
        <item>Australia</item>
        <item>Srilanka</item>
        <item>England</item>
    </string-array>
</resources>

它显示了listView中的复选框现在我想要的是获取复选框的文本,这些复选框被选中并在按钮单击时显示在吐司中(测试)我遵循了oreilly关于android cookbook的教程,但我仍然不知道如何设置监听器

4

5 回答 5

25

在 CheckboxAdapter.java 中添加

ArrayList<String> selectedStrings = new ArrayList<String>();

然后在getView方法里面

tv.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    selectedStrings.add(tv.getText().toString());
                }else{
                    selectedStrings.remove(tv.getText().toString());
                }

            }
        });

编写一个返回 selectedStrings 的 getter

ArrayList<String> getSelectedString(){
  return selectedStrings;
}
于 2013-09-26T12:42:40.493 回答
18

可能这会帮助你:

CheckBox cb;
    ListView mainListView = getListView();
    for (int x = 0; x<mainListView.getChildCount();x++){
        cb = (CheckBox)mainListView.getChildAt(x).findViewById(R.id.myCheckBox);
        if(cb.isChecked()){
            doSomething();
        }
    }
于 2013-09-26T12:21:38.880 回答
1

方法可能是将自己的对象传递给您的 Arrayadapter:

class ArrayItem{
    private String text;
    private boolean checked;
    ... (getter/setter)
}

只需从 Arrayadapter 取回使用过的 Array 并读取它。

Arrayadaper...{

    public ArrayList<ArrayItem> getList(){ 
        return this.arrayList;
    }

    public View getView(int position,View convertView,ViewGroup parent){
        ArrayItem item = this.arrayList.get(position)
        convertView = mInflater.inflate(mViewResourceId, null);

        CheckBox tv = (CheckBox)convertView.findViewById(R.id.checkBox1);
        tv.setCheckChangeListener... //item.setChecked(true:false)
        tv.setText(mStrings[position]);

        return convertView;
    }
}


}

您可以遍历列表并仅操作选中 == true 的项目

希望这可以帮助。

于 2013-09-26T12:19:01.220 回答
1
SparseBooleanArray checked = listView1.getCheckedItemPositions();
                ArrayList<String> selectedItems = new ArrayList<String>();

                for (int i = 0; i < len; i++) {
                    if (checked.get(i))
                        selectedItems.add((String) adapter.getItem(i));
                }
                String[] outputStrArr = new String[selectedItems.size()];

                for (int i = 0; i < selectedItems.size(); i++) {
                    outputStrArr[i] = selectedItems.get(i);
                }
于 2016-12-27T07:04:11.883 回答
0
ListView listView = findViewById(R.id.listView);

listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);

arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_checked,userlist);
listView.setAdapter(arrayAdapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        CheckedTextView checkedTextView= (CheckedTextView)view;
        if(checkedTextView.isChecked()){
            Log.i("info","CHECKED");
        }
        else{
            Log.i("info","UNCHECKED");
        }
    }
});
于 2018-06-27T19:58:23.713 回答