【问题标题】:Android- Firebase onChildChangedAndroid- Firebase onChildChanged
【发布时间】:2017-11-30 07:57:34
【问题描述】:

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

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

【问题讨论】:

    标签: android listview firebase firebase-realtime-database


    【解决方案1】:

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

    用这个替换那行:

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

    它会为你工作 谢谢,编码愉快。

    已编辑

    替换这一行:

      int index = mkeys.indexOf(boothname);
    

    有了这个:

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

    它将解决您的崩溃问题。

    【讨论】:

    • 这将消除编译错误但逻辑上不正确的int index = mkeys.indexOf(boothname);,这里index将永远是-1并产生崩溃
    • 对不起,我没有得到。你想说什么?
    • 哦,我没有看到完整的OP代码,我的错,你的代码仍然无法工作
    • 现在问题出在哪里,实际上是什么??...我认为没有任何语法或逻辑错误。而是告诉我...我会解决
    • 好的,override equals and hashcode in Booth 让它工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 2019-10-06
    相关资源
    最近更新 更多