2017-01-11 11 views
0

アンドロイドデータベースからデータを表示します。ここで私はリサイクラービューを使用しています。参照コードはhttp://www.androidhive.info/2016/01/android-working-with-recycler-view/です。私のコードは以下の通りです。androidはrecyclerviewを使用してデータベースからデータを一覧表示します

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()メソッドでログに回答を出力できます。しかし私は私の活動に表示することはできません。私を助けてください。

答えて

-1

私はList<Bean> beanList = handler.getAllContacts();

を追加することにより、prepareData()方法を変更していると私は、リスト内のデータベースからデータを表示するには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(); 
} 
+0

これは本当にあなたが望むことを行うには汚れた方法です。カーソルアダプタは正しい方法です。現時点では例を書く時間がありませんが、今日後で私はあなたにコードの一部を見せています。 – ChampS

+0

コードを説明してください。コードのみの回答は有用ではなく、ユーザーは無視することができます。 – AxelH

+0

@AxelH私は自分の答えを変更しました。 –

1

cursoradapterを使用して、データベースからデータをロードし、それをビュー所有者と一緒にrecyclerviewに表示することができます。

+0

例を書くことができますか? –

+0

カッターソルアダプターはどのように可能ですか? –

0

を使用する必要があり、あなたはCursorAdapterを使用することができます。これにカーソルを作成するだけで、彼に仕事をさせることができます。

簡単な例は次のようになります。

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

ここでは、私が書いたcursorAdapterと一致するレイアウト(単純に2つのTextView)を生成する必要があります。

source私はこの小さな例を生成するために使用しました。あなたはここよりも多くの情報を見つけるでしょう。

関連する問題