我面臨着一個問題,它是關於在列表視圖中檢查檢查項目時我sc the着列表,我不知道該怎麼辦。帶有複選框的Android ListView在檢查時鬆動檢查的項目
這裏是我的自定義接口的代碼:
public class SPCKontrola extends Activity {
ArrayList<Integer> yourSelectedItems= new ArrayList<Integer>();
@Override
public void onResume() {
super.onResume();
yourSelectedItems.clear();
}
}
public class SPCMjereAdapter extends BaseAdapter
{
private Context context;
public SPCMjereAdapter(Context c)
{
context = c;
}
public int getCount() {
return MyArrList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_list_row, null);
}
TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis);
txtOpis.setText(MyArrList.get(position).get("OpisMjere"));
TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));
CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
Chk.setTag(MyArrList.get(position).get("RbMjere"));
Chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
yourSelectedItems.add(position);
}else{
if(yourSelectedItems.contains(position)){
yourSelectedItems.remove((position));
}
}
}
});
return convertView;
}
}
我應該改變或添加代碼?我找到了一些例子,但我不能重複使用它們並在我的代碼中實現:/
在此先感謝。
https://groups.google.com/forum/#!topic/android-developers/No0LrgJ6q2M。查看Romain Guy的評論。 – Raghunandan
hm ...似乎是一個很好的解決方案,但它擴展了ListActivity。我試圖避免這一點,因爲它會影響幾乎完成的應用程序中的一些事情。 – Josef