数据未显示在recyclerview上

数据未显示在recyclerview上

本文介绍了数据未显示在recyclerview上-出现错误未附加布局管理器;跳过布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从Firebase实时数据库检索数据到Recycler-view.我已经创建了名为Ph.java的Modul类和名为PhAdapter.java的适配器.但是,我很困惑,无法在MainActivity.java上调用适配器.这是我的代码:)

博士班

public class Ph {

    public String date;
    public String nilai;
    public String time;

    public Ph(){
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getNilai() {
        return nilai;
    }

    public void setNilai(String nilai) {
        this.nilai = nilai;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

这是我的PhAdapter.java

public class PhAdapter extends RecyclerView.Adapter<PhAdapter.PhViewHolder> {

    private List<Ph> dataph;
    private Context context;

    public PhAdapter(List<Ph> dataph, Context context) {
        this.dataph = dataph;
        this.context = context;
    }

    @Override
    public PhViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
        View view = LayoutInflater.from(context).inflate(R.layout.view_ph_list,parent,false);
        PhViewHolder phViewHolder = new PhViewHolder(view);
        return new PhViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final PhViewHolder holder, final int position) {
        Ph mylist = dataph.get(position);
        holder.tvNilai.setText(mylist.getNilai());
        holder.tvTime.setText(mylist.getTime());
        holder.tvDate.setText(mylist.getDate());
    }

    @Override
    public int getItemCount() {
        int arr = 0;
        try{
            if(dataph.size()==0){
                arr = 0;
            }
            else{
                arr=dataph.size();
            }
        }catch (Exception e){
        }
        return arr;
    }

    public static class PhViewHolder extends RecyclerView.ViewHolder {

        private TextView tvNilai, tvDate, tvTime;

        public PhViewHolder(View itemview) {
            super(itemview);
            tvNilai = (TextView) itemview.findViewById(R.id.tv_ph);
            tvTime = (TextView) itemview.findViewById(R.id.tv_time);
            tvDate = (TextView) itemview.findViewById(R.id.tv_date);
        }
    }
}

和我的MainActivity类

public class MainActivity extends AppCompatActivity {

    FirebaseDatabase database;
    DatabaseReference myRef;
    List<Ph> dataph;
    RecyclerView recyclerView;
    Button cekdata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cekdata = (Button) findViewById(R.id.btn_cekdata);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("ph");

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                dataph = new ArrayList<Ph>();
                for (DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
                    Ph value = dataSnapshot1.getValue(Ph.class);
                    Ph phModel = new Ph();
                    String tvNilai = value.getNilai();
                    String tvDate = value.getDate();
                    String tvTime = value.getTime();
                    phModel.setNilai(tvNilai);
                    phModel.setDate(tvDate);
                    phModel.setTime(tvTime);
                    dataph.add(phModel);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                //Failed to read value
                Log.w("Hello", "Failed to read Ph.", databaseError.toException());

            }
        });

        cekdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PhAdapter phAdapter = new PhAdapter(dataph,MainActivity.this);
                RecyclerView.LayoutManager recyce = new LinearLayoutManager(MainActivity.this);
                recyclerView.setLayoutManager(recyce);
                recyclerView.setAdapter(phAdapter);
            }
        });
    }

}

当我单击按钮时,数据没有显示在recyclerview上,并且出现错误,RecyclerView:未附加布局管理器;跳过布局.

解决方案

您需要完全按照以下步骤初始化LinearLayoutManager:

recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
RecyclerView.LayoutManager recyce = new
LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(recyce);
PhAdapter phAdapter = new PhAdapter(dataph,MainActivity.this);
recyclerView.setAdapter(phAdapter);

您更正后的Activity类应如下所示:

public class MainActivity extends AppCompatActivity {

    FirebaseDatabase database;
    DatabaseReference myRef;
    List<Ph> dataph= new ArrayList<>();
    RecyclerView recyclerView;
    PhAdapter phAdapter;
    Button cekdata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cekdata = (Button) findViewById(R.id.btn_cekdata);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
        RecyclerView.LayoutManager recyce = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(recyce);
        phAdapter = new PhAdapter(dataph,MainActivity.this);
        recyclerView.setAdapter(phAdapter);


        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("ph");

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
                    Ph value = dataSnapshot1.getValue(Ph.class);
                    Ph phModel = new Ph();
                    String tvNilai = value.getNilai();
                    String tvDate = value.getDate();
                    String tvTime = value.getTime();
                    phModel.setNilai(tvNilai);
                    phModel.setDate(tvDate);
                    phModel.setTime(tvTime);
                    dataph.add(phModel);
                }
                phAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                //Failed to read value
                Log.w("Hello", "Failed to read Ph.", databaseError.toException());

            }
        });

        cekdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phAdapter.notifyDataSetChanged();
            }
        });
    }

}

