这是我的适配器类:
public class CheckboxAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
Context context;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
ArrayList<String> selectedStrings = new ArrayList<String>();
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);
final CheckBox tv = (CheckBox) convertView.findViewById(R.id.checkBox1);
tv.setText(mStrings[position]);
tv.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
selectedStrings.add(tv.getText().toString());
Toast.makeText(buttonView.getContext(),Boolean.toString(selectedStrings.add(tv.getText().toString())), Toast.LENGTH_SHORT).show();
} else {
selectedStrings.remove(tv.getText().toString());
}
}
});
return convertView;
}
}
这是我的行为
public class AddTime extends Activity {
CheckboxAdapter cadpter;
String daya[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thrusday",
"Friday", "Saturday" };
ListView list;
ArrayList<String> selectedStrings = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addtime);
list = (ListView) findViewById(R.id.listView1);
cadpter = new CheckboxAdapter(AddTime.this, R.layout.list_item, daya);
list.setAdapter(cadpter);
}
}
我在listview中全天显示我也有显示复选框,每天当我启用复选框时它在吐司中显示为true但我想要我选择日使用复选框我的意思是我希望在textview中全天显示在主要方面,请建议我如何得到它。
答案 0 :(得分:2)
如何从android
中的list-view点击按钮获取所有选中的值
在selectedStrings
ArrayList中添加所有选中的值。在CheckboxAdapter
类中创建一个方法,该方法返回所有选定值的ArrayList。例如:
public ArrayList<String> getAllSelectedValues(){
return this.selectedStrings;
}
现在使用cadpter
CheckboxAdapter
类对象访问Activity中的getAllSelectedValues
方法:
ArrayList<String> selectedStrings=cadpter.getAllSelectedValues();
答案 1 :(得分:0)
从selectedStrings arrayList获取所有值
for(int i=0;i<=selectedStrings.sizes();i++){
String s =selectedStrings.get(i);
}
答案 2 :(得分:0)
取布尔数组
ArrayList<Boolean> selectedStrings = new ArrayList<>();
并在复选框中添加onClickListener
public View getView(final int position, View convertView, ViewGroup parent) {
-
-
tv.setChecked(false);
if(selectedStrings.size() <= position){
selectedStrings.add(position, false);
}else
tv.setChecked(selectedStrings.get(position));
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean state = selectedStrings.get(position);
selectedStrings.remove(position);
selectedStrings.add(position, state ? false : true);
}
});
}