Android:如何在列表视图中自动显示带有复选框的 Sqlite 数据库
Posted
技术标签:
【中文标题】Android:如何在列表视图中自动显示带有复选框的 Sqlite 数据库【英文标题】:Android : How to display a Sqlite Database With CheckBox in listview Automatically 【发布时间】:2021-01-05 13:16:50 【问题描述】:在我的应用程序中,来自 Diaglogbox 的用户输入数据已存储到 Sqlite 中,并希望在列表视图中与复选框一起显示这些数据
例子:
我在 sqlite 中存储了用户输入,但我不知道如何在列表视图中显示这些保存的名称以及复选框
如何实现我是 android 新手!
数据库:
//在Listview中与CheckBox一起显示这些数据
// Table 2
private static final String TABLE2_NAME = "listitem_name";
public static final String COLUMN1_ID = "I_ID";
public static final String COLUMN2_TITLE = "LISTITEMS_NAME";
onCreate()
String query1 =
"CREATE TABLE IF NOT EXISTS " + TABLE2_NAME + "("
+ COLUMN1_ID + " INTEGER PRIMARY KEY ,"
+ COLUMN2_TITLE + " TEXT ,"
+ COLUMN_ID + " INTEGER, " + "FOREIGN KEY("+
COLUMN_ID +") "
+ "REFERENCES " + TABLE_NAME +"("+COLUMN_ID +")"+ ");";
sqLiteDatabase.execSQL(query1);
Cursor readlistAllData()
String query = "SELECT * FROM " + TABLE2_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if (db != null)
cursor = db.rawQuery(query, null);
return cursor;
活动类
public class AddItems extends AppCompatActivity
Toolbar mToolbar;
DatabaseHelper myDB;
ArrayList<String> listitems;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_items);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_button);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
ShowPopup();
);
myDB = new DatabaseHelper(AddItems.this);
listitems = new ArrayList<>();
DisplayList();
private void DisplayList()
Cursor cursor = myDB.readlistAllData();
if (cursor.getCount() == 0)
Toast.makeText(this, "No Data.", Toast.LENGTH_SHORT).show();
else
while (cursor.moveToNext())
listitems.add(cursor.getString(1));
private void ShowPopup()
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
final EditText lname = dialog.findViewById(R.id.list_Edit_txt);
Button add = dialog.findViewById(R.id.add);
Button cancel = dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
dialog.dismiss();
);
add.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
// Toast.makeText(AddItems.this, "add called", Toast.LENGTH_SHORT).show();
String name = lname.getText().toString();
if (!TextUtils.isEmpty(lname.getText().toString()))
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
db.itemlist(name);
Toast.makeText(AddItems.this, "Added Sucessfully !", Toast.LENGTH_SHORT).show();
ShowPopup();
else
Toast.makeText(AddItems.this, "The name cannot be empty!", Toast.LENGTH_LONG).show();
);
【问题讨论】:
【参考方案1】:您可以使用ListAdapter 类来完成这项工作。 此外,您可以将 RecyclerView 与 RecyclerView 适配器一起使用。 使用 RecyclerView:
joshskeen.com 在http://joshskeen.com/building-a-radiogroup-recyclerview/ 中提供了使用 RecyclerView 的分步方法
以下是适合您场景的改编代码。
RadioAdapter.java
public class RadioAdapter extends RecyclerView.Adapter<RadioAdapter.ViewHolder>
public int mSelectedItem = -1;
public List<String> mItems;
private Context mContext;
public RadioAdapter(Context context, List<String> items)
mContext = context;
mItems = items;
@Override
public void onBindViewHolder(RadioAdapter.ViewHolder viewHolder, final int i)
viewHolder.mRadio.setChecked(i == mSelectedItem);
viewHolder.mText.setText(mItems.get(i));
@Override
public int getItemCount()
return mItems.size();
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i)
LayoutInflater inflater = LayoutInflater.from(mContext);
final View view = inflater.inflate(R.layout.list_view_item, viewGroup, false);
return new ViewHolder(view);
class ViewHolder extends RecyclerView.ViewHolder
public RadioButton mRadio;
public TextView mText;
public ViewHolder(final View inflate)
super(inflate);
mText = (TextView) inflate.findViewById(R.id.text);
mRadio = (RadioButton) inflate.findViewById(R.id.radio);
View.OnClickListener clickListener = new View.OnClickListener()
@Override
public void onClick(View v)
mSelectedItem = getAdapterPosition();
notifyDataSetChanged();
;
itemView.setOnClickListener(clickListener);
mRadio.setOnClickListener(clickListener);
list_view_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio"
android:layout_
android:layout_
/>
<TextView
android:id="@+id/text"
android:layout_
android:layout_
/>
</LinearLayout>
在AddItems.java
类中
RadioAdapter RadioAdapter;
List<String> listItems;
@Override
protected final void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItems = new ArrayList<String>();
radioAdapter = new RadioAdapter(this, listItems)
recyclerView.setAdapter(radioAdapter);
private void DisplayList()
Cursor cursor = myDB.readlistAllData();
if (cursor.getCount() == 0)
Toast.makeText(this, "No Data.", Toast.LENGTH_SHORT).show();
else
while (cursor.moveToNext())
listitems.add(cursor.getString(1));
radioAdapter.notifyDataSetChanged();
参考:http://joshskeen.com/building-a-radiogroup-recyclerview/
以下是一些其他有用的链接:
https://developer.android.com/guide/topics/ui/layout/recyclerview https://medium.com/@codeplayon/android-how-get-list-of-checked-checkboxes-from-recyclerview-android-cba31d8e3291 Single selection in RecyclerView如果您需要更多帮助,请告诉我。
【讨论】:
请注意,此代码的来源网站拥有“版权所有 © 2020 joshskeen.com”。我没有找到实际的许可证。 是的。我添加了对该站点的引用。我希望没关系。 我看到你有一个参考,但是..How to reference material written by others 说“不要复制外部资源的完整文本;相反,用他们的文字和想法来支持你自己的”。在版权问题上,我不是律师,但我不确定是否可以使用受版权保护的材料,即使它链接到它。如果它是 copyleft 或 CC-BY,我相信不会有任何法律问题。 我明白了。您如何建议我在没有法律问题的情况下更改此答案?博客的内容其实解决了OP的问题,所以我一开始加了链接。 很遗憾我不是安卓开发者,所以我不能指导你。我可能会尝试在不查看源代码的情况下尝试修改我的解决方案。如果你想让我的 cmets 消失,你可以在这里 ping 我。以上是关于Android:如何在列表视图中自动显示带有复选框的 Sqlite 数据库的主要内容,如果未能解决你的问题,请参考以下文章