gpt4 book ai didi

java - Android Studio ListView 复选框保存

转载 作者:太空宇宙 更新时间:2023-11-04 11:27:41 24 4
gpt4 key购买 nike

我已经在 stackoverflow 上查看了有关此问题的其他一些问题,但是我无法将其实现到我的代码中,因为我认为我所做的事情有点不同。我仍在学习 Android Studio,所以请容忍我的任何愚蠢行为。

我希望在当前 ListView 的右侧创建复选框,当用户重新加载应用程序时,选中该复选框将保持该状态。

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>

Activity 代码:

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

关于java - Android Studio ListView 复选框保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44171157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com