Я разрабатываю RecyclerView и использую CustomAdapter, и мое приложение работает нормально, но мой ListView не показывает его содержимое!что отсутствует или не так с моим кодом?Я попытался вернуть arraylist.size () и набрал list.setHasFixedSize, но ничего не получилось!
prospectowner_listview.java (класс активности)
public class prospectowner_listview extends AppCompatActivity {
RecyclerView list;
ArrayList <com.example.android.e7gzlykora.owner> ownerlist = new ArrayList <>();
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
owner owner;
customAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prospectowner_listview);
list = findViewById(R.id.list);
LinearLayoutManager manager = new LinearLayoutManager(this);
list.setLayoutManager(manager);
list.setHasFixedSize(true);
manager.setOrientation(LinearLayoutManager.VERTICAL);
adapter = new customAdapter(prospectowner_listview.this,ownerlist);
mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseDatabase = mFirebaseInstance.getReference("owners");
mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot data: dataSnapshot.getChildren()){
owner o = data.getValue(owner.class);
ownerlist.add(o);
}
list.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { }
});
}
}
CustomerAdapter.java
public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder>{
private final ArrayList<owner> ownerlist;
private final Context mContext;
public customAdapter(Context c , ArrayList<owner> p) {
this.mContext = c;
this.ownerlist = new ArrayList <>();
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.prospectowners,parent,false));
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
owner owner = ownerlist.get(position);
holder.name.setText(owner.getName());
holder.field.setText(owner.getFieldname());
holder.mobile.setText(owner.getMobile());
holder.address.setText(owner.getAddress());
holder.cost.setText(owner.getCost());
}
@Override
public int getItemCount() {
return ownerlist.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView field;
public TextView mobile;
public TextView address;
public TextView cost;
public MyViewHolder(View itemView) {
super(itemView);
this.name = itemView.findViewById(R.id.name);
this.field = itemView.findViewById(R.id.fieldName);
this.mobile = itemView.findViewById(R.id.mobileowner);
this.address = itemView.findViewById(R.id.address);
this.cost = itemView.findViewById(R.id.cost);
}
}
}
Owner.java
public class owner extends ArrayList <owner> {
public String name;
public String mobile;
public String fieldname;
public String address;
public String cost;
public String zone1;
public String zone2;
private Class thisClass;
public owner(){}
public owner(String name, String mobile,String fieldname,String address, String cost, String zone1, String zone2) {
this.name = name;
this.mobile = mobile;
this.fieldname = fieldname;
this.address = address;
this.cost = cost;
this.zone1 = zone1;
this.zone2 = zone2;
}
public String getMobile() {
return mobile;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setName(String name) {
this.name = name;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public void setFieldname(String fieldname) {
this.fieldname = fieldname;
}
public void setAddress(String address) {
this.address = address;
}
public void setCost(String cost) {
this.cost = cost;
}
public void setZone1(String zone1) {
this.zone1 = zone1;
}
public void setZone2(String zone2) {
this.zone2 = zone2;
}
public void setThisClass(Class thisClass) {
this.thisClass = thisClass;
}
public String getFieldname() {
return fieldname;
}
public String getCost() {
return cost;
}
public String getZone1() {
return zone1;
}
public String getZone2() {
return zone2;
}
}
------------- prospectowners.xml---------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<TextView
android:id="@+id/fieldName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@android:color/black"
android:textStyle="bold"
/>
<TextView
android:id="@+id/nameowner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/fieldName"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text=""
android:textColor="@android:color/black"
/>
<TextView
android:id="@+id/mobileowner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/nameowner"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
/>
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/mobileowner"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
/>
<TextView
android:id="@+id/cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/address"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:textColor="@android:color/black"
/>
<Button
android:id="@+id/reserve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cost"
android:onClick="reserve"
android:layout_marginStart="230dp"
android:layout_marginLeft="270dp"
android:layout_marginTop="10dp"
android:text="E7gzly"
android:textSize="15dp" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>