Good Evening。Firebaseデータベースを使用してRecyclerViewにデータを入力する
私はImageViewを持つRecycleViewにデータを取り込みたいテストコードを持っています。 ImageViewは、Firebase DatabaseのURLからJPGイメージで埋められます。
問題は、データを取得してリサイクルビューにデータを取り込む方法がないことです。 GithubでFirebaseUIを読んでいましたが、このアプリケーションでは孤独なので、何とか難しいと思っています。
これはMainActivityコードです:
public class MainActivity extends AppCompatActivity{
DatabaseReference mReference;
DatabaseReference mURLReference;
public FirebaseRecyclerAdapter<Pictures, PicturesHolder> mRecyclerViewAdapter;
//UI
RecyclerView mImagesRV;
LinearLayoutManager mManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mReference = FirebaseDatabase.getInstance().getReference();
mURLReference= mReference.child("Pictures Data");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
mImagesRV=(RecyclerView)findViewById(R.id.recyclerView);
mManager=new LinearLayoutManager(this);
mManager.setReverseLayout(false);
mImagesRV.setHasFixedSize(false);
mImagesRV.setLayoutManager(mManager);
}
public void onAttachRecyclerView()
{
Query lastHundred = mURLReference.limitToLast(100);
mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Pictures, PicturesHolder>(
Pictures.class,R.layout.content_main,PicturesHolder.class, lastHundred
) {
@Override
protected void populateViewHolder(PicturesHolder pictureView, Pictures pictures, int position) {
}
};
}
}
これは私がFirebaseDatabase(私はちょうどURLをしたい)内のデータを挿入するために使用するオブジェクトです:
public class Pictures {
String pictureID;
String downloadURL;
double latitude;
double longitude;
public Pictures() {
}
public void setPictureID(String pictureID) {
this.pictureID = pictureID;
}
public void setDownloadURL(String downloadURL) {
this.downloadURL = downloadURL;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getPictureID() {
return pictureID;
}
public String getDownloadURL() {
return downloadURL;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
}
そして、これはViewHolderあり、私はそれが大丈夫かどうかわからない。
public class PicturesHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public PicturesHolder(View itemView)
{
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.picture_view);
}
public void setImageView(ImageView imageView) {
this.imageView = imageView;
}
}
あなたから良い助言を受けるのはいいですね! ありがとうございます。
まあ、私はfirebaseデータベースを使ったことはありませんが、ネットワーキングには何を使用していますか?私は何も見ることができないので。 –
まず、PictureからPictureHolderビューにデータをコピーするためのpopulateViewHolderの実装を提供する必要があります。第二に、おそらく、ピクチャホールダーにビューを設定するメソッドは望ましくありません。 ViewHoldersは、渡されたitemViewで見つかったビューへの参照のみを抽出します。代わりに、おそらくダウンロードURLを取ってGlideのようなものを使って画像をダウンロードし、それをImageViewに配置したいと思うでしょう。最後に、RecyclerViewの仕組みに慣れていない場合は、まずFirebaseを使わずに簡単な例を実行することができます。 –