Question

I'm writing an android RSS feed reader.

when my program read the feeds at the end return me an ArrayList

Item is my class:

public class Item implements Serializable {

    private String title;
    private String description;
    private String link;

    public Item() {
        setTitle(null);
        setDescription(null);
        setLink(null);
    }

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

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

}

Now how can I populate a custom ListView that has 3 TextView in it for Title, description and link?

Was it helpful?

Solution

You don't need to write a custom ListView. You should use a personalized layout and custom adapter.

First, write a layout to define how each row should look. Here's a basic example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:padding="5dp" />

    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:padding="5dp" />

    <TextView
        android:id="@+id/link"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:padding="5dp" />

</LinearLayout>

(Save it as list_item.xml in your res/layout folder.)
Next, I recommend that you create a custom adapter to efficiently display your layout:

public class ItemAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<Item> objects;

    public ItemAdapter(Context context, List<Item> objects) {
        this.objects = objects;
        inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.title);
            // Do the same for description and link
            convertView.setTag(holder);
        }
        else
            holder = (ViewHolder) convertView.getTag();

        Item item = objects.get(position);
        holder.title.setText(item.getTitle());
        // Same for description and link
        return convertView;
    }

    // Override the other required methods for BaseAdapter

    public class ViewHolder {
        TextView title;
        TextView description;
        TextView link;
    }
}

To learn more about custom adapter's, ViewHolders, and efficiency please watch Android's Romain Guy talk on this subject.

Hope that helps!

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