簡體   English   中英

Android Studio ListView復選框保存

[英]Android Studio ListView Checkbox Save

我已經看了一些關於stackoverflow的關於這個問題的其他問題但是我無法將這個問題實現到我的代碼中,因為我相信我做的事情有點不同。 我還在學習Android Studio,所以請耐心等待任何愚蠢。

我希望在列表視圖的右側創建復選框,當前用戶將加載應用程序時,檢查時將保持該狀態。

XML:

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Which Will Be Next"
    android:textAllCaps="false"
    android:textColor="#f1b75a"
    android:textSize="32sp"
    android:textStyle="bold"  />

<ListView
    android:id="@+id/results_listview"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_below="@+id/textView3"></ListView>
</RelativeLayout>

活動代碼:

public class .... extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_flavour);

    ListView resultListView = (ListView) findViewById(R.id.results_listview);

    HashMap<String, String> nameAddress = new HashMap<>();
    nameAddress.put("exmp ", "Locate");


    List<HashMap<String, String>> listItems = new ArrayList<>();
    SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.list_item, new String[]{"First Address", "Second Address"},new int[]{R.id.tv1, R.id.tv3});

    Iterator it  = nameAddress.entrySet().iterator();
    while (it.hasNext()){
        HashMap<String, String> resultsMap = new HashMap<>();
        Map.Entry pair  = (Map.Entry)it.next();
        resultsMap.put("First Address", pair.getKey().toString());
        resultsMap.put("Second Address", pair.getValue().toString());
        listItems.add(resultsMap);

    }

    resultListView.setAdapter(adapter);


}


}

列出Items.xml

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

<CheckedTextView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#ffa200"
    android:textSize="21sp"
    android:textStyle="bold"
    android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"
    />
<CheckedTextView
    android:id="@+id/tv3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#ffffff"
    android:textSize="16sp"
    android:textStyle="bold"

    />

</LinearLayout>

任何幫助表示贊賞:)

我建議您可以使用Recyclerview而不是listview,試試這種方式

public class CardViewDataAdapter extends
  RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {

 private List<Student> stList;

 public CardViewDataAdapter(List<Student> students) {
  this.stList = students;

 }

 // Create new views
 @Override
 public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
   int viewType) {
  // create a new view
  View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
    R.layout.cardview_row, null);

  // create ViewHolder

  ViewHolder viewHolder = new ViewHolder(itemLayoutView);

  return viewHolder;
 }

 @Override
 public void onBindViewHolder(ViewHolder viewHolder, int position) {

  final int pos = position;

  viewHolder.tvName.setText(stList.get(position).getName());

  viewHolder.tvEmailId.setText(stList.get(position).getEmailId());

  viewHolder.chkSelected.setChecked(stList.get(position).isSelected());

  viewHolder.chkSelected.setTag(stList.get(position));


  viewHolder.chkSelected.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    CheckBox cb = (CheckBox) v;
    Student contact = (Student) cb.getTag();

    contact.setSelected(cb.isChecked());
    stList.get(pos).setSelected(cb.isChecked());

    Toast.makeText(
      v.getContext(),
      "Clicked on Checkbox: " + cb.getText() + " is "
        + cb.isChecked(), Toast.LENGTH_LONG).show();
   }
  });

 }

 // Return the size arraylist
 @Override
 public int getItemCount() {
  return stList.size();
 }

 public static class ViewHolder extends RecyclerView.ViewHolder {

  public TextView tvName;
  public TextView tvEmailId;

  public CheckBox chkSelected;

  public Student singlestudent;

  public ViewHolder(View itemLayoutView) {
   super(itemLayoutView);

   tvName = (TextView) itemLayoutView.findViewById(R.id.tvName);

   tvEmailId = (TextView) itemLayoutView.findViewById(R.id.tvEmailId);
   chkSelected = (CheckBox) itemLayoutView
     .findViewById(R.id.chkSelected);

  }

 }

 // method to access in activity after updating selection
 public List<Student> getStudentist() {
  return stList;
 }

}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM