如何修改這個imageCheckBoxAdapter代碼,以便在滾動時保持checkBox的狀態(即所有選中的複選框在滾動後都應該保持檢查狀態,並且所選的變量也需要存儲在數組中)?android中的自定義複選框難度
class imageCheckBoxAdapter extends ArrayAdapter<String>
{
private final Context context;
private final ArrayList<String> values;
private final Map< String, SmbFile> obj;
static ArrayList<Boolean> checks=new ArrayList<Boolean>();
public imageCheckBoxAdapter(Context context,ArrayList<String> values,Map< String, SmbFile>obj)
{
super(context, R.layout.row_checkbox, values);
this.context = context;
this.values = values;
this.obj=obj;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row_checkbox, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.text1_check);
textView.setText(values.get(position));
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon_image_check);
try
{
if((obj.get(values.get(position)).isFile()))
{
imageView.setImageResource(R.drawable.view_file_icon);
}
else
{
imageView.setImageResource(R.drawable.view_folder_icon);
}
}
catch (SmbException e)
{
Toast.makeText(context,"Network error",Toast.LENGTH_SHORT).show();
Log.d("id1", "error1");
e.printStackTrace();
}
return rowView;
}
}
row_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false" />
<ImageView
android:id="@+id/icon_image_check"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="5px"
android:layout_marginRight="20px"
android:layout_marginTop="5px"
android:src="@drawable/view_file_icon" >
</ImageView>
<TextView
android:id="@+id/text1_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30px"
android:typeface="sans">
</TextView>
</LinearLayout>
嗨,這個代碼中使用'狀態'變量是什麼? @Override public void onClick(View view){ Integer index =(Integer)view.getTag(); 布爾狀態= checks.get(index.intValue()); checks.set(index.intValue(),true); } – raghs 2012-04-23 14:58:37
當我試圖從外面讀'檢查'靜態變量時,它會拋出錯誤..爲什麼? – raghs 2012-04-23 15:05:10
不訪問檢查嘗試有一個getter方法的檢查值列表,我編輯我的代碼上面。 – Joey 2012-04-24 09:54:21