博士班
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:未附加布局管理器;跳过布局。
答案 0 :(得分:1)
您需要完全按照以下步骤初始化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();
}
});
}
}
答案 1 :(得分:1)
只需在您的回收站视图上设置布局管理器即可。
recyclerView.setLayoutManager(new LinearLayoutManager(this));
答案 2 :(得分:0)
初始化时需要附加layoutManager。
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
答案 3 :(得分:0)
有两种方法可以将布局管理器设置为回收站视图。简单来说,LinearLayoutManager告诉recyclerView以LinearLayout
的方式创建元素列表。
JAVA
recyclerView.setLayoutManager(new LinearLayoutManager(this));
科特琳
recyclerView.layoutManager = LinearLayoutManager(this)
布局
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
/>
您可以使用其中任何一个。
好运:)