自定义ListView适配器忽略了设置的值。

huangapple go评论67阅读模式
英文:

Custom adapter for ListView ignores set values

问题

我将为您提供代码的中文翻译:

// 我打算创建一个包含标签和图标的ListView。

这是我所做的事情
// 我创建了一个名为“cardList”的空白ListView。
// 我创建了一个自定义视图,扩展了“ArrayAdapter<String>”。
// 然后我将“cardList”设置为该自定义视图:
cardList = findViewById(R.id.cards_list);
CustomView customView = new CustomView(this, names, images);
cardList.setAdapter(customView);

其中

ListView cardList;
String[] names = {"Name1", "Name2", "Name3"};
Integer[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3};
// CustomView类:
package ...

import ...

public class CustomView extends ArrayAdapter<String> {

    private String[] names;
    private Integer[] images;
    private Activity context;

    CustomView(Activity context, String[] names, Integer[] images) {
        super(context, R.layout.card_row, R.id.cardName, names);
        this.context = context;
        this.names = names;
        this.images = images;
    }

    @SuppressLint("InflateParams")
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View r = convertView;
        ViewHolder viewHolder;
        if (r == null) {
            LayoutInflater layoutInflater = context.getLayoutInflater();
            r = layoutInflater.inflate(R.layout.card_row, null, true);
            viewHolder = new ViewHolder(r);
            r.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) r.getTag();
        }
        viewHolder.name.setText(/*跳过:names[position]*/"一些文本");
        viewHolder.name.setTypeface(Typeface.createFromAsset(AppName.assetManager, "fonts/prototype.ttf"));
        viewHolder.image.setImageResource(images[position]);
        return super.getView(position, convertView, parent);
    }

    class ViewHolder {
        TextView name;
        ImageView image;
        ViewHolder(View v) {
            name = v.findViewById(R.id.cardName);
            image = v.findViewById(R.id.symbolImg);
        }
    }
}

现在,我故意将这行代码 viewHolder.name.setText(/*跳过:names[position]*/"一些文本"); 编写成这样。结果是我的ListView显示了数组中的名称,尽管方法setText("一些文本")应该更改它,而且没有图标和自定义字体。所以,问题出现在这一部分:

viewHolder.name.setText(/*跳过:names[position]*/"一些文本");
viewHolder.name.setTypeface(Typeface.createFromAsset(AppName.assetManager, "fonts/prototype.ttf"));
viewHolder.image.setImageResource(images[position]);

调试显示应用程序经过此部分,但应在其中设置的所有内容都被忽略了。我是否漏掉了什么?


<details>
<summary>英文:</summary>
I&#39;m going to create a `ListView` of labels and icons.
Here&#39;s what I&#39;ve done:
&lt;br&gt;I&#39;ve created a blank `ListView` called `cardList`.
&lt;br&gt;I&#39;ve created a custom view that extends `ArrayAdapter&lt;String&gt;`
&lt;br&gt;And I&#39;ve set the `cardList` to that custom view:

cardList = findViewById(R.id.cards_list);
CustomView customView = new CustomView(this, names, images);
cardList.setAdapter(customView);

where

ListView cardList;
String[] names = {"Name1", "Name2", "Name3"};
Integer[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3};

The `CustomView` class:

package ...

import ...

public class CustomView extends ArrayAdapter<String> {

private String[] names;
private Integer[] images;
private Activity context;
CustomView(Activity context, String[] names, Integer[] images) {
super(context, R.layout.card_row, R.id.cardName, names);
this.context = context;
this.names = names;
this.images = images;
}
@SuppressLint(&quot;InflateParams&quot;)
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View r = convertView;
ViewHolder viewHolder;
if (r == null) {
LayoutInflater layoutInflater = context.getLayoutInflater();
r = layoutInflater.inflate(R.layout.card_row, null, true);
viewHolder = new ViewHolder(r);
r.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) r.getTag();
}
viewHolder.name.setText(/*skipping: names[position]*/&quot;Some text&quot;);
viewHolder.name.setTypeface(Typeface.createFromAsset(AppName.assetManager, &quot;fonts/prototype.ttf&quot;));
viewHolder.image.setImageResource(images[position]);
return super.getView(position, convertView, parent);
}
class ViewHolder {
TextView name;
ImageView image;
ViewHolder(View v) {
name = v.findViewById(R.id.cardName);
image = v.findViewById(R.id.symbolImg);
}
}

}

Now, on purpose I wrote the line `viewHolder.name.setText(/*skipping: names[position]*/&quot;Some text&quot;);` like this. The result is that my `ListView` is displayed with names from the array although the method `setText(&quot;Some text&quot;)` should change that, and there are no icons and custom font. So, the problem is with this section:

viewHolder.name.setText(/skipping: names[position]/"Some text");
viewHolder.name.setTypeface(Typeface.createFromAsset(AppName.assetManager, "fonts/prototype.ttf"));
viewHolder.image.setImageResource(images[position]);

Debugging has shown that the application goes through this section, but all the things that should be set inside of it are ignored. Am I missing something?
</details>
# 答案1
**得分**: 0
以下是要翻译的内容:
你的自定义视图被忽略了,因为你在 `getView` 中返回了父视图,而不是你自己的视图。
请使用:
```java
return r;

而不是:

return super.getView(position, convertView, parent);
英文:

Your custom view is ignored as you return parent view from getView instead of your one

Use

return r;

Instead of

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

huangapple
  • 本文由 发表于 2020年1月6日 20:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定