-1

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?

Sam
  • 86,580
  • 20
  • 181
  • 179

1 Answers1

3

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!

Sam
  • 86,580
  • 20
  • 181
  • 179
  • I like your answer but I think OP didn't even try to read any tutorial on listviews and adapters. – Mikita Belahlazau Sep 24 '12 at 18:02
  • @NikitaBeloglazov I have that feeling too. But someone else dumped a useless answer here first and many of the search engine tutorials are slow (a pet-peeve of mine), so I took 10 minutes to whip this up. Also my layout is primitive and there's more work to be done in general, this leaves the OP with plenty more to learn! – Sam Sep 24 '12 at 18:12
  • @nikita you know i tried to find a good tutorial for 2 days but all i found had something wrong in it. – Alireza A.Masrurkhah Sep 24 '12 at 19:10
  • this : context.getSystemService made me crazy because in one tutorial they write it like this: getSystemService – Alireza A.Masrurkhah Sep 24 '12 at 19:18
  • Next time you struggle like that, visit the [Android Developer's page](http://developer.android.com/) and try searching the documentation for the method (in this case `getSystemService`). This may will help. – Sam Sep 24 '12 at 19:44
  • i use this code : Items itms = rss.parse(Accessor.fileReader("rss.jsp")); lv.setAdapter(new ItemAdapter(this, itms)); for set my listView adapter but it doesnt work!!!! – Alireza A.Masrurkhah Sep 24 '12 at 21:39
  • The phrase "but it doesnt work!!!!" is not very helpful. Does it crash? (If so post your LogCat in the question, that way I can see why it crashed). Is it just blank? (If so, `itms` is empty.) – Sam Sep 24 '12 at 23:32
  • Sam there is no Error or even warning in my log but it is just blank. but i test the itms and it was not empty because I can see its elements in TextViews for example – Alireza A.Masrurkhah Sep 25 '12 at 11:02
  • Hmm, did you add valid information to the other base adapter functions? For example if `getCount()` returns `0`, then obviously nothing will be displayed. It must return `objects.size()`. Also try adding `Log.v("Item", item.getTitle() + ": " + item.getDescription());` in the `getView()` method after the line `Item item = objects.get(position);`. Let me know what you see. – Sam Sep 25 '12 at 14:59
  • Sam its running now problem was that object.size() really thanks brother. by the way can i have your mail address or skyp id? because im truly new in android an d i need a friend just like u man – Alireza A.Masrurkhah Sep 25 '12 at 15:44
  • I'm glad it is working, please click the checkmark to mark this as correct. I am on Stack Overflow almost every day, you can notify me by adding `@Sam` to a comment here with a link to your new question and I'll take a look. Cheers! – Sam Sep 25 '12 at 16:01