Android 如何访问自定义ListView中的项目

Android 如何访问自定义ListView中的项目,android,listview,Android,Listview,我正在开发一个Android应用程序,我必须实现一个定制的ListView 我有一个包含ListView声明的主布局: <ListView android:id="@+id/imageList" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.5"> </ListView> 以及自定义行的布局: <?xm

我正在开发一个Android应用程序,我必须实现一个定制的ListView

我有一个包含ListView声明的主布局:

<ListView
    android:id="@+id/imageList"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5">
</ListView>
以及自定义行的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_row_selector"
    android:padding="8dp">

    <LinearLayout
        android:id="@+id/titleLL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/title"
            android:textColor="#FF0000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/photoList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/titleLL"
        android:orientation="horizontal"
        android:layout_alignParentStart="true">

        <ImageView
            android:id="@+id/takePhoto"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:scaleType="center"
            android:src="@drawable/ico_camera"
            android:layout_marginRight="40dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true" />

        <ImageView
            android:id="@+id/thumbnail0"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:scaleType="center"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="40dp" />

        <ImageView
            android:id="@+id/thumbnail1"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:scaleType="center"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="40dp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/deleteall"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ico_menu_delete_photo"
        android:layout_below="@+id/titleLL"
        android:layout_alignParentEnd="true" />
    </RelativeLayout>
public class CustomRow {
    private Integer id;
    private String title;

    public CustomRow() {}

    public CustomRow(String ti, Integer id) {
        this.id = id;
        this.title = ti;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String ti) {
        this.title = ti;
    }
}

and finally the code of the CustomList:


public class CustomList extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<CustomRow> ListItem;

    public CustomList(Activity activity, List<CustomRow> listPhotoItems) {
        this.activity = activity;
        this.ListItem = listPhotoItems;
    }

    @Override
    public int getCount() {
        return ListItem.size();
    }

    @Override
    public Object getItem(int location) {
        return ListItem.get(location);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater == null)
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.custom_list, null);

        ImageView takePhoto =  (ImageView) convertView.findViewById(R.id.takePhoto);

        TextView title = (TextView) convertView.findViewById(R.id.title);
        CustomRow m = ListItem.get(position);
        takePhoto.setTag(m.getId());

        title.setText(m.getTitle());
        return convertView;
    }
}
这是自定义行的类:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_row_selector"
    android:padding="8dp">

    <LinearLayout
        android:id="@+id/titleLL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/title"
            android:textColor="#FF0000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/photoList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/titleLL"
        android:orientation="horizontal"
        android:layout_alignParentStart="true">

        <ImageView
            android:id="@+id/takePhoto"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:scaleType="center"
            android:src="@drawable/ico_camera"
            android:layout_marginRight="40dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true" />

        <ImageView
            android:id="@+id/thumbnail0"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:scaleType="center"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="40dp" />

        <ImageView
            android:id="@+id/thumbnail1"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:scaleType="center"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="40dp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/deleteall"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ico_menu_delete_photo"
        android:layout_below="@+id/titleLL"
        android:layout_alignParentEnd="true" />
    </RelativeLayout>
public class CustomRow {
    private Integer id;
    private String title;

    public CustomRow() {}

    public CustomRow(String ti, Integer id) {
        this.id = id;
        this.title = ti;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String ti) {
        this.title = ti;
    }
}

and finally the code of the CustomList:


public class CustomList extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<CustomRow> ListItem;

    public CustomList(Activity activity, List<CustomRow> listPhotoItems) {
        this.activity = activity;
        this.ListItem = listPhotoItems;
    }

    @Override
    public int getCount() {
        return ListItem.size();
    }

    @Override
    public Object getItem(int location) {
        return ListItem.get(location);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater == null)
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.custom_list, null);

        ImageView takePhoto =  (ImageView) convertView.findViewById(R.id.takePhoto);

        TextView title = (TextView) convertView.findViewById(R.id.title);
        CustomRow m = ListItem.get(position);
        takePhoto.setTag(m.getId());

        title.setText(m.getTitle());
        return convertView;
    }
}
问题很简单,我如何在创建CustomRow之前访问ListView的项,而不是在ListView的侦听器中

例如,我想更改ImageView thumbail0的图像

我试过不同的方法,但都不管用

例如:

lv.findviewbydr.id.thumbnail0

视图vi=充气机.充气机.布局.自定义列表,空; vi.findviewbydr.id.thumbnail0

当我尝试实现此代码时,我无法使用解决方案错误occours:

java.lang.NullPointerException:尝试调用虚拟方法 空值上的“android.view.view android.view.view.FindViewByDint” 对象引用


在适配器的getView方法中创建之前,可以访问任何项。此方法绑定每个项。

在适配器的getView方法中执行此操作。我无法在适配器getView中执行此操作。@Max为什么不能在适配器getView中执行此操作?ListView的行在膨胀之前不存在,因此在此之前您无法访问它们。因为我需要以友好方式创建/修改行列表中的某些元素,例如更改列表中的图像ImageView@Max是的,这就是getView方法的用途-在必要时对行进行充气,以及在显示每行之前设置其视图属性。