簡體   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