2016-12-06 71 views
-2

我使用複選框和兩行文本創建了自定義列表視圖。我想在點擊列表項時改變複選框狀態(這意味着如果我點擊列表項的任何部分複選框或文本,複選框將改變狀態)。 要做到這一點我已經在custom_list_row xml文件中添加了這個:Listview複選框在向下滾動時變爲未選中狀態

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:descendantFocusability="blocksDescendants" > 

<CheckBox 
    android:id="@+id/checkbox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    android:clickable="false" /> 

,並在Java類我做這些:

listview.setAdapter(adapter); 

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      final CheckBox checkBox; 
      checkBox = (CheckBox)view.findViewById(R.id.checkbox); 


      if(checkBox.isChecked()){ 
       checkBox.setChecked(false); 
      } 
      else { 
       checkBox.setChecked(true); 
      } 
     } 
    }); 

這工作得很好。但唯一的問題是,當我選擇一些項目並向下滾動 - >返回到所選項目 - >所選項目變爲未選中狀態。

我沒做什麼用setOnCheckedChangeListener

出了什麼問題?任何解決這個問題的建議都會有幫助。

感謝

+0

你需要維護布爾數組 –

+0

你的意思是我必須創建一個數組,它將保存所有複選框的狀態? – supertramp

+0

是的,但嘗試使用recyclerview –

回答

0

我建議你需要使用recyclerview

public class CardViewActivity extends AppCompatActivity { 

private Toolbar toolbar; 

private RecyclerView mRecyclerView; 
private RecyclerView.Adapter mAdapter; 
private RecyclerView.LayoutManager mLayoutManager; 

private List<Student> studentList; 

private Button btnSelection; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    btnSelection = (Button) findViewById(R.id.btnShow); 

    studentList = new ArrayList<Student>(); 

    for (int i = 1; i <= 15; i++) { 
    Student st = new Student("Student " + i, "androidstudent" + i 
    + "@gmail.com", false); 

    studentList.add(st); 
    } 

    if (toolbar != null) { 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setTitle("Android Students"); 

    } 

    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); 

    // use this setting to improve performance if you know that changes 
    // in content do not change the layout size of the RecyclerView 
    mRecyclerView.setHasFixedSize(true); 

    // use a linear layout manager 
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 

    // create an Object for Adapter 
    mAdapter = new CardViewDataAdapter(studentList); 

    // set the adapter object to the Recyclerview 
    mRecyclerView.setAdapter(mAdapter); 

    btnSelection.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
    String data = ""; 
    List<Student> stList = ((CardViewDataAdapter) mAdapter) 
     .getStudentist(); 

    for (int i = 0; i < stList.size(); i++) { 
    Student singleStudent = stList.get(i); 
    if (singleStudent.isSelected() == true) { 

     data = data + "\n" + singleStudent.getName().toString(); 
     /* 
     * Toast.makeText(CardViewActivity.this, " " + 
     * singleStudent.getName() + " " + 
     * singleStudent.getEmailId() + " " + 
     * singleStudent.isSelected(), 
     * Toast.LENGTH_SHORT).show(); 
     */ 
    } 

    } 

    Toast.makeText(CardViewActivity.this, 
     "Selected Students: \n" + data, Toast.LENGTH_LONG) 
     .show(); 
    } 
    }); 

} 

} 

更多看到http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html

OR

如果您想使用列表視圖嘗試這種方式

public class CustomAdapter extends BaseAdapter { 
    private final LayoutInflater inflater; 
    private final Context context; 
    private List<Modelclass> listData; 

    public CustomAdapter(Context mainActivity, List<Modelclass> listData) { 
     context = mainActivity; 
     this.listData = listData; 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 
     return listData.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return listData.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 

     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = inflater.inflate(R.layout.list_item_poojaselection, null); 
      holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname); 
      holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck); 
      convertView.setTag(holder); 
     }else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.checks.setOnCheckedChangeListener(null); 
     holder.checks.setFocusable(false); 

     if (listData.get(position).isselected) { 
      holder.checks.setChecked(true); 
     } else { 
      holder.checks.setChecked(false); 
     } 

     holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton cb, boolean b) { 

       if (checkMaxLimit()) { 

        if (listData.get(position).isselected && b) { 
         holder.checks.setChecked(false); 
         listData.get(position).isselected = false; 

        } else { 
         holder.checks.setChecked(false); 
         listData.get(position).isselected = false; 
         Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show(); 
        } 
       } else { 
        if (b) { 
         listData.get(position).isselected = true; 
        } else { 
         listData.get(position).isselected = false; 
        } 
       } 
      } 
     }); 

     holder.tv.setText(listData.get(position).getLISTING_NAME()); 
     return convertView; 
    } 

    public boolean checkMaxLimit() { 
     int countermax = 0; 
     for(Modelclass item : listData){ 
      if(item.isselected){ 
       countermax++; 
      } 
     } 
     return countermax >= 5; 
    } 

    public class ViewHolder { 
     TextView tv; 
     public CheckBox checks; 
    } 
} 
+0

我已經通過維護一個arraylist – supertramp

+0

接受了答案,如果它幫助你解決了這個問題 –

0

請變化而複選框選擇/取消選擇

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      final CheckBox checkBox; 
      checkBox = (CheckBox)view.findViewById(R.id.checkbox); 


      if(checkBox.isChecked()){ 
       checkBox.setChecked(false); 

        // Reset the checkbox in array list/Bean like 

        list.set(); // set new value of list 

        adapter.notifyChange(); 

      } 
      else { 
       checkBox.setChecked(true); 

       // Reset the checkbox in array list/Bean like 

        list.set(); // set new value of list 

        adapter.notifyChange(); 
      } 
     } 
    }); 

這種變化解決我的這個問題。

相關問題