我正在尝试创建一个客户ListView
我已经设置了布局和getView
方法
但我不确定如何将新视图从getView绑定到我的适配器?
我从[{1}}延伸feedClass
我有ArrayAdapter<String>
方法:
getView
}
在从public class FeedClass extends ArrayAdapter<String> {
private final Context context;
public FeedClass(Context context) {
super(context, R.layout.feed_item_layout);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.feed_item_layout, parent,
false);
ImageView tweetPicture = (ImageView) rowView
.findViewById(R.id.tweetPicture);
TextView tweetDescription = (TextView) rowView
.findViewById(R.id.tweetDescription);
TextView legends = (TextView) rowView.findViewById(R.id.legends);
tweetPicture.setImageResource(R.drawable.ic_profile);
tweetDescription.setText("Hello");
legends.setText("Legends");
//How to show this data in the ListView?
return rowView;
}
延伸的MainActivity
中我有这个:
ListActivity
目前它正在使用数组,现在我不知道如何将自定义视图从getView传递给它?请帮忙。
答案 0 :(得分:2)
您需要扩展BaseAdapter并覆盖getView方法
public class FeedClass extends BaseAdapter {
private Context context;
private ArrayList<String> arrayList;
public FeedClass(Context context, ArrayList<String> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int arg0) {
return arrayList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.feed_item_layout, parent,
false);
ImageView tweetPicture = (ImageView) rowView
.findViewById(R.id.tweetPicture);
TextView tweetDescription = (TextView) rowView
.findViewById(R.id.tweetDescription);
TextView legends = (TextView) rowView.findViewById(R.id.legends);
tweetPicture.setImageResource(R.drawable.ic_profile);
// set the text from the ArrayList
tweetDescription.setText(arrayList.get(position));
// set the text from the ArrayList
legends.setText(arrayList.get(position));
return rowView;
}
}
答案 1 :(得分:1)
我假设您有一个名为feedClass的自定义适配器,并且您希望将其设置为ListActivity
LayoutInflater inflater;
public FeedClass(Context context)
{
super(context, R.layout.feed_item_layout);
inflater = LayoutInflater.from(context);
// no need to initialize everytime in getview
// so you can initialize in the constructor
}
还使用ViewHolder
模式进行平滑滚动和演奏
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
现在
FeedClass fc = new FeedClass(MainActivity.this);
setListAdapter(fc);
我在网上找到的例子
答案 2 :(得分:1)
ListActivity有自己的适配器实现,您已经正确地完成了 但是如果你想使用你自己的Adapter类实例,那么你需要扩展你的活动文件来扩展Activity然后你可以使用你的适配器getView方法