自定义适配器不显示输出

卡尔布鲁因斯

几天来,我一直在努力将自定义适配器简单地应用于列表视图,希望有人能帮助我解决我出错的地方。

我有一个 ListView (listViewAsItHappens),我想对视图使用自定义适配器,这样我就可以格式化 ListView 的外观。我已经阅读了很多文章,但似乎没有一篇文章像预期的那样工作。

当我调试代码时,它会步进到适配器,但 ListView 中没有显示任何内容。我没有收到任何错误,看起来所有变量/对象都很好地传递。

任何帮助我让 ListView 显示 3 行文本和图像的帮助都会很棒。

ListView list;
String[] itemname ={
        "Whistle",
        "Football",
        "Card"
};

Integer[] imgid={
        R.drawable.ic_whistle,
        R.drawable.ic_football,
        R.drawable.ic_yellowcard
};

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_refscorecard, container, false);
    list=(ListView) view.findViewById(R.id.listViewAsItHappens);
    asItHappened adapter=new asItHappened(getActivity(), itemname, imgid);
    list.setAdapter(adapter);
    adapter.notifyDataSetChanged();

我的自定义适配器(asithappens_listview 是我的 XML 布局);

public class asItHappened extends ArrayAdapter<asItHappened_List> {
private Activity context;
private String[] eventType;
private final Integer[] imgid;

public asItHappened(Context context, String[] event, Integer[] icon) {
    super(context, R.layout.asithappens_listview);
    this.eventType=event;
    this.imgid=icon;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.asithappens_listview, null,true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txtEvent);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imgEvent);
    rowView.findViewById(R.id.textView1);
    txtTitle.setText(eventType[position]);
    imageView.setImageResource(imgid[position]);
   return rowView;
  }
}
山姆

您还没有覆盖的getCount()方法ArrayAdapter您需要在您的客户适配器类中覆盖它并返回需要显示的元素的大小。在您的自定义适配器类中添加类似的内容。

@Override
    public int getCount() {
        return imgid.length;
    }

您可以更改自定义适配器的声明并从 ArrayAdapter<String>

或者为了获得更大的灵活性,您可以BaseAdapter像这样扩展它,只需将您的自定义适配器类更改为这个。

public class asItHappened extends BaseAdapter {
    private Activity context;
    private String[] eventType;
    private final Integer[] imgid;

    public asItHappened(Context context, String[] event, Integer[] icon) {
        this.context = context;
        this.eventType=event;
        this.imgid=icon;
    }

    @Override
    public int getCount() {
        return eventType.length;
    }

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

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

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.asithappens_listview, null,true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txtEvent);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imgEvent);
        rowView.findViewById(R.id.textView1);
        txtTitle.setText(eventType[position]);
        imageView.setImageResource(imgid[position]);
        return rowView;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在适配器中正确显示自定义对话框

我的自定义适配器未显示在片段中

自定义适配器未在ListView上显示任何数据

SwipeRefreshLayout中的Listview-自定义适配器仅显示一行时不显示任何内容

在自定义ListView适配器中按项目时突出显示效果

Android自定义适配器未显示所有项目

在自定义列表视图适配器(kotlin)中设置默认突出显示的项目

使用自定义适配器未显示刷卡

listview项目未在自定义适配器的getview方法中显示分配的值

如何使用自定义列表适配器显示listView为空

自定义适配器未显示正确的信息

使用自定义适配器未显示ListView

自定义适配器不显示数据

自定义Android适配器从不显示数据

自定义旋转适配器显示空列表

ListView使用自定义适配器不显示任何内容

使用自定义适配器的Listview搜索无法正确显示搜索结果

使用自定义数组适配器,ListFragment中的项目不会显示在Listview中

使用凌空下载时在自定义适配器中显示进度栏

自定义适配器微调器无法显示所有值

自定义ListView的适配器

在自定义适配器列表视图中突出显示搜索到的文本

在ListFragment的适配器显示的列表之前添加自定义布局

带有自定义适配器的alertdialog listview不显示任何内容

长时间单击时,在“自定义适配器”上显示淡淡的色相覆盖

自定义适配器不显示列表视图

自定义适配器不在列表视图中显示文本

带有自定义适配器的 Android ListView 仅显示最后一项

带有自定义适配器的列表视图不显示所有项目