基本上,我正在创建一个任务/待办事项列表应用程序,但我无法弄清楚这一部分。我想在每个任务旁边添加一个复选框,并能够检查它们。
这是MainActivity.java
public class MainActivity extends AppCompatActivity {
static ArrayList<String> notes = new ArrayList<>();
static ArrayAdapter arrayAdapter;
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (item.getItemId() == R.id.add_note) {
Intent intent = new Intent(getApplicationContext(),
note_editor.class);
startActivity(intent);
return true;
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
SharedPreferences sharedPreferences =
getApplicationContext().getSharedPreferences
("com.example.assignment1", Context.MODE_PRIVATE);
HashSet<String> set = (HashSet<String>)
sharedPreferences.getStringSet("notes", null);
if (set == null) {
notes.add("Add Your Task Hereee");
} else {
notes = new ArrayList(set);
}
arrayAdapter = new ArrayAdapter
(this, android.R.layout.simple_list_item_1, notes);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick
(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(),
note_editor.class);
intent.putExtra("noteId", i);
startActivity(intent);
}
});
我的main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:id="@+id/listView" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="32dp"
android:layout_marginBottom="28dp"
android:backgroundTint="#4BB3A9"
android:src="@drawable/add_task"/>
</RelativeLayout>
我希望它看起来像这样“:I drew the checkboxes here
我还有另一个类,用于编辑任务以及启动画面,但我认为在这里没有必要这样做。任何帮助都将不胜感激!
发布于 2021-03-10 13:16:42
你可以使用回收器视图代替列表视图。参考代码如下:
创建自定义适配器并将其设置为您的回收器视图,并像recyclerView.setAdapter(CustomAdapter(new ArrayList("AAAAA","BBBBB","CCCCC","DDDDD"),getContext()));
一样调用它
自定义适配器
public class CustomAdapter extends ArrayAdapter<String> {
private ArrayList<String> dataSet;
// View lookup cache
private static class ViewHolder {
CheckedTextView checkedTextView;
}
public CustomAdapter(ArrayList<String> data, Context context) {
super(context, R.layout.list_item, data);
this.dataSet = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_item, parent, false);
viewHolder.checkedTextView = convertView.findViewById(R.id.check_box);
viewHolder.checkedTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewHolder.checkedTextView.setChecked(!viewHolder.checkedTextView.isChecked());
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkedTextView.setText(dataSet.get(position));
return convertView;
}
}
每个列表项的XML代码: list_item.xml
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/check_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:checked="false"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Check Me"
/>
发布于 2021-03-10 12:28:52
您正在为您的ListView使用安卓默认布局,而此默认布局仅显示TextView,您必须为您的列表项视图和自定义ArrayAdapter创建自定义布局才能实现您想要的效果。
欲了解更多信息,请访问以下网址:https://javapapers.com/android/android-listview-custom-layout-tutorial/
作为RecyclerView的提示开关,哪个更有效:https://developer.android.com/guide/topics/ui/layout/recyclerview
https://stackoverflow.com/questions/66564665
复制相似问题