我是Firebase的新手。我正在尝试查询firebase数据库并在ListView中显示结果的所有子对象。我没有错,但没有显示任何内容。它不会崩溃,但它也没有做任何事情。请帮帮我。
我的数据库的内容:
这是我的数据检索代码:
imgbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
key = search.getText().toString().trim();
Firebase newRef = new Firebase("https://stockmanager-142503.firebaseio.com/Items");
Query query = newRef.orderByChild("Idno").equalTo(key);
query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map<String,Item> td = (HashMap<String,Item>) dataSnapshot.getValue();
List<Item> valuesToMatch = new ArrayList<Item>(td.values());
myAdapter myadapter=new myAdapter(getActivity(),valuesToMatch);
mlistView.setAdapter(myadapter);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(firebaseError.getMessage())
.setTitle("Error!")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
});
这是我的适配器类:
public class myAdapter extends BaseAdapter {
private List<Item> items;
private Context mContext;
public myAdapter(Context mContext, List<Item> items) {
this.mContext=mContext;
this.items=items;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LinearLayout linearLayout;
Item item=items.get(i);
if (view == null) {
linearLayout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.rowitem, viewGroup, false);
} else {
linearLayout = (LinearLayout)view;
}
TextView text1=(TextView)linearLayout.findViewById(R.id.text1);
TextView text2=(TextView)linearLayout.findViewById(R.id.text2);
TextView text3=(TextView)linearLayout.findViewById(R.id.text3);
TextView text4=(TextView)linearLayout.findViewById(R.id.text4);
TextView text5=(TextView)linearLayout.findViewById(R.id.text5);
text1.setText(item.getIdno());
text2.setText(item.getName());
text3.setText(item.getBrand());
text4.setText(item.getCost());
text5.setText(item.getDate());
return null;
}
}
这是项目类:
public class Item {
private String Type;
private String Name;
private String Brand;
private String Cost;
private String Date;
private String Store;
private String Idno;
public String getName() {
return Name;
}
public String getIdno() {
return Idno;
}
public String getCost() {
return Cost;
}
public void setDate(String date) {
Date = date;
}
public String getBrand() {
return Brand;
}
public String getStore() {
return Store;
}
public String getType() {
return Type;
}
public String getDate() {
return Date;
}
}
答案 0 :(得分:2)
imgbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
key = scanContent;
Query q = dbRef.orderByChild("idno").equalTo(key);
FirebaseListAdapter<Item> adapter =new FirebaseListAdapter<Item>(getActivity(),Item.class,R.layout.rowitem,q) {
@Override
protected void populateView(View v, Item item, int position) {
TextView text1=(TextView)v.findViewById(R.id.text1);
TextView text2=(TextView)v.findViewById(R.id.text2);
TextView text3=(TextView)v.findViewById(R.id.text3);
TextView text4=(TextView)v.findViewById(R.id.text4);
TextView text5=(TextView)v.findViewById(R.id.text5);
text1.setText(item.getIdno());
text2.setText(item.getName());
text3.setText(item.getBrand());
text4.setText(item.getCost());
text5.setText(item.getDate());
solditem=item;
}
};
sell.setText("Add this result to Sold Items List?");
sList.setAdapter(adapter);
}
});