0

I have two ArrayLists from which I am trying to insert data into separate static ArrayList in another class and display in RecyclerView, but the recycler is not getting populated though the same was being done with a dummy ArrayList.
Please help me with this problem.

My Class where I am inserting data from phoneContactNos and phoneContactName in two separate ArrayList: Common.selectedContactNos and Common.selectedContactName.

    public void displayMatchedContacts()
{
    try {
        for (int i = 1; i < phoneContactNos.size(); i++) {
            if (phoneContactNos.contains(registeredContactNos.get(i))) {
                if (registeredContactNos.get(i) != null) {
                    try {
                        indexOfRegNumber = phoneContactNos.indexOf(registeredContactNos.get(i));
                        //Common.indexPosition_contacts=indexOfRegNumber;
                        Toast.makeText(this, "index" + String.valueOf(indexOfRegNumber), Toast.LENGTH_LONG).show();
                        if ((phoneContactNos.get(indexOfRegNumber) != null) &&(phoneContactName.get(indexOfRegNumber) != null)) {

                            //String regName="";
                            //String regContact="";
                            Common.selectedContactNos.add(phoneContactNos.get(indexOfRegNumber));
                            //Toast.makeText(this,selectedContactNos.get(i).toString(),Toast.LENGTH_SHORT).show();
                            //Toast.makeText(this,phoneContactNos.get(indexOfRegNumber).toString(),Toast.LENGTH_SHORT).show();
                            Common.selectedContactName.add(phoneContactName.get(indexOfRegNumber));
                            //Toast.makeText(this,selectedContactName.get(i).toString(),Toast.LENGTH_SHORT).show();
                            //Toast.makeText(this, phoneContactName.get(indexOfRegNumber).toString(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(this, "null index no", Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        Log.i("Contacts display error", e.getLocalizedMessage());
                        e.printStackTrace();
                    }

                }
            }

        }



    }catch (Exception e)
    {
        Log.i("Contacts error in loop", e.getLocalizedMessage());
        e.printStackTrace();
    }


}

My Common class

public final  class Common {
public static ArrayList<String> selectedContactNos=new ArrayList<>();
public static ArrayList<String> selectedContactName=new ArrayList<>();
public  static String fcmId="";
public static int position;
public  static String contacts_list="";
public static int indexPosition_contacts;

}

My RecyclerView populating code

   public void populateList() {
  Log.i("Populate List","Entered");
    LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
    recyclerView_contacts.setLayoutManager(mLinearLayoutManager);
    displayRecyclerAdapter = new DisplayRecyclerAdapter(this);
    recyclerView_contacts.setAdapter(displayRecyclerAdapter);
}

My Adapter Class

public class DisplayRecyclerAdapter extends RecyclerView.Adapter<DisplayRecyclerAdapter.displayViewHolder> {
private LayoutInflater mInflater;
private Context context;
Fragment fragment;
FragmentTransaction ft;
FrameLayout container;
public DisplayContacts displayContacts;



public DisplayRecyclerAdapter(Context context) {
    this.mInflater = LayoutInflater.from(context);
    this.context = context;
    ft = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
    //displayContacts = new DisplayContacts();
}

@Override
public displayViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = mInflater.inflate(R.layout.contacts_row, parent, false);
    displayViewHolder holder = new displayViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(displayViewHolder holder, int position) {
    holder.setData(position);
    //Common.position = position;
    holder.setListeners();
    //for(int i=0;i<((DisplayContacts)context).selectedContactName.size();i++)
        for(int i=0;i<Common.selectedContactName.size();i++)
    {
        //String contactName=((DisplayContacts)context).selectedContactName.get(i);
        String contactName=Common.selectedContactName.get(i);
        //String contactNumber=((DisplayContacts)context).selectedContactNos.get(i);
        String contactNumber=Common.selectedContactNos.get(i);
        Toast.makeText(context,contactName+","+contactNumber,Toast.LENGTH_SHORT).show();
    }
}


class displayViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    int position;
    //ImageView productSearchImg;
    TextView name_contactList;
    Button call_contact;

    public displayViewHolder(View itemView) {
        super(itemView);

        name_contactList = (TextView) itemView.findViewById(R.id.contactlist_name);
        call_contact = (Button) itemView.findViewById(R.id.contactlist_call);

    }

    public void setData(int position) {
        this.position = position;
        //String displayContacts=((DisplayContacts) context).selectedContactName.get(position);
        String displayContacts=Common.selectedContactName.get(position);
        Toast.makeText(context,"name to display"+ displayContacts,Toast.LENGTH_SHORT).show();
        //name_contactList.setText(((DisplayContacts) context).selectedContactName.get(position));
        //name_contactList.setText(displayContacts);
        name_contactList.setText("dummy text");
    }

    @Override
    public void onClick(View v) {
        //sendPushNotification();
        startAudioCall();
    }


    public void setListeners() {
        call_contact.setOnClickListener(displayViewHolder.this);
    }
}

public void startAudioCall() {
    Intent i = new Intent(context, AudioCallActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i);
}

@Override
public int getItemCount() {
    return ((DisplayContacts) context).selectedContactName.size();
}

}
user8601021
  • 219
  • 3
  • 18
  • are you sure you are using the updated lists in your adapter class? post the adapter class – himel Nov 08 '17 at 11:43
  • I have updated my answer with my adapter class.I tried with dummy text which was earlier showing but now even that is not displaying.data is getting inserted in both the Common class Arraylist as perthe debugger – user8601021 Nov 08 '17 at 11:52

1 Answers1

0

can be one of two things: 1.you are not attaching the adapter with data,hence empty 2.or you are updating your data but not calling notifyDataSetChanged()

try to pass the list data in your Adapter class while you are creating it,then attach it to the recyelerview.

    //to give a vary basic example

        List dataList;
        RvAdapter(Context c,List data){
    this.dataList=data;
        }

        onBidViewHolder(Viewholder holder,int position){
        holder.tvName.setText(dataList.get(position).getName());
    .......
        }

    //in your activity/fragment

    RvAdapter adapter=new RavAdapter(context,dataList);
    recylerview.setAdapter(adapter);
//if you change your data 
adapter.notifyDataSetChanged()
himel
  • 500
  • 5
  • 14
  • can u please write the complete code including this line to populate the adapter class – user8601021 Nov 08 '17 at 11:59
  • check if you are correctly initialized the adapter or not, also the code avobe is a vary vague example , you should change/modify as your need. – himel Nov 08 '17 at 12:11
  • this link says I should set the adapter on onCreate but I first have to fetch data and then populate the adapter https://stackoverflow.com/questions/43730887/android-recyclerview-adapter-is-giving-null-on-unit-testing – user8601021 Nov 08 '17 at 12:25