gpt4 book ai didi

java - ListView 第一项的值错误

转载 作者:行者123 更新时间:2023-12-01 12:45:06 28 4
gpt4 key购买 nike

我有一个由CursorAdapter填充的ListView。此 ListView 的每个项目都有 5 个子 View。我在 CursorAdapteronBind() 中设置了这些子 View 的值(我正在使用 ViewHolder图案)。这些值包括:电话号码、通话状态、评论数、通话持续时间、通话发生时间。如果通话丢失或掉线,我会将View 持续时间设置为INVISIBLE。当所有调用都“应答”时,一切都很好,但是,如果出现未接或掉线的调用,则 ListView 中的第一个项目,我总是重复,并且只有第一个项目具有持续时间 View 设置为 INVISIBLE 并对 View 的值进行计数。我不明白。所有其他第一项 View 均设置正确,但持续时间由于某种原因是错误的。请帮我解决这个问题。

这是我的 CursorAdaper 代码:

package com.example.nrsearch.adapter;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.nrsearch.R;
import com.example.nrsearch.Utils;

public class NumberListAdapter extends CursorAdapter {
private static final String KEY_NUMBER = "number";
private static final String KEY_STATE = "state";
private static final String KEY_COUNT = "count";
private static final String KEY_TIME = "time";
private static final String KEY_DURATION = "duration";

public NumberListAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}

private class ViewHolder {
private TextView numberView;
private ImageView stateView;
private TextView countView;
private TextView timeView;
private TextView durationView;
}

@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
Log.i(Utils.TAG, "newView() is called");
View view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.numberlistitem, parent, false);

ViewHolder holder = new ViewHolder();
holder.numberView = (TextView) view.findViewById(R.id.number);
holder.stateView = (ImageView) view.findViewById(R.id.state);
holder.countView = (TextView) view.findViewById(R.id.count);
holder.timeView = (TextView) view.findViewById(R.id.time);
holder.durationView = (TextView) view.findViewById(R.id.duration);

view.setTag(holder);

return view;
}

@Override
public void bindView(View view, Context context, Cursor c) {
Log.i(Utils.TAG, "bindView() is called");

ViewHolder holder = (ViewHolder) view.getTag();

holder.numberView.setText(c.getString(c.getColumnIndexOrThrow(KEY_NUMBER)));
int stateCode = c.getInt(c.getColumnIndexOrThrow(KEY_STATE));
if (stateCode == 0) holder.stateView.setBackgroundResource(R.drawable.red_arrow);
else holder.stateView.setBackgroundResource(R.drawable.green_arrow);

int count = c.getInt(c.getColumnIndexOrThrow(KEY_COUNT));
if (count == -2) holder.countView.setBackgroundResource(R.drawable.count_bubble_unknown);
else if (count == -1) holder.countView.setBackgroundResource(R.drawable.count_bubble_blocked);
else {
holder.countView.setBackgroundResource(R.drawable.count_bubble);
holder.countView.setText(""+count);
}

holder.timeView.setText(parseElapsedTime(c.getLong(c.getColumnIndexOrThrow(KEY_TIME)),
System.currentTimeMillis() / 1000L));


if (stateCode != 0) {
long duration = c.getLong(c.getColumnIndexOrThrow(KEY_DURATION));
holder.durationView.setText(parseDuration(duration));
} else holder.durationView.setVisibility(View.INVISIBLE);
}

private String parseElapsedTime(long pastTimeSecs, long presentTimeSecs) {
long elapsedTimeSecs = presentTimeSecs - pastTimeSecs;
if (elapsedTimeSecs >= 60L && elapsedTimeSecs < 3600L) {
return "Pries " + Math.round(elapsedTimeSecs / 60L) + " min.";
} else if (elapsedTimeSecs >= 3600L && elapsedTimeSecs < 86400L) {
return "Pries " + Math.round(elapsedTimeSecs / 3600L) + " val.";
} else if (elapsedTimeSecs >= 86400L && elapsedTimeSecs < 604800L) {
return "Pries " + Math.round(elapsedTimeSecs / 86400L) + " d.";
} else if (elapsedTimeSecs >= 604800L && elapsedTimeSecs < 2592000L) {
return "Pries " + Math.round(elapsedTimeSecs / 604800L) + " sav.";
} else if (elapsedTimeSecs >= 2592000L && elapsedTimeSecs < 31536000L) {
return "Pries " + Math.round(elapsedTimeSecs / 2592000L) + " men.";
} else if (elapsedTimeSecs >= 31536000L) {
return "Pries " + Math.round(elapsedTimeSecs / 31536000L) + " m.";
} else return "Pries " + Long.toString(elapsedTimeSecs) + " s.";
}

private String parseDuration(long timeSeconds) {
int hours = Math.round(timeSeconds / 3600);
int minutes = Math.round((timeSeconds - hours * 3600) / 60);
long seconds = timeSeconds - hours * 3600 - minutes * 60;

String h, min, s;

if (hours / 10 < 1)
h = "0" + hours;
else
h = "" + hours;
if (minutes / 10 < 1)
min = "0" + minutes;
else
min = "" + minutes;
if (seconds / 10 < 1)
s = "0" + seconds;
else
s = "" + seconds;

return h + ":" + min + ":" + s;
}

}

最佳答案

    if (stateCode != 0) {
long duration = c.getLong(c.getColumnIndexOrThrow(KEY_DURATION));
holder.durationView.setText(parseDuration(duration));
holder.durationView.setVisibility(View.VISIBLE)
} else{
holder.durationView.setVisibility(View.INVISIBLE);
}

尝试在 if 子句中将可见性设置为 VISIBLE。我最近遇到了一个问题。

关于java - ListView 第一项的值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24780497/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com