Android- Firebase onChildChanged

时间:2017-11-30 07:57:34

标签: android listview firebase firebase-realtime-database

我想在更改数据时实时更新我的​​子数据。我是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;
        } 
    }

1 个答案:

答案 0 :(得分:1)

错误是因为它不是字符串类型2nd Argument。

用这个替换该行:

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

它适合你 谢谢,快乐的编码。

<强> EDITED

替换此行:

  int index = mkeys.indexOf(boothname);

有了这个:

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

它将解决您的崩溃问题。