我正在开发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的Listener中访问?
例如,我想要更改ImageView thumbail0的图像......
我尝试了不同的方法,但没有任何效果。
例如:
lv.findViewById(R.id.thumbnail0);
或者
查看vi = inflater.inflate(R.layout.custom_list,null); vi.findViewById(R.id.thumbnail0);
当我尝试实现此代码时,我无法使用this解决方案出现错误:
java.lang.NullPointerException:尝试调用虚方法 &#39; android.view.View android.view.View.findViewById(int)&#39;在null 对象参考
答案 0 :(得分:1)
您可以在使用getView()
adapter
方法创建之前访问任何项目。这个方法绑定每个项目。