2013-04-27 80 views
2

我正在實現listview的每個Iten listview的複選框。我得到的問題是當我點擊任何單個複選框,當我滾動然後一些其他未點擊的複選框也隨機點擊。我經歷了很多鏈接,但它並沒有解決我的問題。在列表視圖中的複選框不能正常工作

public class CustomListViewAdapter extends ArrayAdapter<Client> { 

ViewHolder holder = null; 
private LayoutInflater inflator = null; 
private ArrayList<Client> orderList = null; 
ArrayList<View> allViews; 

public CustomListViewAdapter(Context context, int resource, 
     List<Client> objects) { 
    super(context, resource, objects); 

    orderList = (ArrayList<Client>) objects; 
    inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    allViews = new ArrayList<View>(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(convertView == null) { 
     convertView = inflator.inflate(R.layout.listview_add_order, null); 
     holder = new ViewHolder(); 
     convertView.setTag(holder); 
     holder.txtViewName = (TextView) convertView.findViewById(R.id.txtViewAddOrder); 
     holder.spinnerAddorder = (Spinner) convertView.findViewById(R.id.spinnerAddOrder); 
     holder.checkAddorder = (CheckBox) convertView.findViewById(R.id.checkAddOrder); 
     holder.checkAddorder.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 


     holder.checkAddorder.setTag(holder); 
    } 

    allViews.add(position, holder.spinnerAddorder); 

    holder.checkAddorder.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      CheckBox cchk = (CheckBox)view; 
      ViewHolder checkPosition = (ViewHolder) view.getTag(); 


      if(cchk.isChecked()) { 

       checkPosition.spinnerAddorder.setVisibility(View.VISIBLE); 

      } else { 
       checkPosition.spinnerAddorder.setVisibility(View.INVISIBLE); 
      } 
     } 
    }); 

    List<String> list = new ArrayList<String>(); 
    list.add("Select Quantity"); 
    list.add("1"); 
    list.add("2"); 
    list.add("3"); 
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.getContext(),R.layout.spinner_add_order_style, list); 
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item); 
    holder.spinnerAddorder.setAdapter(dataAdapter); 

    if(holder.checkAddorder.isChecked()) { 


     ViewHolder checkPosition = (ViewHolder) holder.checkAddorder.getTag(); 
     checkPosition.spinnerAddorder.setVisibility(View.VISIBLE); 
    } else { 

     ViewHolder checkPosition = (ViewHolder) holder.checkAddorder.getTag(); 
     checkPosition.spinnerAddorder.setVisibility(View.INVISIBLE); 
    } 


    Client order = orderList.get(position); 

    holder.txtViewName.setText(order.getFirstName()); 
    return convertView; 
} 

private static class ViewHolder { 
    public TextView txtViewName = null; 
    public Spinner spinnerAddorder = null; 
    public CheckBox checkAddorder = null; 
} 

    } 

我的XML文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:gravity="center_vertical" 
android:orientation="horizontal" 
android:paddingLeft="5dp" 
android:paddingRight="5dp" 
android:weightSum="2" > 

<TextView 
    android:id="@+id/txtViewAddOrder" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1.2" 
    android:text="TextView" 
    android:textSize="25sp" /> 

<Spinner 
    android:id="@+id/spinnerAddOrder" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight=".8" 
    android:background="@drawable/dropdown" /> 

<CheckBox 
    android:id="@+id/checkAddOrder" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="20dp" /> 

</LinearLayout> 
+0

你能告訴我你的這個適配器類的xml文件嗎? – ckpatel 2013-04-27 06:14:58

+0

@ChiragPatel此問題將發生,因爲listview回收它的意見什麼Koded101發佈是完全正確的..沒有需要的XML文件 – Pragnani 2013-04-27 06:39:00

+0

@chirag我添加了xml文件 – Avinash 2013-04-27 06:41:29

回答

3

這是因爲鑑於回收的發生。你需要做的是維護一個布爾數組,並且每當選中一個複選框時切換相應的布爾值。然後在你的getview中檢查相應的布爾數組位置並設置複選框的狀態。

沿着這些路線:我修改了它,使其正確顯示。沒有檢查你的代碼的其餘部分。^_^

public class CustomListViewAdapter extends ArrayAdapter<Client> { 

ViewHolder holder = null; 
private LayoutInflater inflator = null; 
private ArrayList<Client> orderList = null; 
ArrayList<View> allViews; 

boolean[] checkedStates; //********** NEW ******** 

public CustomListViewAdapter(Context context, int resource, 
     List<Client> objects) { 
    super(context, resource, objects); 

    orderList = (ArrayList<Client>) objects; 
    inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    allViews = new ArrayList<View>(); 

    checkedStates = new boolean[objects.size()]; //********** NEW ******** 
} 

@Override //********** NEW ******** position argument is now final 
public View getView(final int position, View convertView, ViewGroup parent) { 
    if(convertView == null) { 
     convertView = inflator.inflate(R.layout.listview_add_order, null); 
     holder = new ViewHolder(); 
     convertView.setTag(holder); 
     holder.txtViewName = (TextView) convertView.findViewById(R.id.txtViewAddOrder); 
     holder.spinnerAddorder = (Spinner) convertView.findViewById(R.id.spinnerAddOrder); 
     holder.checkAddorder = (CheckBox) convertView.findViewById(R.id.checkAddOrder); 
     holder.checkAddorder.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 


     holder.checkAddorder.setTag(holder); 
    } 

    allViews.add(position, holder.spinnerAddorder); 

    holder.checkAddorder.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View view) { 

       checkedStates[position] = !checkedStates[position]; //********** NEW ******** 
     } 
    }); 

    holder.chechAddorder.setChecked(checkedStates[position]); //********** NEW ******** 

    List<String> list = new ArrayList<String>(); 
    list.add("Select Quantity"); 
    list.add("1"); 
    list.add("2"); 
    list.add("3"); 
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.getContext(),R.layout.spinner_add_order_style, list); 
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item); 
    holder.spinnerAddorder.setAdapter(dataAdapter); 

    if(checkedStates[position]) { //********** NEWEST ******** assuming true is checked and false is unchecked 


     ViewHolder checkPosition = (ViewHolder) holder.checkAddorder.getTag(); 
     checkPosition.spinnerAddorder.setVisibility(View.VISIBLE); 
    } else { 

     ViewHolder checkPosition = (ViewHolder) holder.checkAddorder.getTag(); 
     checkPosition.spinnerAddorder.setVisibility(View.INVISIBLE); 
    } 


    Client order = orderList.get(position); 

    holder.txtViewName.setText(order.getFirstName()); 
    return convertView; 
} 

private static class ViewHolder { 
    public TextView txtViewName = null; 
    public Spinner spinnerAddorder = null; 
    public CheckBox checkAddorder = null; 
} 

    } 
+0

你發佈的是完全正確的,但他提供了一些代碼 – Pragnani 2013-04-27 06:35:00

+0

@Pragnani ,Koded101-你能不能更新我的代碼 – Avinash 2013-04-27 06:42:18

+0

是的,它的工作很好,但它滾動列表視圖後更新選中或未選中狀態 – Avinash 2013-04-27 06:48:15

0

只需設置focusable屬性= false。 在適配器中的CheckBox上生成onClickListener。

{ <CheckBox 
    android:id="@+id/cb_alert" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:layout_marginRight="5dip" 
    android:button="@drawable/custom_checkbox" 
    android:checked="true" 
    android:focusable="false" 
    android:padding="7dip" />}