gpt4 book ai didi

Android-Firebase onChildChanged

转载 作者:行者123 更新时间:2023-11-30 00:14:21 26 4
gpt4 key购买 nike

我希望我的子数据在我更改数据时实时更新。我是 Java 编程的新手。错误是当我试图用新的子数据设置我的列表 arraylist 时,错误的第二个参数出现错误。以下是我的代码...感谢任何帮助,提前致谢。

public class OverallResultFragment extends Fragment {
private static final String TAG = "OverallResultFragment";

private ArrayList<Booth> list = new ArrayList<>();;
private ArrayList<Booth> mkeys = new ArrayList<>();
ProductListAdapter adapter = null;
private Firebase mRef;
ListView gridView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Firebase.setAndroidContext(this.getContext());
View view = inflater.inflate(R.layout.activity_votingresult, container, false);
mRef = new Firebase("https://myfb.firebaseio.com/myfb");
gridView = (ListView) view.findViewById(R.id.gridView);
adapter = new ProductListAdapter(this.getContext(), R.layout.list_product_item, list);
gridView.setAdapter(adapter);


com.firebase.client.Query Queryref = mRef.orderByValue();
Queryref.addChildEventListener(new com.firebase.client.ChildEventListener() {
@Override
public void onChildAdded(com.firebase.client.DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
String boothname = dataSnapshot.getKey();
list.add(new Booth(boothname, value));

mkeys.add(new Booth(boothname));
adapter.notifyDataSetChanged();
}

@Override
public void onChildChanged(com.firebase.client.DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
String boothname = dataSnapshot.getKey();

int index = mkeys.indexOf(boothname);
list.set(index, value);//error on "value" -> wrong 2nd argument.
adapter.notifyDataSetChanged();
}
}

这是我的 Booth 类:

 public class Booth {
String rating;
String Boothname;
int index;


public Booth(String boothname, String Rating){
this.Boothname = boothname;
this.rating= Rating;
}

public String getBoothName(){
return Boothname;
}
public void setProductId(String boothname){
this.Boothname = boothname;
}

public String getRating(){
return rating;
}
public void setRating(String rating){
this.rating = rating;
}
}

最佳答案

错误是因为它不是字符串类型的第二个参数。

将该行替换为:

  list.set(index, new Booth(boothname,value));

这对你有用谢谢,编码愉快。

已编辑

替换这一行:

  int index = mkeys.indexOf(boothname);

有了这个:

 int index = mkeys.indexOf(new Booth(boothname));

它将解决您的崩溃问题。

关于Android-Firebase onChildChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47568482/

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