gpt4 book ai didi

android - 在 ListView 中显示 firebase 子对象

转载 作者:太空狗 更新时间:2023-10-29 13:11:57 24 4
gpt4 key购买 nike

我是 Firebase 的新手。我正在尝试查询 firebase 数据库并在 ListView 中显示结果的所有子对象。我没有错误,但没有显示任何内容。它不会崩溃,但也不会执行任何操作。请帮帮我。

我的数据库的内容: Contents of my database

这是我的数据检索代码:

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


}

最佳答案

我自己修好了。显然,查询返回了一个对象列表,因此我用 for 循环替换了 Hashmap 以获取子项并将它们添加到列表中。但是我的 ListView 根本不会显示检索到的数据。最后,我使用了下面给出的 firebase UI List Adpater 并修复了它。谢谢大家!

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

关于android - 在 ListView 中显示 firebase 子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39558397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com