i tried to retrieving data from Firebase Realtime Database to Recycler-view. i already create Modul class called Ph.java and adapter called PhAdapter.java. but, i am so confused to call the adapter on MainActivity.java. here's my code :)

Ph.Class

public class Ph {

    public String date;
    public String nilai;
    public String time;

    public Ph(){
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getNilai() {
        return nilai;
    }

    public void setNilai(String nilai) {
        this.nilai = nilai;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

and here's my PhAdapter.java

public class PhAdapter extends RecyclerView.Adapter<PhAdapter.PhViewHolder> {

    private List<Ph> dataph;
    private Context context;

    public PhAdapter(List<Ph> dataph, Context context) {
        this.dataph = dataph;
        this.context = context;
    }

    @Override
    public PhViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
        View view = LayoutInflater.from(context).inflate(R.layout.view_ph_list,parent,false);
        PhViewHolder phViewHolder = new PhViewHolder(view);
        return new PhViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final PhViewHolder holder, final int position) {
        Ph mylist = dataph.get(position);
        holder.tvNilai.setText(mylist.getNilai());
        holder.tvTime.setText(mylist.getTime());
        holder.tvDate.setText(mylist.getDate());
    }

    @Override
    public int getItemCount() {
        int arr = 0;
        try{
            if(dataph.size()==0){
                arr = 0;
            }
            else{
                arr=dataph.size();
            }
        }catch (Exception e){
        }
        return arr;
    }

    public static class PhViewHolder extends RecyclerView.ViewHolder {

        private TextView tvNilai, tvDate, tvTime;

        public PhViewHolder(View itemview) {
            super(itemview);
            tvNilai = (TextView) itemview.findViewById(R.id.tv_ph);
            tvTime = (TextView) itemview.findViewById(R.id.tv_time);
            tvDate = (TextView) itemview.findViewById(R.id.tv_date);
        }
    }
}

and my MainActivity class

public class MainActivity extends AppCompatActivity {

    FirebaseDatabase database;
    DatabaseReference myRef;
    List<Ph> dataph;
    RecyclerView recyclerView;
    Button cekdata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cekdata = (Button) findViewById(R.id.btn_cekdata);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("ph");

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                dataph = new ArrayList<Ph>();
                for (DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
                    Ph value = dataSnapshot1.getValue(Ph.class);
                    Ph phModel = new Ph();
                    String tvNilai = value.getNilai();
                    String tvDate = value.getDate();
                    String tvTime = value.getTime();
                    phModel.setNilai(tvNilai);
                    phModel.setDate(tvDate);
                    phModel.setTime(tvTime);
                    dataph.add(phModel);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                //Failed to read value
                Log.w("Hello", "Failed to read Ph.", databaseError.toException());

            }
        });

        cekdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PhAdapter phAdapter = new PhAdapter(dataph,MainActivity.this);
                RecyclerView.LayoutManager recyce = new LinearLayoutManager(MainActivity.this);
                recyclerView.setLayoutManager(recyce);
                recyclerView.setAdapter(phAdapter);
            }
        });
    }

}

when i click the button, data doesn't show up on recyclerview and i got the error, RecyclerView: No layout manager attached; skipping layout.

解决方案

You need to intialize LinearLayoutManager completely as follows:

recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
RecyclerView.LayoutManager recyce = new
LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(recyce);
PhAdapter phAdapter = new PhAdapter(dataph,MainActivity.this);
recyclerView.setAdapter(phAdapter);

Your corrected Activity class should look like this:

public class MainActivity extends AppCompatActivity {

    FirebaseDatabase database;
    DatabaseReference myRef;
    List<Ph> dataph= new ArrayList<>();
    RecyclerView recyclerView;
    PhAdapter phAdapter;
    Button cekdata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cekdata = (Button) findViewById(R.id.btn_cekdata);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
        RecyclerView.LayoutManager recyce = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(recyce);
        phAdapter = new PhAdapter(dataph,MainActivity.this);
        recyclerView.setAdapter(phAdapter);


        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("ph");

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
                    Ph value = dataSnapshot1.getValue(Ph.class);
                    Ph phModel = new Ph();
                    String tvNilai = value.getNilai();
                    String tvDate = value.getDate();
                    String tvTime = value.getTime();
                    phModel.setNilai(tvNilai);
                    phModel.setDate(tvDate);
                    phModel.setTime(tvTime);
                    dataph.add(phModel);
                }
                phAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                //Failed to read value
                Log.w("Hello", "Failed to read Ph.", databaseError.toException());

            }
        });

        cekdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phAdapter.notifyDataSetChanged();
            }
        });
    }

}

这篇关于数据未显示在recyclerview上-出现错误未附加布局管理器;跳过布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:37