-1

I am developing RecyclerView and using CustomAdapter and my app runs smoothly but my ListView not showing it's content! what's missing or wrong with my code? I tried to return arraylist.size() and typed the list.setHasFixedSize and did not work !

prospectowner_listview.java (Activity class)

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>

4 Answers4

1

I think the problem coming from your adapter, change your adapter code like below:

public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder>{

    private ArrayList<owner> ownerlist;
    private Context mContext;

    public customAdapter(Context c , ArrayList<owner> p) {
        this.mContext = c;
        this.ownerlist = p;
    }

    @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 void addData(ArrayList<owner> newData){
        this.ownerlist.addAll(newData);
        notifyOnDataSetChanged();
    }

    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);
        }
    }
}

then call method addData like this:

@Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot data: dataSnapshot.getChildren()){
                    owner o = data.getValue(owner.class);
                    ownerlist.add(o);
                }
                list.setAdapter(adapter);
                adapter.addData(ownerlist);

            }

Btw,Actually you can set adapter outside listener

UPDATE (This update sets the position of the adapter value by default empty data, then it will filled when listener triggered)

public class customAdapter extends RecyclerView.Adapter<customAdapter.MyViewHolder>{

    private ArrayList<owner> ownerlist;
    private 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 void addData(ArrayList<owner> newData){
        this.ownerlist.addAll(newData);
        notifyOnDataSetChanged();
    }

    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);
        }
    }
}

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);
        list.setAdapter(adapter);

        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);
                }
                adapter.addData(ownerlist);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) { }
        });
    }
}
Nanda Z
  • 1,604
  • 4
  • 15
  • 37
  • It will throw NullPointer Exception I think because you didnot initiate the arraylist! evenif I initiate the arraylist it did not solve the issue and still my list of data did not appear – Ahmed Mamdouh Jan 29 '19 at 23:11
  • Do not initiate arraylist inside adapter eventhrough in constructor. Adapter only receive data and managing them all then giving callback to their caller. I just fixing your adapter like normal adapter. If adapter didnt give data ofcourse there are problem when you store data to them. OwnerList in Adapter will not throw you to nullPointer because you already pass by reference on Activity. Try to check your activity now and make sure data available in listener – Nanda Z Jan 30 '19 at 00:13
  • the problem with showing the view itself not only the data in listner ... the listView is not visible after running the app! I think it is still around the adapter! – Ahmed Mamdouh Jan 30 '19 at 01:16
  • Have you tried to print out logcat onDataChange? Does the data (owner) exist? – Nanda Z Jan 30 '19 at 01:49
  • I applied Log.d("Value: " ,String.valueOf(ownerlist)); and nothing appeard from the data! why? – Ahmed Mamdouh Jan 30 '19 at 14:49
  • ownerlist is an arraylist, if you print out it it only showing you memory address. try to print out every single object of "owner", but create method "toString()" on owner object first . You can learn from this link [toString()](https://www.dummies.com/programming/java/use-javas-tostring-method/) – Nanda Z Jan 31 '19 at 01:46
  • I print them out and all of the data appeared in the log but did not appear while running on the screen! I am more confused now! how can I solve it? – Ahmed Mamdouh Jan 31 '19 at 23:11
  • i created new updated on my Answer, please check it, probably its works – Nanda Z Feb 01 '19 at 01:21
  • Where are the updated codes? I can not see it in the updated section! – Ahmed Mamdouh Feb 01 '19 at 01:42
  • how can I do that? how I can set position value to emtpy default data? – Ahmed Mamdouh Feb 01 '19 at 02:25
  • there is method named notifyOnDataSetChanged(), it will refresh your adapter whenever you calling – Nanda Z Feb 01 '19 at 02:27
  • Still nothing new happen! still after run app,,,,, only white empty screen without my recycleview! – Ahmed Mamdouh Feb 01 '19 at 02:48
  • i just added fork to your project, please check is it work now? ( in your girhub) – Nanda Z Feb 01 '19 at 03:52
  • Omg, ofcourse it not working, please fix this part notifyDataSetChanged(); do not create new method, just call it on public void addData(ArrayList newData){ this.ownerlist.addAll(newData); notifyOnDataSetChanged(); } you create it with empty code, of course it does nothing – Nanda Z Feb 01 '19 at 04:08
  • No still not working ..... regards the part of getValue , the part is giving value .... the issue is that when run the app .... the screen doesn't show anything from the recycleview itself not only the data .... only empty screen !! – Ahmed Mamdouh Feb 01 '19 at 04:12
  • please fix this part notifyDataSetChanged(); do not create new method, just call it on public void addData(ArrayList newData){ this.ownerlist.addAll(newData); notifyOnDataSetChanged(); } you create it with empty code, of course it does nothing. I dont fix that part – Nanda Z Feb 01 '19 at 04:12
  • I fixed it and in my app tool window, it shows the activity of the data retrieving from database but still screen not showing anything just white screen! – Ahmed Mamdouh Feb 01 '19 at 04:37
  • please commit your update snippet code, let me check it later – Nanda Z Feb 01 '19 at 10:18
  • Did you find anything? Really I am stuck in this step! – Ahmed Mamdouh Feb 03 '19 at 00:44
  • Sorry i bit busy, i will check it later. Seems you should trying renew your architevture. It could make someone confusing, like create object with Capital first. – Nanda Z Feb 03 '19 at 05:45
  • Also dont make class with snakecase, it's only for method – Nanda Z Feb 03 '19 at 06:10
  • Hey, please check it now – Nanda Z Feb 04 '19 at 01:17
  • I checked it and after run my app, on the screen I can pull and nestedscroll but no recyclerview showed ..... like there is recycleview but hidden!! – Ahmed Mamdouh Feb 04 '19 at 05:32
  • you will find it in my post here! both xml files – Ahmed Mamdouh Feb 04 '19 at 13:21
  • You have to make experiments for UI problems, since i can't compile your code. Please submit answer if your main problem solved, and make new questions for new issue – Nanda Z Feb 05 '19 at 11:13
  • It did not solve my main problem still which is that my recyclerview is not showed after run! – Ahmed Mamdouh Feb 06 '19 at 22:00
  • try using dummy data which it source not from firebase, and also try implement tools:listitem="@layout/item" in your Recylerview on your layout and see dummy text inside is showing or not. this is simplest way to check UI problem – Nanda Z Feb 07 '19 at 01:58
  • See [this](https://stackoverflow.com/questions/29929963/is-there-a-way-to-show-a-preview-of-a-recyclerviews-contents-in-the-android-stu) – Nanda Z Feb 07 '19 at 02:24
0

Create new function in your adapter:

public void addData(List<Owner> owners){
 this.ownerList = owners;
 notifyOnDataSetChanged();
}

and in onDataChange call :

adapter.addData(owners);
Maksim Novikov
  • 835
  • 6
  • 18
  • I did that but it did not solve the issue, I create this new function in adapter and then put method.addData in my onDataChange call in Activity.class .... and it throw this message "W/dalvikvm: VFY: unable to resolve interface method 22446: Landroid/view/ViewParent;.onStopNestedScroll (Landroid/view/View;)V" ... is there something wrong in onBindViewHolder ?? – Ahmed Mamdouh Jan 29 '19 at 15:46
  • I am really stuck in this step! any other suggestions? – Ahmed Mamdouh Feb 03 '19 at 00:45
0

Array list always empty because int the adapter constructor you initialize as new list. Please check it out

enter image description here

Farid Haq
  • 3,728
  • 1
  • 21
  • 15
-1

Try this,

In your activity class change

list.setAdapter(adapter)

to

list.setAdapter(new customAdapter(prospectowner_listview.this, ownerlist));
M Umer
  • 353
  • 6
  • 13
  • Do not create new CustomAdapter() each time the data chaged. Need to update adapter value and call adapter.notifyOnDataSetChaged() – Maksim Novikov Jan 29 '19 at 14:21
  • it did not solve my issue, is thereanything wrong with my onBindViewHolder? and my method.setText?? – Ahmed Mamdouh Jan 29 '19 at 15:48
  • Actually it's not generally wrong, but in this case it's wrong, because we do not know when the listener will triggered value, and everytime listener triggered you will creating new adapter objec, thats point make it wrong. I think someone downvoted because of this – Nanda Z Jan 31 '19 at 01:53