为什么 ArrayList 在 Adapter 中总是相同的值?

Why ArrayList is always same value in only Adapter?

这是我见过的最严重的错误。我正在按照以下方式获取呼叫日志。

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;

我试图将其设置为适配器。在这里我如何将它们链接到适配器

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

由于适配器 class 有点大,因此我没有添加所有上下文。我只是在这里添加构造函数。

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;
    }

Android 应用程序开发人员可以理解这一点。下面列出了错误:

我正在从 Logcat 列表中删除 phoneNumber。

[{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}

logcat来自方法。我也得到相同的登录片段 (activity) 页面。 Logcat 在适配器中。

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}

不关心 DATE。它们的格式正确。它们以毫秒为单位。也不在乎数字。我在这些数字中添加了一个数字(字符)。我是这样称呼它的。

@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)));
}

但为什么它们每次都return输入相同的值?

而不是:

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;

尝试:

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;