【问题标题】:CustomList adapter not TextView and ImageViewCustomList 适配器不是 TextView 和 ImageView
【发布时间】:2016-05-29 10:28:37
【问题描述】:

我想要一个列表视图来显示带有自定义列表的数组中的内容,但是当我运行应用程序时列表视图变为空白。 list_item.xml 的 TextView 或 Image 均未显示。

这里是 MainActivity.java

public class MainActivity extends Activity {

    ListView list;
    List<Integer> users = new ArrayList<Integer>();
    List<String> imageId = new ArrayList<String>();

    Integer[] userNames;
    String[] imageUrls;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        users.add(50);
        imageId.add("https://color.adobe.com/build2.0.0-buildNo/resource/img/kuler/color_wheel_730.png");


        userNames = new Integer[users.size()];
        userNames = users.toArray(userNames);

        imageUrls = new String[imageId.size()];
        imageUrls = imageId.toArray(imageUrls);

        CustomList adapter = new
                CustomList(MainActivity.this, userNames, imageUrls);


        list=(ListView)findViewById(R.id.listView);
        list.setAdapter(adapter);

    }
}

这里是 CustomList.java:

public class CustomList extends ArrayAdapter<String> {

    private final Activity context;
    private final Integer[] userNames;
    private final String[] imageUrl;


    static class ViewHolderItem {
        TextView textViewItem;
    }

    public CustomList(Activity context,
                      Integer[] users, String[] imageUrl) {
        super(context, R.layout.list_item);
        this.context = context;
        this.userNames = users;
        this.imageUrl = imageUrl;

    }

    static class ViewHolder{
        TextView userName;
        ImageView image;
    }


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

        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.list_item, null, true);

        ViewHolder holder;

        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
            holder = new ViewHolder();

            holder.userName = (TextView)convertView.findViewById(R.id.userName);
            holder.image = (ImageView)convertView.findViewById(R.id.img);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }


        holder.userName.setText(String.valueOf(userNames[position]));
        Picasso.with(context).load(imageUrl[position]).resize(1000, 1000).centerInside().into(holder.image);

        return convertView;

    }
}

这里是list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:id="@+id/userName" android:padding="10dp"/>

    <ImageView
        android:id="@+id/img"
        android:tag="@string/app_name"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        />
</TableLayout>

【问题讨论】:

  • 您的日志中是否有任何错误?可能是您传递给适配器构造函数的数组是空的。
  • @ishmaelMakitla 数组不为空,因为我在 MainActivity.java 中添加了项目
  • 您能否通过在 CustomList 构造函数中显示每个数组的长度来确认数组具有值 - 仅显示用户和 imageUrl 数组的长度。另外,只是好奇,您为什么将 TableLayout 用于您的 list_item XML?

标签: java android arrays listview custom-lists


【解决方案1】:

只需在您的 CustomList.class 中添加这一行

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

另外,我注意到 TextView 中没有宽度和高度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多