繁体   English   中英

android使用recyclerview列出数据库中的数据

[英]android list the data from database using recyclerview

我想显示来自android数据库的数据。 在这里我正在使用回收站视图。 参考代码为http://www.androidhive.info/2016/01/android-working-with-recycler-view/ 我的代码如下所示。

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);*/
}
}

在prepareData()方法中,我可以在日志中打印答案。 但是我无法在活动中显示。 请帮我。

您可以使用cursoradapter从数据库加载数据,并在recyclerview中将其与视图持有者一起显示。

要在列表中显示数据库中的数据,可以使用CursorAdapter 只需为此创建一个Cursor,就可以让他完成这项工作。

一个简单的例子如下:

 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);

复杂的部分是根据您的需要实现CursorAdapter。 这是将构建视图并读取光标的视图。 每行将调用BindView

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));
  }
}

在这里,您有一个小示例,您只需要生成Layout(仅两个TextView)即可匹配我编写的cursorAdapter。

所述我用于生成此小例子。 您会发现比这里更多的信息。

我通过添加List<Bean> beanList = handler.getAllContacts();更改了prepareData()方法List<Bean> beanList = handler.getAllContacts();

我已经使用了mAdapter.notifyDataSetChanged();

private void prepareData() {
    DatabaseHandler handler = new DatabaseHandler(getApplicationContext());
    Log.d("Reading: ", "Reading all contacts..");
    List<Bean> beanList = handler.getAllContacts();
    this.beanList.addAll(beanList);
    mAdapter.notifyDataSetChanged();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM