これはおそらくここでのいくつかの質問と重複していることは知っていますが、ちょっと、それらの解決策は機能していません。だから私は本当に提案が必要です。 firebase
を使用してlistview
を管理しようとしましたが、役に立たないです。 私はいくつかのチュートリアルを試しましたが、誰も私の問題を解決していません。 Firebase
から移入しようとしているlistview
があります。 custom adapter
を作成し、そこにchildEventListeners
を追加しました。最後に私はにadapter
を添付します。 firebase
database
にいくつかの項目がありますが、最後のものだけがlistview
に表示されています。これらの項目をすべて表示したいと考えています。リストビューでFirebaseを使用すると1行しか表示されない
public abstract class FireBaseListAdapter extends ArrayAdapter<SaleModel> {
Activity activity;
// Setup our Firebase mFirebaseRef
DatabaseReference mRef;
private SaleModel model;
private String mUsername;
private LayoutInflater mInflater;
private List<SaleModel> mModels;
private List<String> mKeys;
private ChildEventListener mListener;
private ViewGroup viewGroup;
private LinearLayout wrapper;
public FireBaseListAdapter(DatabaseReference mRef, Activity activity) {
super(activity, android.R.layout.simple_list_item_1);
this.activity = activity;
this.mRef = mRef;
this.mUsername = mUsername;
mInflater = activity.getLayoutInflater();
mModels = new ArrayList<SaleModel>();
mKeys = new ArrayList<String>();
// Look for all child events. We will then map them to our own internal ArrayList, which backs ListView
mListener = this.mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Map<String, Object> newPost =
(Map<String, Object>) dataSnapshot.getValue();
model = new SaleModel(newPost.get("name").toString(),
newPost.get("attendant_name").toString(),
newPost.get("created_at").toString());
String key = dataSnapshot.getKey();
// Insert into the correct location, based on previousChildName
if (previousChildName == null) {
mModels.add(0, model);
mKeys.add(0, key);
} else {
int previousIndex = mKeys.indexOf(previousChildName);
int nextIndex = previousIndex + 1;
if (nextIndex == mModels.size()) {
mModels.add(model);
mKeys.add(key);
} else {
mModels.add(nextIndex, model);
mKeys.add(nextIndex, key);
//showNotification(model);
}
}
notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
// One of the mModels changed. Replace it in our list and name mapping
String key = dataSnapshot.getKey();
Map<String, Object> newPost =
(Map<String, Object>) dataSnapshot.getValue();
SaleModel newModel = new SaleModel(newPost.get("message").toString(), newPost.get("author").toString(), newPost.get("created_at").toString());
int index = mKeys.indexOf(key);
mModels.set(index, newModel);
notifyDataSetChanged();
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
// A model was removed from the list. Remove it from our list and the name mapping
String key = dataSnapshot.getKey();
int index = mKeys.indexOf(key);
mKeys.remove(index);
mModels.remove(index);
notifyDataSetChanged();
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
// A model changed position in the list. Update our list accordingly
String key = dataSnapshot.getKey();
Map<String, Object> newPost =
(Map<String, Object>) dataSnapshot.getValue();
SaleModel newModel = new SaleModel(newPost.get("message").toString(), newPost.get("author").toString(), newPost.get("created_at").toString());
int index = mKeys.indexOf(key);
mModels.remove(index);
mKeys.remove(index);
if (previousChildName == null) {
mModels.add(0, newModel);
mKeys.add(0, key);
} else {
int previousIndex = mKeys.indexOf(previousChildName);
int nextIndex = previousIndex + 1;
if (nextIndex == mModels.size()) {
mModels.add(newModel);
mKeys.add(key);
} else {
mModels.add(nextIndex, newModel);
mKeys.add(nextIndex, key);
}
}
notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("FireBaseListAdapter", "Listen was cancelled, no more updates will occur");
}
});
}
public void cleanup() {
// We're being destroyed, let go of our mListener and forget about all of the mModels
mRef.removeEventListener(mListener);
mModels.clear();
mKeys.clear();
}
@Override
public int getCount() {
return mModels.size();
}
@Override
public SaleModel getItem(int i) {
return mModels.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
SaleModel comment = getItem(position);
int type = getItemViewType(position);
// Map a Chat object to an entry in our listview
String author = comment.getName();
String attendant_name = comment.getAttendant_name();
String created_at = comment.getCreated_at();
// If the message was sent by this user, color it differently
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService
(this.activity.getApplicationContext().LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.itemview, parent, false);
((TextView) row.findViewById(R.id.attname)).setText(attendant_name);
((TextView) row.findViewById(R.id.c_name)).setText(author);
((TextView) row.findViewById(R.id.updated_at)).setText(created_at);
return row;
}
}
SaleModel.java:
public class SaleModel {
//obviously there's a lot of things i'm not using here, so ignore these
String id, business_id, attendant_id, attendant_name, product_id,
customer_id, customer_first_name, code,
customer_last_name, customer_image, uid,
reader_id, price, quantity, name, image,
created_at, deleted_at, offlineTag, updated_at, svg;
public SaleModel() {
}
public SaleModel(String name, String attendant_name, String created_at) {
this.name = name;
this.attendant_name = attendant_name;
this.created_at = created_at;
}
//getters and setters
public String getAttendant_name() {
return attendant_name;
}
public void setAttendant_name(String attendant_name) {
this.attendant_name = attendant_name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}}
そして、これは私が私のフラグメントでそれを使用している方法です:
FireBaseListAdapter.java@Override
public void onStart() {
super.onStart();
FirebaseDatabase ref = FirebaseDatabase.getInstance().getReference().child("sales");
setUpAdapter();
final FireBaseListAdapter mChatListAdapter = new FireBaseListAdapter(ref, parentActivity) {
@Override
public void cleanup() {
super.cleanup();
}
};
listView.setAdapter(mChatListAdapter);
mChatListAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listView.setSelection(mChatListAdapter.getCount() - 1);
}
});
}
そして例の私のデータ構造:
{
"-KMjcAb5O2MOx_WrofUT" : {
"attendant_id" : "93",
"business_id" : "9",
"code" : "9.146860413464",
"created_at" : "2016-07-15 08:32:10",
"customer_first_name" : "Unidentified",
"customer_id" : "10",
"customer_last_name" : "",
"image" : "",
"name" : "Black Forest Cake 2kg",
"price" : "3500.0",
"product_id" : "59",
"quantity" : "1",
"reader_id" : "93",
"uid" : ""
},
"-KMjcAb8EQi3ujTpoJG1" : {
"attendant_id" : "93",
"business_id" : "9",
"code" : "9.146860413465",
"created_at" : "2016-07-15 08:32:10",
"customer_first_name" : "Unidentified",
"customer_id" : "10",
"customer_last_name" : "",
"image" : "",
"name" : "Black Forest Cake 2kg",
"price" : "3500.0",
"product_id" : "59",
"quantity" : "1",
"reader_id" : "93",
"uid" : ""
}}
私はhttps://github.com/firebase/AndroidChat/blob/master([AndroidChatサンプルアプリケーションから 'FirebaseListAdapter'クラス]からあなたは多くのコードを再使用していた印象を受けます/app/src/main/java/com/firebase/androidchat/FirebaseListAdapter.java)。このような再利用は問題ありませんが、[FirebaseUIプロジェクトの1つ](https://github.com/firebase/FirebaseUI-Android/blob/master/database/src/main/java/com/)から始めることを強くお勧めします。 firebase/ui/database/FirebaseListAdapter.java)、これはずっと簡単です*と*はかなりのバグを修正します。 –
それ以外は、すでにデバッガのコードをステップしましたか?あなたの 'onChildAdded()'は何度もトリガされますか?あなたの配列リストは正しい数の子供で終わるのですか?スタックオーバーフローは非常に非効率的なデバッグの仕組みなので、あなた自身でそれをして、私たちに教えてください。また、問題をより小さなものに縮小することも役に立ちます。例えば。単純な文字列リストで同じ問題が発生すると思います。 **最小限の**再現でより多くの努力を払うことができますが、問題の原因となる可能性のあるものにもっと迅速に集中することができます。 –
@Frank van Puffelenはい私はそこからコードのほとんどを再利用しました:)問題は私が結果を複製するように見えないことです。また、はい、私のOnChildAddedは複数回トリガされますか?私のシンプルさの欠如のため申し訳ありません、私jusは昨日以来不満を持っていました。 OnChildAddedイベントを記録すると、私のarraylistのサイズがListviewに表示されるサイズよりも大きいことがわかりました。 –