我想在数据库中列出所有存储的数据,并使用回收站视图/适配器显示它。任何人都可以帮我解决这个问题。
-i有一类getter和setter(PWClass)
以下是我的一些代码:
这是我在课堂上存储一些数据并将其添加到列表中的时间,但它只是一个样本。
PWClass pwc = new PWClass("Facebook", "Action & Adventure", "2015");
pwList.add(pwc);
pwc = new PWClass("Instagram", "Action & Adventure", "2015");
pwList.add(pwc);
mAdapter.notifyDataSetChanged();
我的问题是我如何结合上面的代码并在数据库上调用查询来显示数据,如:
//i declare on top
private List<PWClass> pwList = new ArrayList<>();
private RecyclerView recyclerView;
private PWAdapter mAdapter;
Database controller;
//and from a method below
pwList.add(controller.add_pw());//adding on list array by calling my db method
我应该使用一些for / while循环/数组吗?我正在考虑上面的代码,但我知道它的错误。你明白了吗?
请帮助,以便我能够理解更多,感谢您的建议,谢谢!
答案 0 :(得分:0)
public List<userData> getAlluser() {
List<userData> usersdetail = new ArrayList<>();
curser = dbs.query("data_table", null, null, null, null, null, null);
try {
if (curser.moveToFirst()) {
do {
userData muser = new userData();
muser.appname = curser.getString(curser.getColumnIndex("appname"));
muser.user = curser.getString(curser.getColumnIndex("email"));
muser.passwordss = curser.getString(curser.getColumnIndex("password"));
usersdetail.add(muser);
} while (curser.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
}
return usersdetail;
}
&#13;
public class RecyclerViewAdopterMy extends RecyclerView.Adapter<RecyclerViewAdopterMy.MyViewHolder> implements View.OnClickListener{
List<userData> user = new ArrayList<>();
Context context;
public RecyclerViewAdopterMy(List<userData> user, Context context) {
this.user = user;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, null);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.appR.setText(user.get(position).appname);
holder.nameR.setText(user.get(position).user);
holder.passR.setText(user.get(position).passwordss);
holder.cardR.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(view.getRootView().getContext());
alertbox.setMessage("Do you want delete this item");
alertbox.setTitle("Delete");
alertbox.setIcon(R.drawable.delete);
alertbox.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
int pos=holder.getAdapterPosition();
user.remove(user.get(position));
notifyItemRemoved(position);
notifyItemRangeChanged(position,getItemCount());
notifyItemChanged(position);
}
});
alertbox.show();
return true;
}
});
}
@Override
public int getItemCount() {
return user.size();
}
@Override
public void onClick(View view) {
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView appR;
private final TextView nameR;
private final TextView passR;
private final CardView cardR;
public MyViewHolder(View view) {
super(view);
appR = (TextView) view.findViewById(R.id.Appstore);
nameR = (TextView) view.findViewById(R.id.userStore);
passR = (TextView) view.findViewById(R.id.passstore);
cardR=(CardView) view.findViewById(R.id.card);
}
}
}
&#13;
答案 1 :(得分:0)
阅读这篇文章,您将对Sqlite数据库有清晰的认识。 check this out