已成功从数据库检索数据,但无法通过适配器类

时间:2019-01-05 21:27:16

标签: android firebase-realtime-database recycler-adapter observer-pattern

好吧...我想在RecycleView中显示从Firebase数据库检索的数据,并做进一步的编辑,更新等工作。因此,我正在使用可观察的模式来检索数据(成功完成)并尝试将数据传递给构造函数和适配器类。数据确实显示在构造函数类中,但没有加载到适配器类以及RecycleView中。

片段类或主类

public class overviewFragment extends Fragment {

View view;
public static RecyclerView overRecycleView;
public overviewFragment() {}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.overview_fragment, container, false);
    overRecycleView = view.findViewById(R.id.overViewRecycle);
    overviewRecyclerAdapter adapter = new overviewRecyclerAdapter(getContext(), observerData.overViewlist);
    overRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
   // adapter.notifyDataSetChanged();
    overRecycleView.setAdapter(adapter);
    return view;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    observableData ob =new observableData();
    observerData observerData = new observerData(ob)
    ob.setMeasurement();
    ob.setMeasurement();  }}

可观察的类,用于检索Firebase数据并通知观察者

public class observableData extends Observable {
private String data;

public observableData() { }

public void setMeasurement(){
    final DatabaseReference n = ScrollingActivityforTutor.db();

    n.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String a = dataSnapshot.child("name").getValue().toString();
            Log.d("name from database ", a);

            data =a;

            measurementChanged();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

public void measurementChanged(){
    setChanged();
    notifyObservers();
}

public String getData() {
    return data;
}
}

将在其中调用构造函数类的观察者类

public class observerData implements  Observer {
Observable observable;
public String data;
public static List<itemOverview> overViewlist = new ArrayList<>();;

public observerData(Observable observable) {
    this.observable = observable;
    observable.addObserver(this);
}

@Override
public void update(Observable o, Object arg) {
    observableData od = (observableData) o;
    this.data = od.getData();
    passData();
}

public void passData(){
    overViewlist.add(new itemOverview(data));
    Log.d("data in observer ", data);
}
}

构造函数类

public class itemOverview {

private String text;

public itemOverview(String text) {
    Log.d("IN CONSTRUCTOR ", text);  // It is perfectly showing the data
    this.text = text;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}}

适配器类

 public class overviewRecyclerAdapter extends 
 RecyclerView.Adapter<overviewRecyclerAdapter.overviewViewHolder> {
 Context mcontext;
 List<itemOverview> mdata;

 public overviewRecyclerAdapter(Context mcontext, List<itemOverview> mdata) 
{
    this.mcontext = mcontext;
    this.mdata = mdata;  }

public static class overviewViewHolder extends RecyclerView.ViewHolder{

    private TextView t;

    public overviewViewHolder(@NonNull View itemView) {
        super(itemView);

        t = itemView.findViewById(R.id.overViewtextView); }}

@NonNull
@Override
public  overviewRecyclerAdapter.overviewViewHolder 
onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v;
    v = LayoutInflater.from(mcontext).inflate(R.layout.item_overview, 
    parent, false);
    overviewViewHolder overview = new overviewViewHolder(v);
    return overview;  }

@Override
public void onBindViewHolder(@NonNull 
overviewRecyclerAdapter.overviewViewHolder holder, int position) {

    Log.d("IN ADAPTER ", mdata.get(position).getText());  // HERE, IT IS 
    NOT WORKING

    holder.t.setText(mdata.get(position).getText());

}
@Override
public int getItemCount() {
    return mdata.size() ;
}}

但是,当我在onDatachange方法之外使用measurementChanged()(在可观察的类中)并将具有某些字符串值的数据(变量)放入时,一切正常,给定的数据显示在recycleView中。

不了解问题出在哪里以及在哪里调试。

但是,对这么长的代码感到抱歉:) 谢谢

1 个答案:

答案 0 :(得分:1)

我可以看到您正在尝试以编程方式将元素添加到RecyclerView。但是,当您更新包含必要数据的数组时,RecyclerView不会更新。将RecyclerView设为静态后,可以使用以下代码轻松地将更改通知更改为适配器:

public void passData(){
    overViewlist.add(new itemOverview(data));
    Log.d("data in observer ", data);

    overviewRecyclerAdapter adapter = (overviewRecyclerAdapter) overviewFragment.overRecycleView.getAdapter();
    adapter.notifyDataSetChanged();        
}

有很多更好的方法可以做到这一点。就性能而言,adapter.notifyItemInserted(overViewlist.size()-1);可能是一个更好的解决方案。

P.S。您应该遵循Java中的命名约定,以使代码更具可读性。