CheckBox
默认值无效。我已经通过CheckBox
给出了setcheck(true)
值,并且在布局中给出了,我需要将选中的值传递给arraylist。
如何使用arraylist在两个类之间发送值如何在android中的资产db
中保存数据。
public class CustomListAdapter extends ArrayAdapter<String>
{
RetrieveFromDB sqlConn;
private final Context context;
private ArrayList<String> resArray;
public static ArrayList<String> selitemsList=new ArrayList<String>();
public CustomListAdapter(Context context, ArrayList<String> resArray)
{
super(context, R.layout.categorylist, resArray);
// TODO Auto-generated constructor stub
this.context = context;
this.resArray = resArray;
}
public View getView(final int position, View convertView, ViewGroup parent) {
try{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item_2, parent, false);
CheckBox chk=(CheckBox)rowView.findViewById(R.id.checkBox1);
TextView list= (TextView) rowView.findViewById(R.id.text2);
list.setText(resArray.get(position).toString());
sqlConn=new RetrieveFromDB(context);
selitemsList=sqlConn.displaySelectedItems(EditSelectedItems.sel_category);
sqlConn.close();
if(selitemsList.contains(list.getText().toString()))
chk.setChecked(true);
else
chk.setChecked(false);
chk.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// TODO Auto-generated method stub
if(isChecked)
{
CategoryListView.st.add(resArray.get(position).toString());
selitemsList.add(resArray.get(position).toString());
};
答案 0 :(得分:0)
关于.setChecked的代码是对的。尝试记录有关selitemsList内容的内容,您可以检查logcat。例如
Log.d("my debug", "selitemsList content:");
for (String s: selitemsList){
Log.d("my debug", "\t- " + s);
}
if(selitemsList.contains(list.getText().toString())){
Log.d("my debug", "selitemsList contains " + list.getText().toString());
chk.setChecked(true);
}else{
Log.d("my debug", "selitemsList doesn't contain " + list.getText().toString());
chk.setChecked(false);
}
在这种模式下,您应该能够在logcat中看到问题所在。也许selitemsList没有正确填充。
答案 1 :(得分:0)
使用SparseBooleanArray获取所选项目的列表。
int len = listView.getCount();
ArrayList<String> selectedItems = new ArrayList<String>();
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < len; i++) {
if (checked.get(i)) {
String item = cont_list.get(i);
selectedItems.add(item);
}