【问题标题】:How to access to the items in a custom ListView [duplicate]如何访问自定义 ListView 中的项目 [重复]
【发布时间】:2017-02-03 09:53:21
【问题描述】:

我正在开发一个 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;
    }
}

问题很简单,我如何在创建 CustomRow 之前而不是在 ListView 的侦听器中访问 ListView 的项目??

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

我尝试了不同的方法,但没有任何效果。

例如:

lv.findViewById(R.id.thumbnail0);

或者

查看 vi = inflater.inflate(R.layout.custom_list, null); vi.findViewById(R.id.thumbnail0);

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

java.lang.NullPointerException:尝试调用虚拟方法 'android.view.View android.view.View.findViewById(int)' 为空 对象引用

【问题讨论】:

  • 在适配器的getView() 方法中进行。

标签: android listview


【解决方案1】:

您可以在adaptergetView() 方法中创建之前访问任何项目。此方法绑定每个项目。

【讨论】:

  • 我无法在适配器 getView 中执行此操作..
  • @Max 为什么你不能在那里做呢? ListView 的行在它们在那里膨胀之前不会存在,所以你不能在此之前访问它们。
  • '因为我需要动态创建/修改行列表中的某些元素,(例如更改 ImageView 中的图像)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多