Question

I'm trying to populate custom ListView with this LazyAdapter class from this tutorial

I want to make list view items with custom design. Layout file list_row.xml is in my layouts file just like in the tutorial. This is a slightly modified getView function in LazyAdapter class:

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        //vi = inflater.inflate(R.layout.list_row, null);    //EDITED
        vi = inflater.inflate(R.layout.list_row, parent, false);
    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView subtitle = (TextView)vi.findViewById(R.id.subtitle); // artist name
    //ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);

    //This is not needed
    //HashMap<String, String> post = new HashMap<String, String>();
    HashMap<String, String> post;
    post = data.get(position);
    // Setting all values in listview
    title.setText(post.get("title"));     //the code crashes here
    subtitle.setText(post.get("subtitle"));
    //imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
    return vi;
}

Well this code crashes at title.setText(), because the title is null. I read somewhere that this makes sense because this LazyAdapter is calling findViewById before setContentView or something..

So how do I have to do this? And how does this code work for that guy?

Update

This is the list_row.xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!-- Title -->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="14sp"
        android:textStyle="bold"/>

    <!-- Subtitle -->
    <TextView
        android:id="@+id/subtitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/subtitle"
        android:textColor="#343434"
        android:textSize="10sp"
        android:layout_marginTop="1dip" />

</RelativeLayout>

To save space I'm only adding ListView xml from the main activity layout file:

<ListView
    android:id="@+id/feedListView"
    android:layout_marginTop="20dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/volumeControl1">
</ListView>

Adapter code: http://pastebin.com/niC1yXiJ

And the stack trace: http://pastebin.com/myW8B0Xz

Was it helpful?

Solution 2

This was a hell of a ride (two sleepless nights). Thanks for all your help, somehow I fixed it! :)

Mostly thanks to this question Null pointer exception in getView() of custom Adapter I made new getView function:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = new ViewHolder();
    View vi = convertView;
    if (vi == null) {
        LayoutInflater inflater = ((Activity)activity).getLayoutInflater();
        vi = inflater.inflate(R.layout.list_row, null);
        holder.title = (TextView) vi.findViewById(R.id.title);
        holder.subtitle = (TextView) vi.findViewById(R.id.subtitle);
        vi.setTag(holder);
    } else {

        holder = (ViewHolder) vi.getTag();
    }
    HashMap<String, String> post;
    post = data.get(position);
    holder.title.setText(post.get("title"));
    holder.subtitle.setText(post.get("subtitle"));
    return vi;
}

holder is just a simple class:

private class ViewHolder {
    public TextView title;
    public TextView subtitle;
}

I've done that and I also copied xml drawable files in all drawable folders (merged all). So one of this actions fixed it, but I'm not quite sure what..

OTHER TIPS

Try passing in the parent when you inflate your cell. Using this method:

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

vi = inflater.inflate(R.layout.list_row, parent, false);

If you get android.widget.AbsListView.obtainView(AbsListView.java:1408) exception than your returned View of getView() is probably null.So check first of all that you are not return null view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top