0

I'm developing an Android application, I have to implement a custom ListView.

I have a main layout that contains the declaration of the ListView:

<ListView
    android:id="@+id/imageList"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5">
</ListView>

and a layout for the custom row:

<?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>

This is the class for the custom row:

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;
    }
}

The question is simple how i can access to the item of the ListView before the creation the CustomRow and not in the Listener of the ListView ??

For example, i want change the image of the ImageView thumbail0...

I have tried diffrerent method but nothing work.

For example:

lv.findViewById(R.id.thumbnail0);

Or

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

I cannot use this solution when i try to implement this code an error occours:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

Community
  • 1
  • 1
Mattia
  • 880
  • 2
  • 13
  • 28

1 Answers1

1

You can access any item before creating in getView() method of adapter. this method bind each items.

LearnPainLess
  • 2,136
  • 1
  • 13
  • 22
  • I can't do this in adapter getView.. – Mattia Feb 03 '17 at 10:39
  • 1
    @Max Why can't you do it there, exactly? The `ListView`'s rows aren't going to exist before they're inflated there, so you can't access them any earlier than that. – Mike M. Feb 03 '17 at 11:20
  • 'cause I need to create / modify some element in the row list dinamically, (for example change the image in the ImageView) – Mattia Feb 03 '17 at 11:50
  • 1
    @Max Yeah, that's what the `getView()` method is for - to inflate the row if necessary, and to set the `View`s' properties for each row before it is displayed. – Mike M. Feb 03 '17 at 12:06