Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Why ArrayList is always same value in only Adapter?

+1
−0

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?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+0
−0

Each list index refers to the same HashMap which it just override the key's value with each iteration of your loop, losing any previously set values. So if I put the HashMap value inside loop then it works fine. So the code will look like

while(c.moveToNext()){
HashMap<String, String> data = new HashMap<>();
            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);
        }
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »