Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中对象的自定义列表视图_Android_Arrays_Listview_Object - Fatal编程技术网

Android中对象的自定义列表视图

Android中对象的自定义列表视图,android,arrays,listview,object,Android,Arrays,Listview,Object,我是Android编程的初学者,不需要什么帮助。 我有一个应该在listview中显示的对象数组。以下是课程代码: public class Car { public String img; public String thumbnail; public String manufacturer; public String model; public int price; public String description; } 缩略图是一个字符

我是Android编程的初学者,不需要什么帮助。 我有一个应该在listview中显示的对象数组。以下是课程代码:

public class Car {
    public String img;
    public String thumbnail;
    public String manufacturer;
    public String model;
    public int price;
    public String description;
}
缩略图是一个字符串变量,包含要显示的缩略图的URL。 我还在我的数据存储类中声明了一些称为“cars”的Car对象数组

我只想在我的自定义listview中显示每个对象的两个东西:缩略图(来自internet)和模型

我创建了layout item.xml,它应该表示listview的一项:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@android:color/holo_blue_dark">
    <ImageView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="left"
        android:id="@+id/image_tmb"
        android:layout_gravity="left|center_vertical"/>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_dark">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:paddingBottom="10dp"
            android:id="@+id/name"
            android:layout_weight="1"
            android:textSize="23dp"/>

    </LinearLayout>
</LinearLayout>
public class MyAdapter extends BaseAdapter {
    private Context myContext;
    public MyAdapter(Context context) {
        this.myContext = context;
    }

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

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
      //Here I should customize my view but don't know how.

    }
我在activity_main.xml中创建了listview元素,并在MainActivity中删除它:

 ListView lv=(ListView) findViewById(R.id.lv);

那么,我该如何完成我的任务呢?

一个简单的模式应该是这样的

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    // Inflate the view if it doesn't already exist
    if (view == null) {
        LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
    }

    // Get your current Car object
    Car c = getItem(i);

    // Initialize the views
    ImageView image = (ImageView) view.findViewById(R.id.image_tmb);
    TextView name = (TextView) view.findViewById(R.id.name);

    // Fill the views according to the car's properties

    /* NOTE: I'm not going to go in depth to explain how to set 
    an ImageView image from a URL, as it is an entirely different question,
    so I'll put a link to this library below */

    Picasso.with(myContext).load(c.thumbnail).into(image);
    name.setText(c.model);
}
MyAdapter mAdapter = new MyAdapter(this);
lv.setAdapter(mAdapter);
您需要修改
getItem
以实际获取项目,而不是返回null

@Override
public Object getItem(int position) {
    return DataStorage.cars[position];
}
然后,在Activity/Fragment/which中设置适配器,如下所示

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    // Inflate the view if it doesn't already exist
    if (view == null) {
        LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
    }

    // Get your current Car object
    Car c = getItem(i);

    // Initialize the views
    ImageView image = (ImageView) view.findViewById(R.id.image_tmb);
    TextView name = (TextView) view.findViewById(R.id.name);

    // Fill the views according to the car's properties

    /* NOTE: I'm not going to go in depth to explain how to set 
    an ImageView image from a URL, as it is an entirely different question,
    so I'll put a link to this library below */

    Picasso.with(myContext).load(c.thumbnail).into(image);
    name.setText(c.model);
}
MyAdapter mAdapter = new MyAdapter(this);
lv.setAdapter(mAdapter);

如果您打算大量使用图像,并且不想编写自己的实现,那么这是一个非常容易使用的库。

您好,很遗憾,您的问题有点“糟糕”,因为您要求我们为您编写代码,或者让您到一个地方学习材料。。。不是为了“修复一个bug”(这里的帖子应该是关于这个bug的,你的帖子应该会被标记),这里是我在谷歌搜索“android自定义listview”时发现的众多链接之一,我随意选择了一个看起来像教程的链接。因此,在将来,如果您只需要tutorial/etc,那么您可以自己搜索一下。您必须在适配器中将tumbnails定义为参数。在您的主要活动中,将其传递给适配器。不要忘了初始化getview方法中的项。但是在将来,如果您遵循教程或其他内容,并且不理解某些内容(或者您遇到了错误,或者它可以工作,但您不理解“为什么”),那么请随意问另一个问题,了解问题是什么。