1

I tried for many hours to solve the following error: "W/RecyclerView: No adapter attached; skipping layout" Unfortunately, I can't get the solution can you please help me. The App I try to make is from this video.

Here is my code to the adapter:

package com.example.whatsappclone;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.UserListViewHolder> {

ArrayList<UserObject> userList;

public UserListAdapter(ArrayList<UserObject> userList){
    this.userList = userList;

}

@NonNull
@Override
public UserListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_user, null, false);
    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutView.setLayoutParams(lp);
    UserListViewHolder rcv = new UserListViewHolder(layoutView);
    return rcv;
} 

public void onBindViewHolder(@NonNull UserListViewHolder holder, int position) {
        //Here i try to get the name and phonenumber for my Whatsappclone app
holder.mName.setText(userList.get(position).getName());
    holder.mPhone.setText(userList.get(position).getPhone());

}

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


public class UserListViewHolder extends RecyclerView.ViewHolder{
    public TextView mName, mPhone;
    public UserListViewHolder(View view){
        super(view);
        mName = view.findViewById(R.id.name);
        mPhone = view.findViewById(R.id.phone);
    }
}

}

Here ist my Code to my XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="contact"
    android:id="@+id/name"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="phone"
    android:id="@+id/phone"/>
</LinearLayout>

Here is my activity where i initialize the recyclerview (Look in OnCreateMethod):

package com.example.whatsappclone;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;
import android.widget.LinearLayout;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class FindUserActivity extends AppCompatActivity {

private RecyclerView mUserList;
private RecyclerView.Adapter mUserListAdapter;
private RecyclerView.LayoutManager mUserListLayoutManager;

ArrayList<UserObject> userList, contactList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_find_user);

    contactList= new ArrayList<>();
    userList= new ArrayList<>();

    initializeRecyclerView();
    getContactList();


}
private void getContactList(){
    String ISOPrefix = getCountryISO();
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
    while(phones.moveToNext()){
        String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        phone = phone.replace(" ","");
        phone = phone.replace("-","");
        phone = phone.replace("(","");
        phone = phone.replace(")","");

        if(!String.valueOf(phone.charAt(0)).equals("+"))
            phone = ISOPrefix + phone;

        UserObject mContact = new UserObject(name, phone);
        contactList.add(mContact);
        getUserDetails(mContact);
    }
}

private void getUserDetails(UserObject mContact) {
    DatabaseReference mUserDB = FirebaseDatabase.getInstance("https://whatsappclone-3cc31-default-rtdb.europe-west1.firebasedatabase.app/").getReference().child("user");
    Query query = mUserDB.orderByChild("phone").equalTo(mContact.getPhone());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                String phone = "",
                       name = "";
                for(DataSnapshot childSnapshot: dataSnapshot.getChildren()){
                    if(childSnapshot.child("phone").getValue()!=null)
                        phone = childSnapshot.child("phone").getValue().toString();
                    if(childSnapshot.child("name").getValue()!=null)
                        phone = childSnapshot.child("name").getValue().toString();


                    UserObject mUser = new UserObject(name, phone);
                    userList.add(mUser);
                    mUserListAdapter.notifyDataSetChanged();
                    return;
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

private String getCountryISO(){
    String iso = null;

    TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(getApplicationContext().TELEPHONY_SERVICE);
    if(telephonyManager.getNetworkCountryIso()!=null)
        if(!telephonyManager.getNetworkCountryIso().toString().equals(""))
            iso = telephonyManager.getNetworkCountryIso().toString();


    return CountryToPhonePrefix.getPhone(iso);
}
private void initializeRecyclerView() {
    mUserList= findViewById(R.id.userList);
    mUserList.setNestedScrollingEnabled(false);
    mUserList.setHasFixedSize(false);
    mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), RecyclerView.VERTICAL,false);
    mUserList.setLayoutManager(mUserListLayoutManager);
    mUserListAdapter = new UserListAdapter(userList);
    mUserList.setAdapter(mUserListAdapter);
}
 }
Mainfeld
  • 23
  • 4

0 Answers0