2016-05-31 82 views
1

`我剛接觸android並構建一個列表,並且列表包含已安裝的應用程序名稱以及CheckBox以選擇要卸載的應用程序名稱。例如,問題是當列表變得比電話屏幕更長並且滾動被激活時;當我選擇一個複選框時,會在列表底部自動選擇第二個複選框。Android自定義列表視圖中的自動複選框選擇

問題是自動複選框選擇複選框,PLZ幫助我。

public View getView(final int position, View convertView, ViewGroup parent) 
{ 

     LayoutInflater inflater = context.getLayoutInflater(); 

     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.ultimate, null); 
      holder = new ViewHolder(); 


      holder.apkName = (TextView) convertView.findViewById(R.id.appName); 
      holder.apkInstall=(TextView)convertView.findViewById(R.id.appMemory); 
      holder.cb=(CheckBox)convertView.findViewById(R.id.cb); 




      convertView.setTag(holder); 

     } else { 
      holder = (ViewHolder) convertView.getTag(); 


     } 


     PackageInfo packageInfo = (PackageInfo) getItem(position); 
     Drawable appIcon = packageManager.getApplicationIcon(packageInfo.applicationInfo); 
     String appName = packageManager.getApplicationLabel(packageInfo.applicationInfo).toString(); 
     appIcon.setBounds(0, 0, 50, 50); 
     holder.apkName.setCompoundDrawables(appIcon, null, null, null); 
     holder.apkName.setCompoundDrawablePadding(15); 
     holder.apkName.setText(appName); 


     holder.cb.setTag(position); 
+0

請張貼一些代碼 – Nisarg

+0

holder.cb.setChecked(sba.get(位置));你爲什麼這樣做? – Nisarg

回答

1

爲此,您必須將您選中的複選框狀態保存到模型類的ArrayList中。它會反映到您的適配器,然後進入列表視圖。首先製作一個模型類像下面,

public class Model{ 
String name; 
int value; /* 0 -> checkbox disable, 1 -> checkbox enable */ 

Model(String name, int value){ 
this.name = name; 
this.value = value; 
} 
public String getName(){ 
return this.name; 
} 
public int getValue(){ 
return this.value; 
} 

} 

然後到你的適配器使你的代碼看起來像這樣,

public class CustomAdapter extends ArrayAdapter<Model>{ 
Model[] modelItems = null; 
Context context; 
public CustomAdapter(Context context, Model[] resource) { 
super(context,R.layout.row,resource); 
// TODO Auto-generated constructor stub 
this.context = context; 
this.modelItems = resource; 
} 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
// TODO Auto-generated method stub 
LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
convertView = inflater.inflate(R.layout.row, parent, false); 
TextView name = (TextView) convertView.findViewById(R.id.textView1); 
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1); 
name.setText(modelItems[position].getName()); 
if(modelItems[position].getValue() == 1) 
cb.setChecked(true); 
else 
cb.setChecked(false); 
return convertView; 
} 
} 
從活動

現在只要通過你的複選框列表插入適配器象下面這樣:

lv = (ListView) findViewById(R.id.listView1); 
modelItems = new Model[5]; 
modelItems[0] = new Model("pizza", 0); 
modelItems[1] = new Model("burger", 1); 
modelItems[2] = new Model("olives", 1); 
modelItems[3] = new Model("orange", 0); 
modelItems[4] = new Model("tomato", 1); 
CustomAdapter adapter = new CustomAdapter(this, modelItems); 
lv.setAdapter(adapter); 

現在,如果用戶將選中/取消選中任何複選框,只需更改其模型類中的值並通知適配器即可。

0

當您每次需要顯示列表視圖項目時滾動列表視圖時,它將調用getView來創建或獲取佈局。因此,您需要檢查模型類中是否使用布爾變量進行檢查,並根據boolan值將複選框視爲選中狀態。您應該在用戶選中複選框時更新模型的布爾值。

您的模型類應該包含一個變量,表示複選框是否被選中。

public class Model{ 
    ..... 
    boolean isChecked; //true if checkbox is checked 
    ..... 
} 

在您的適配器類中,在getView()中。 您應該使用isChecked變量檢查它是否被選中。

if(isChecked){ 
    holder.cb.setChecked(true); 
} else { 
    holder.cb.setChecked(false); 
} 
相關問題