Android - ArrayAdapter 混合位置?

Android - ArrayAdapter mixed positions?

TL;DR - 我有一个数组适配器,由用户对象的 ArrayList 填充, 我想打印他们的订单号——第一、第二、第三……等等 - 来回快速滚动时..这些数字混合在一起..

例如,快速来回滚动后我得到的不是 1,2,3,4,而是 1,2,9,4..

我不明白为什么会发生这种情况,我已经尝试了几种方法来解决它但没有成功,我什至从视图持有者中取出该文本视图以查看它是否有帮助但也没有成功。

这是我的适配器:

public class HomeHallOfFameAdapter extends ArrayAdapter<User> {

private HomeFragment fragment;
private ArrayList<User> data;
int page;

public HomeHallOfFameAdapter(Context context, ArrayList<User> data, HomeFragment fragment, int page) {
    super(context, 0);
    this.data = data;
    this.fragment = fragment;
    this.page = page;
}

@Override
public int getCount() {
    return data != null ? data.size() : 0;
}

@Override
public User getItem(final int position) {
    return data.get(position);
}

@Override
public long getItemId(final int position) {
    //only used for adapters connected to a cursor.
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    MyHolder holder = null;

    final User user = data.get(position);

    //initiallize the views
    if (convertView == null) {

        convertView = LayoutInflater.from(getContext()).inflate(R.layout.home_frag_hall_of_fame_item, null);
        holder = new MyHolder();

        holder.profileImage1 = (ImageView) convertView.findViewById(R.id.home_hallofame_image);

        convertView.setTag(holder);

    } else {
        holder = (MyHolder) convertView.getTag();
    }

    final TextView titleRank = (TextView) convertView.findViewById(R.id.home_hallofame_title);
    final String numberEnding = MainScreenActivity.getInstance().NumberWithSuffix(position+1);
    titleRank.setText(numberEnding);

    loadImage(user.getImages().get(0), holder.profileImage1);

    return convertView;
    //return super.getView(position, convertView, parent);
}

private class MyHolder {
    //TextView titleRank;
    ImageView profileImage1; //fameBlueBtn;
}

}

问题出在 titleRank textView..

NumberWithSuffix 只是一个实现数字结尾的方法:

//method to get the currect number endings.
public String NumberWithSuffix(int number) {

    String outputNumber = "";
    String numberString = String.valueOf(number);

    if (String.valueOf(number).endsWith("11")) {
        outputNumber = numberString + "th";
    } else if (String.valueOf(number).endsWith("12")) {
        outputNumber = numberString + "th";
    } else if (String.valueOf(number).endsWith("13")) {
        outputNumber = numberString + "th";
    } else if (String.valueOf(number).endsWith("1")) {
        outputNumber = numberString + "st";
    } else if (String.valueOf(number).endsWith("2")) {
        outputNumber = numberString + "nd";
    } else if (String.valueOf(number).endsWith("3")) {
        outputNumber = numberString + "rd";
    } else {
        outputNumber = numberString + "th";
    }
    return outputNumber.trim();
}

知道我的问题是什么吗?

感谢您的帮助。

我只想使用 BaseAdapter 和 2 个数据集(或者您需要更多),然后在 getView() 中按数据集的类型混合它们...

你有这个

MainScreenActivity.getInstance()

我假设您在这里实例化了一个 Activity class。我建议您将 NumberWithSuffix(int number) 移动到适当的 Utility class 使方法静态,您可以在其他地方使用相同的方法。您所有的实用程序都不应该在 Activity 中。还要注意保持对 activity 的引用并避免内存泄漏

你的项目位置似乎没问题。我测试了你的样本,它工作正常。

您可以进行一些更改以遵循如下的 viewholder 模式

@Override
public View getView(final int position, View convertView, ViewGroup parent)  {

MyHolder holder = null;

final User user = data.get(position);

//initiallize the views
if (convertView == null) {

    convertView = LayoutInflater.from(getContext()).inflate(R.layout.home_frag_hall_of_fame_item, null);
    holder = new MyHolder();

    holder.profileImage1 = (ImageView) convertView.findViewById(R.id.home_hallofame_image);
    holder.titleRank = (TextView) convertView.findViewById(R.id.home_hallofame_title);

    convertView.setTag(holder);

} else {
    holder = (MyHolder) convertView.getTag();
}
final String numberEnding = Utility.NumberWithSuffix(position+1);
holder.titleRank.setText(numberEnding);

loadImage(user.getImages().get(0), holder.profileImage1);

return convertView;

}

ViewHolder

private static class MyHolder {
    TextView titleRank;
    ImageView profileImage1; //fameBlueBtn;

}

创建实用程序class

public class Utility {

    public static String NumberWithSuffix(int number) {

        String outputNumber = "";
        String numberString = String.valueOf(number);

        if (String.valueOf(number).endsWith("11")) {
            outputNumber = numberString + "th";
        } else if (String.valueOf(number).endsWith("12")) {
            outputNumber = numberString + "th";
        } else if (String.valueOf(number).endsWith("13")) {
            outputNumber = numberString + "th";
        } else if (String.valueOf(number).endsWith("1")) {
            outputNumber = numberString + "st";
        } else if (String.valueOf(number).endsWith("2")) {
            outputNumber = numberString + "nd";
        } else if (String.valueOf(number).endsWith("3")) {
            outputNumber = numberString + "rd";
        } else {
            outputNumber = numberString + "th";
        }
        return outputNumber.trim();
    }
}

也代替这个

final User user = data.get(position);

您可以使用

final User user = getItem(position); 

因为你已经有了

@Override
public User getItem(final int position) {
return data.get(position);
}

所以..我已经解决了这个问题.. 我的代码没问题,正如我提到的,它在垂直列表视图中工作正常,但这个问题发生在水平列表视图中。 我删除了我正在使用的列表视图 github 库并切换到 Horizo​​ntal Recycler View.. 现在效果很好!