gpt4 book ai didi

java - 如何从 firebase 检索 itemName 并将其用于 searchView?

转载 作者:行者123 更新时间:2023-11-30 00:13:36 24 4
gpt4 key购买 nike

我尝试从 Firebase 检索 itemName 并尝试将其用于 searchView。而且我不知道我在下面的代码中的逻辑有什么问题。

So here is my database

这是我的数据快照:-

 protected void onStart() {
super.onStart();

databaseItems.child("items").orderByChild("itemName").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
itemFinalList.clear();
for (DataSnapshot itemSnapShot : dataSnapshot.getChildren()) {
ItemInventory itemInventoryTest = itemSnapShot.getValue(ItemInventory.class);

itemFinalList.add(itemInventoryTest);

}

ItemList adapter = new ItemList(SearchActivity.this, itemFinalList);
listFinal.setAdapter(adapter);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

和searchview函数

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
MenuItem item = menu.findItem(R.id.menuSearch);
SearchView searchView = (SearchView) item.getActionView();


searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}

这是我的“itemList”类,因为它正在我的数据快照中使用:-

public class ItemList extends ArrayAdapter<ItemInventory> {

private Activity context;
private List<ItemInventory> items;

public ItemList(Activity context, List<ItemInventory> items) {
super(context, R.layout.list_layout, items);
this.context = context;
this.items = items;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();

View listViewItem = inflater.inflate(R.layout.list_layout, null, true);

TextView textViewCategory = (TextView) listViewItem.findViewById(R.id.textViewCategory);
TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
TextView textViewCost = (TextView) listViewItem.findViewById(R.id.textViewCost);
final TextView textViewCount = (TextView) listViewItem.findViewById(R.id.textViewCount);

ItemInventory itemInventoryTest = items.get(position);

textViewCategory.setText(itemInventoryTest.getItemCategory());
textViewName.setText(itemInventoryTest.getItemName());
textViewCost.setText(itemInventoryTest.getItemCost());
textViewCount.setText(itemInventoryTest.getItemCount());

return listViewItem;

}
}

我还可以为 searchList 创建一个新类,我可以在其中更改我的数据类型。

最佳答案

 //under class declaration do this:
private ArrayList<String> itemlists;




itemLists=new ArrayList<String>();
databaseItems.child("items").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot itemSnapShot : dataSnapshot.getChildren()) {
String itemname=itemSnapShot.child("itemName").getValue().toString();

itemlists.add(itemname);
ArrayAdapter adapter = new ArrayAdapter(SearchActivity.this,android.R.layout.simple_list_item_1 itemlists);
listFinal.setAdapter(adapter);

}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

尝试上面的方法,将 clear() 放在 onDataChange 之前,这就是它不起作用的原因,每次您尝试检索时,您也会清除列表。在开始 Activity 时将 clear() 放在前面以避免重复值。

关于java - 如何从 firebase 检索 itemName 并将其用于 searchView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47719548/

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