如何填充从ArrayList中℃的ListView控件; CustomClass&GT ;? [英] How to populate a ListView from ArrayList<CustomClass>?
问题描述
我在写一个Android RSS阅读器。
I'm writing an android RSS feed reader.
在我的程序在阅读正文之后的饲料还给我一个ArrayList
when my program read the feeds at the end return me an ArrayList
项目是我的类:
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;
}
}
现在我怎么能填充一个其中有3 TextView中的标题
,说明
和<$自定义的ListView C $ C>链接?
Now how can I populate a custom ListView that has 3 TextView in it for Title
, description
and link
?
推荐答案
您不必编写自定义的ListView。您应该使用个性化的布局和自定义适配器。
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>
(在 RES /布局
文件夹中保存为 list_item.xml
)。结果
下一步我建议你创建一个自定义适配器,能够有效地显示您的布局:
(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;
}
}
要了解更多有关自定义适配器,ViewHolders和效率敬请收看Android的罗曼盖伊的谈这个题目。
To learn more about custom adapter's, ViewHolders, and efficiency please watch Android's Romain Guy talk on this subject.
希望帮助!
这篇关于如何填充从ArrayList中℃的ListView控件; CustomClass&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!