CustomList适配器不是TextView和ImageView

时间:2016-05-29 10:28:37

标签: java android arrays listview custom-lists

我想要一个listview显示带有自定义列表的数组中的内容,但是当我运行应用程序时listview变为空白。 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>

1 个答案:

答案 0 :(得分:0)

只需在CustomList.class中添加此行

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

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