viewPager适配器未更新

时间:2018-04-25 02:20:48

标签: android firebase firebase-realtime-database android-viewpager android-adapter

我正在尝试从CollegeHTMLParse.class更新适配器(MyCollegePagerAdapter)。但是,即使我创建了一个对象并在其上调用了notifyDataSetChanged()方法,注意也会显示在片段上。你能告诉我,我的代码是否有任何问题,因为我已经用Google搜索了几个星期的解决方案,经过几个小时的尝试,我无法弄清楚这个问题。

这是CollegeHTMLParse类:

public class CollegeHTMLParse extends AppCompatActivity {
    public static ArrayList<String> colleges = new ArrayList<>();
    public static ArrayList<String> accepted = new ArrayList<>();
    public static ArrayList<String> link = new ArrayList<>();
    public static ArrayList<String> location = new ArrayList<>();
    public static ArrayList<String> tuition = new ArrayList<>();
    FirebaseDatabase mRef;
    DatabaseReference database;
    public static ArrayList<String> data = new ArrayList<>();
    MyCollegePagerAdapter adapter = new MyCollegePagerAdapter(getSupportFragmentManager());

    public void onCreate(String CollegeCode) {
        colleges.clear();
        accepted.clear();
        link.clear();
        location.clear();
        tuition.clear();
        mRef = FirebaseDatabase.getInstance("https://successway18.firebaseio.com/");
        database = mRef.getReference().child("CollegeCodes").child("30");
        database.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                setCollegeValues(dataSnapshot.getKey());
            }

            @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(DatabaseError databaseError) {

            }
        });
    }


    public void setCollegeValues(String values) {
        colleges.add(values);
        int i = 0;
        while (i < colleges.size()) {
            database.child(colleges.get(i)).addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    setDetailsValues(dataSnapshot.getValue().toString());
                }

                @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(DatabaseError databaseError) {

                }
            });
            i++;
        }

    }

    private void setDetailsValues(String details) {
        data.add(details);
        if (data.size() == 4) {
            accepted.add(data.get(0));
            link.add(data.get(1));
            location.add(data.get(2));
            tuition.add(data.get(3));
            data.clear();
        }
        update();
    }

    private void update() {           
      adapter.notifyDataSetChanged();    
    }
}

非常感谢你!

2 个答案:

答案 0 :(得分:0)

在寻呼机适配器中添加此内容

  @Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}

答案 1 :(得分:0)

Firebase查询在工作线程中运行,因此您必须在主线程中运行notifyDataSetChanged。 尝试在函数update()

中使用以下代码
runOnUiThread(new Runnable() {
    @Override
    public void run() {

    }
});