Java android使用recyclerview列出数据库中的数据
我想显示来自android数据库的数据。这里我使用的是回收视图。参考代码为。我的代码如下所示Java android使用recyclerview列出数据库中的数据,java,android,android-recyclerview,Java,Android,Android Recyclerview,我想显示来自android数据库的数据。这里我使用的是回收视图。参考代码为。我的代码如下所示 SpeedDialViewActivity.java public class SpeedDialViewActivity extends AppCompatActivity { private List<Bean> beanList = new ArrayList<>(); private RecyclerView recyclerView; private ViewAdap
SpeedDialViewActivity.java
public class SpeedDialViewActivity extends AppCompatActivity {
private List<Bean> beanList = new ArrayList<>();
private RecyclerView recyclerView;
private ViewAdapter mAdapter;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_speed_dial_view);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new ViewAdapter(beanList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
prepareData();
}
private void prepareData() {
DatabaseHandler handler = new DatabaseHandler(getApplicationContext());
Log.d("Reading: ", "Reading all contacts..");
List<Bean> beanList = handler.getAllContacts();
Bean bean;
for (Bean cn : beanList) {
String log = "Id: " + cn.getId() + " ,Name: " + cn.getName() + " ,Phone: " + cn.getNumber();
// Writing Contacts to log
Log.d("Name: ", log);
/* bean = new Bean(cn.getName(), cn.getNumber(), cn.getSpeeddial());
beanList.add(bean);
}*/
/* Bean bean = new Bean("Mad Max: Fury Road", "Action & Adventure", "2015");
beanList.add(bean);
bean = new Bean("Inside Out", "Animation, Kids & Family", "2015");
beanList.add(bean);
bean = new Bean("Inside Out", "Animation, Kids & Family", "2015");
beanList.add(bean);
bean = new Bean("Inside Out", "Animation, Kids & Family", "2015");
beanList.add(bean);*/
}
}
SpeedDialViewActivity.java
公共类SpeedDialViewActivity扩展了AppCompatActivity{
private List beanList=new ArrayList();
私人回收站;
专用可视适配器;
语境;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u speed\u dial\u view);
recyclerView=(recyclerView)findViewById(R.id.recycler\u视图);
mAdapter=新的ViewAdapter(beanList);
RecyclerView.LayoutManager mLayoutManager=新的LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
setItemAnimator(新的DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
prepareData();
}
私有无效准备数据(){
DatabaseHandler=新的DatabaseHandler(getApplicationContext());
日志d(“读取:”,“读取所有联系人…”);
List beanList=handler.getAllContacts();
菜豆;
for(Bean cn:Bean列表){
字符串log=“Id:”+cn.getId()+”,名称:“+cn.getName()+”,电话:“+cn.getNumber()”;
//将联系人写入日志
Log.d(“名称:”,Log);
/*bean=新bean(cn.getName(),cn.getNumber(),cn.getSpeeddial());
添加(bean);
}*/
/*豆豆=新豆豆(“疯狂马克斯:狂暴之路”,“行动与冒险”,“2015”);
添加(bean);
bean=新bean(“由内而外”、“动画、儿童与家庭”、“2015”);
添加(bean);
bean=新bean(“由内而外”、“动画、儿童与家庭”、“2015”);
添加(bean);
bean=新bean(“由内而外”、“动画、儿童与家庭”、“2015”);
添加(bean)*/
}
}
在prepareData()方法中,我可以在日志中打印答案。但我不能在我的活动中表现出来。请帮助我。您可以使用游标适配器从数据库加载数据,并在您的recyclerview中使用视图保持器显示数据。我通过添加
列表beanList=handler.getAllContacts()更改了prepareData()
方法代码>
我使用了mAdapter.notifyDataSetChanged()代码>
private void prepareData(){
DatabaseHandler=新的DatabaseHandler(getApplicationContext());
日志d(“读取:”,“读取所有联系人…”);
List beanList=handler.getAllContacts();
this.beanList.addAll(beanList);
mAdapter.notifyDataSetChanged();
}
要在列表中显示数据库中的数据,可以使用。只需创建一个指向此的光标,您就可以让他完成此工作
下面是一个简单的例子:
DatabaseHandler handler = new DatabaseHandler(this);
SQLiteDatabase db = handler.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT first_name, last_name FROM person", null);
然后,将此光标传递给适配器:
ListView lvItems = (ListView) findViewById(R.id.lvItems);
MyCursorAdapter adapter = new TodoCursorAdapter(this, cursor);
lvItems.setAdapter(adapter);
复杂的部分是根据您的需要实现游标适配器。这将构建视图并读取光标<代码>绑定视图
将为每一行调用
public class MyCursorAdapter extends CursorAdapter {
public MyCursorAdapter (Context context, Cursor cursor) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_cursor, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView txtLastName= (TextView) view.findViewById(R.id.txtLastName);
TextView txtFirstName= (TextView) view.findViewById(R.id.txtFirstName);
String lastName = cursor.getString(cursor.getColumnIndexOrThrow("last_name"));
String firstName = cursor.getString(cursor.getColumnIndexOrThrow("first_name"));
txtLastName.setText(body);
txtFirstName.setText(String.valueOf(priority));
}
}
这里有一个小示例,您只需要生成布局(仅两个TextView)以匹配我编写的游标适配器
我用来生成这个小示例的。你会发现比这里更多的信息。你能写一个例子吗?用cutrsor adapter怎么可能呢?这是一个非常不干净的方法来做你想做的事情。光标适配器将是正确的方法。目前我没有时间写一个例子,但今天晚些时候我会给你看一段代码,请解释一下你的代码。纯代码答案没有帮助,用户可以忽略。@AxelH我已经修改了答案。
public class MyCursorAdapter extends CursorAdapter {
public MyCursorAdapter (Context context, Cursor cursor) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_cursor, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView txtLastName= (TextView) view.findViewById(R.id.txtLastName);
TextView txtFirstName= (TextView) view.findViewById(R.id.txtFirstName);
String lastName = cursor.getString(cursor.getColumnIndexOrThrow("last_name"));
String firstName = cursor.getString(cursor.getColumnIndexOrThrow("first_name"));
txtLastName.setText(body);
txtFirstName.setText(String.valueOf(priority));
}
}