android ListView ViewHolder 和类型

发布于 2024-12-05 15:11:18 字数 1449 浏览 0 评论 0原文

我想要一个 Listview 适配器,其中每行都使用它自己的 ConvertView, 所以这个convertview无法共享到另一行。 基本上,如果我有 4 行,我想要 4 个 ConvertView。 当绘制行#3时,我应该得到convertview#3

为此,我将每一行设置为不同的类型(它的位置) 我以为这样就可以了。

public class myadapter extends BaseAdapter {
final List<String> mdata;

@Override
public int getCount() {
    return mdata.size();
}

@Override
public Object getItem(int p) {
    return p;
}

@Override
public long getItemId(int p) {
    return p;
}

@Override
public int getItemViewType(int p) {
    return p;
}

@Override
public int getViewTypeCount() {
    return mdata.size()  == 0  ? 1 :mdata.size() + 1;
}

@Override
public View getView(int p, View v, ViewGroup arg2) {
    ViewHolder holder = null;
    if (v == null) {
        v = li.inflate(R.layout.data, null);
        holder = new ViewHolder();
        holder._pos = p;
        Log.v("TAG", "creating holder pos " + p + " for " + mdata.get(p));

        {...}

        v.setTag(holder);
    } else {
        holder = (ViewHolder)v.getTag();
        Log.d("TAG", "get convert for " + mdata.get(p)+ " holder " + holder._pos);

    }
    {...}
    return v;
}
public static class ViewHolder {
    public TextView _textView;
    public int _pos;
}
}

请注意,视图保持器中的 pos 仅用于调试目的。

不幸的是它显示:

为 string0 创建持有者 pos 0 为 string1 创建持有者 pos 1 获取 string0 持有者 1 的转换 获取 string0 持有者 0 的转换 get Convert for string1holder 1

如何 get Convert for string0holder 1?

谢谢

I want to have a Listview adapter where each rows uses it's own convertview,
so this convertview couldn't be share to another row.
Basically, if I have 4 rows, I would like to have 4 convertview.
And when drawing the row #3 I should get the convertview #3

To do this I setted each row a different type (it's position)
I thought it would do the trick.

public class myadapter extends BaseAdapter {
final List<String> mdata;

@Override
public int getCount() {
    return mdata.size();
}

@Override
public Object getItem(int p) {
    return p;
}

@Override
public long getItemId(int p) {
    return p;
}

@Override
public int getItemViewType(int p) {
    return p;
}

@Override
public int getViewTypeCount() {
    return mdata.size()  == 0  ? 1 :mdata.size() + 1;
}

@Override
public View getView(int p, View v, ViewGroup arg2) {
    ViewHolder holder = null;
    if (v == null) {
        v = li.inflate(R.layout.data, null);
        holder = new ViewHolder();
        holder._pos = p;
        Log.v("TAG", "creating holder pos " + p + " for " + mdata.get(p));

        {...}

        v.setTag(holder);
    } else {
        holder = (ViewHolder)v.getTag();
        Log.d("TAG", "get convert for " + mdata.get(p)+ " holder " + holder._pos);

    }
    {...}
    return v;
}
public static class ViewHolder {
    public TextView _textView;
    public int _pos;
}
}

Note the pos in the viewholder is only here to debug purpose.

Unfortunatly it displays:

creating holder pos 0 for string0
creating holder pos 1 for string1
get convert for string0 holder 1
get convert for string0 holder 0
get convert for string1 holder 1

How the get convert for string0 holder 1 is possible ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骷髅 2024-12-12 15:11:18

当我复制粘贴您的代码并设置以下字段时......

final List<String> mdata = Arrays.asList("s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "s11", "s12");

iT 的工作方式与您期望的完全相同。
您确定您没有误解日志输出吗?

When I copy paste your code and set following field ...

final List<String> mdata = Arrays.asList("s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "s11", "s12");

... iT works the exact way you would expect it.
Are you sure you didn't missunderstand the log output?

演出会有结束 2024-12-12 15:11:18

我发现了问题,

getViewTypeCount()

程序执行过程中无法改变。
即使使用较少,也必须将其设置为较大的值。它必须是一个常数。

I found the problem,

getViewTypeCount()

could not change during the execution of the program.
You have to set it to a big value, even if you use less. It has to be a constant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文