0

I have a recyclerview that yields all contacts and each contact's phone numbers if they have more than one. When I test my code, in my logcat, I get all the phone numbers. For instance:

I/System.out: name : John Smith, ID : 1
I/System.out: 018-65 5678
I/System.out: 597-676-643-64

However, when I look at my emulator, only the last of these two numbers is displayed. This is the case for all my contacts, only the last of several numbers is displayed.

I tried modifying my xml layout to make space for all numbers by adding:

android:maxLines="4"
android:lines="4"

to

            <TextView
            android:id="@+id/textViewNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textViewName"
            android:layout_marginStart="10dp"
            android:padding="5dp"
            android:text="@string/numbers" />

But the result is the same.

I tried looping through the phone numbers before setting them, but the result is the same.

        if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            listItem = new ListItem(name, id);
            listItem.setName(name);

            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                System.out.println("name : " + name + ", ID : " + id);
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
                while (pCur.moveToNext()) {
                    phones = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    System.out.println(phones);

                    for (int i = 0; i <phones.length() ; i++) {
                        listItem.setNumber(phones);
                    }


                }
                pCur.close();
                listItems.add(listItem);
            }
        }
    }

Here is part of my Adapter class:

    @Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ListItem listItem = listItems.get(position);

    holder.nameTextView.setText(listItem.getName());
    holder.numberTextView.setText(listItem.getNumber());
}

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

public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView nameTextView;
    public TextView numberTextView;

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

        nameTextView = itemView.findViewById(R.id.textViewName);
        numberTextView = itemView.findViewById(R.id.textViewNumber);
    }
}

I'd be grateful for some pointers.

This is my whole layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="0dp"
    android:layout_marginTop="0dp"
    app:cardElevation="2dp">

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textViewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:padding="5dp"
            android:text="@string/name"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Switch
            android:id="@+id/switch1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:padding="5dp" />

        <TextView
            android:id="@+id/textViewNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textViewName"
            android:layout_marginStart="10dp"
            android:padding="5dp"
            android:text="@string/numbers" />

    </RelativeLayout>
</android.support.v7.widget.CardView>

Code Poet
  • 6,222
  • 2
  • 29
  • 50

2 Answers2

0

When you are getting the phones from the DB you are setting multiple phone numbers to a single "number" var. so only the last number will actually be set

for (int i = 0; i <phones.length() ; i++) {
    listItem.setNumber(phones);
}

add an additional number var on your listItem and show it in case it's not empty

Also, there's no reason to iterate over it as you are iterating over a string... meaning you are just setting the number for as many times as the number has digits

Yuxal
  • 445
  • 4
  • 14
0

you need to change your modal class ListItem To something like this if you want to store multiple phone numbers

public class ListItem{

private String name;
private List<String> phones;

ListItem(name,phones){
    this.name=name;
    this.phones = phones;
}

public String getName(){
    return name;
}
public List<String> getPhones(){
    return phones;
}
}

And then should try to store each number in the phones list in loop.

Edit : Use https://stackoverflow.com/a/15243473/3758972 for storing phones in list.

Nitesh Verma
  • 2,460
  • 4
  • 20
  • 36