Home > Enterprise >  Why ArrayList is always same value in only Adapter?
Why ArrayList is always same value in only Adapter?

Time:09-22

This is the worst bug I have ever seen.. I was fetching CallLogs following way.

ArrayList<HashMap<String, String>> callLog= new ArrayList<>();
        HashMap<String, String> data = new HashMap<>();
        Cursor c = activity.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE   " DESC ");
        String phoneNumber,type,date,duration,name;

        while(c.moveToNext()){
            phoneNumber = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));
            type = c.getString(c.getColumnIndex(CallLog.Calls.TYPE));
            date = c.getString(c.getColumnIndex(CallLog.Calls.DATE));
            duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));
            name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));

            data.put(Constants.PHONE_NUMBER, phoneNumber);
            data.put(Constants.NAME, name);
            data.put(Constants.DATE, date);
            data.put(Constants.TYPE, type);
            data.put(Constants.DURATION, duration);

            callLog.add(data);
        }
        c.close();
        return callLog;

I was trying to set it to Adapter. Here how I had linked them to adapter

ContactViewAdapter contactViewAdapter = new ContactViewAdapter(getActivity(),getActivity(),CallLog(getActivity()),"contact");
        recyclerViewCallLog.setAdapter(contactViewAdapter);

Since Adapter class is little bit bigger hence I am not adding all context. I am just adding constructor here.

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

Context context;
    ArrayList<HashMap<String, String>> list;
    String type;
    Activity activity;

    public ContactViewAdapter(Activity activity,Context context, ArrayList<HashMap<String,String>> list,String type){
        this.activity = activity;
        this.context = context;
        this.list = list;
        this.type = type;
    }

This is understandable for an Android App developer. The bug is listed below :

  • I am actually using multiple RecyclerView using the Adapter. Their layout is closely same that's why I chose to use single Adapter that could decrease amount of source code. It was working fine for Contacts. But the bug is available in CallLog.
  • The method can return all call logs. In DialerFragment (where I am calling that method), I can see all CallLog either. When the list gets to Adapter then only single CallLog is returning.

I am removing phoneNumber from Logcat list.

[{date=123123, name=null, phoneNumber= 4365643, duration=45, type=1}, {date=43743, name=null, phoneNumber= 4543, duration=45, type=1}, {date=23452, name=null, phoneNumber= 3245432, duration=45, type=1}, {date=234523, name=null, phoneNumber= 3245432, duration=45, type=1}, {date=1617550988892, name=null, phoneNumber= 2345, duration=45, type=1}, {date=1617550923452388892, name=null, phoneNumber= 88018115723457032, duration=45, type=1}, {date=16175502345988892, name=null, phoneNumber= 88018234511577032, duration=45, type=1}, {date=16175509888234592, name=null, phoneNumber= 88018112345577032, duration=45, type=1}, {date=16172345550988892, name=null, phoneNumber= 88018112345577032, duration=45, type=1}, {date=16175234550988892, name=null, phoneNumber= 88018234511571237032, duration=45, type=1}

The logcat is from the method. I get the same log in fragment (activity) page either. Logcat in Adapter.

2021-09-19 14:00:07.995 20558-20558/com.contact D/LOGCAT: {date=1617550988892, name=null, phoneNumber= 88018115747032, duration=45, type=1}
2021-09-19 14:00:08.012 20558-20558/com.contact D/LOGCAT: {date=1617550988892, name=null, phoneNumber= 88018115747032, duration=45, type=1}
2021-09-19 14:00:08.026 20558-20558/com.contact D/LOGCAT: {date=1617550988892, name=null, phoneNumber= 88018115747032, duration=45, type=1}
2021-09-19 14:00:08.040 20558-20558/com.contact D/LOGCAT: {date=1617550988892, name=null, phoneNumber= 88018115747032, duration=45, type=1}
2021-09-19 14:00:08.054 20558-20558/com.contact D/LOGCAT: {date=1617550988892, name=null, phoneNumber= 88018115747032, duration=45, type=1}
2021-09-19 14:00:08.068 20558-20558/com.contact D/LOGCAT: {date=1617550988892, name=null, phoneNumber= 88018115747032, duration=45, type=1}
2021-09-19 14:00:08.082 20558-20558/com.contact D/LOGCAT: {date=1617550988892, name=null, phoneNumber= 88018115747032, duration=45, type=1}
2021-09-19 14:00:08.098 20558-20558/com.contact D/LOGCAT: {date=1617550988892, name=null, phoneNumber= 88018115747032, duration=45, type=1}
2021-09-19 14:00:08.112 20558-20558/com.contact D/LOGCAT: {date=1617550988892, name=null, phoneNumber= 88018115747032, duration=45, type=1}
2021-09-19 14:00:08.126 20558-20558/com.contact D/LOGCAT: {date=1617550988892, name=null, phoneNumber= 88018115747032, duration=45, type=1}
2021-09-19 14:00:08.140 20558-20558/com.contact D/LOGCAT: {date=1617550988892, name=null, phoneNumber= 88018115747032, duration=45, type=1}
2021-09-19 14:00:08.154 20558-20558/com.contact D/LOGCAT: {date=1617550988892, name=null, phoneNumber= 88018115747032, duration=45, type=1}

don't care of DATE. They are in correct format. They are in miliseconds. don't care of number either. I have added a number (character) in those numbers. Here how I have called it.

@Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        String phoneNumber = list.get(position).get(Constants.PHONE_NUMBER);
        String name = list.get(position).get(Constants.NAME);
        Bitmap imgBitmap = null;
        String photo = null;

        Log.d("LOGCAT", String.valueOf(list.get(position)));
}

But why they are returning same value each time?

CodePudding user response:

Instead of:

ArrayList<HashMap<String, String>> callLog= new ArrayList<>();
    HashMap<String, String> data = new HashMap<>();
    Cursor c = activity.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE   " DESC ");
    String phoneNumber,type,date,duration,name;

    while(c.moveToNext()){
        phoneNumber = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));
        type = c.getString(c.getColumnIndex(CallLog.Calls.TYPE));
        date = c.getString(c.getColumnIndex(CallLog.Calls.DATE));
        duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));
        name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));

        data.put(Constants.PHONE_NUMBER, phoneNumber);
        data.put(Constants.NAME, name);
        data.put(Constants.DATE, date);
        data.put(Constants.TYPE, type);
        data.put(Constants.DURATION, duration);

        callLog.add(data);
    }
    c.close();
    return callLog;

try :

ArrayList<HashMap<String, String>> callLog= new ArrayList<>();
    Cursor c = activity.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE   " DESC ");
    String phoneNumber,type,date,duration,name;

    while(c.moveToNext()){
        phoneNumber = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));
        type = c.getString(c.getColumnIndex(CallLog.Calls.TYPE));
        date = c.getString(c.getColumnIndex(CallLog.Calls.DATE));
        duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));
        name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));

        HashMap<String, String> data = new HashMap<>();
        data.put(Constants.PHONE_NUMBER, phoneNumber);
        data.put(Constants.NAME, name);
        data.put(Constants.DATE, date);
        data.put(Constants.TYPE, type);
        data.put(Constants.DURATION, duration);

        callLog.add(data);
    }
    c.close();
    return callLog;
  • Related