I am trying to creat a Customer ListView
I have setup the layout and getView
method But I am not sure how to bind that new view from getView to my adapter?
I have a feedClass
extending from ArrayAdapter<String>
in which I have getView
method:
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;
}
}
And in MainActivity
which extends from ListActivity
I have this:
private void activateFeed() {
//Now I Want to show data in ListView that i am setting in getView?
ArrayAdapter<String> adptr = new ArrayAdapter<String>(this,
R.layout.feed_item_layout, ?); //What to pass here?
setListAdapter(adptr);
}
Currently it is using an array, now I don't know how pass the custom view from getView to this? Please help.
You need to extend the BaseAdapter and override the getView method
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;
}
}
I assume you have a Custom Adapter by name feedClass and you want to set the same to 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
}
Also use a ViewHolder
pattern for smooth scrolling and performance
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Now
FeedClass fc = new FeedClass(MainActivity.this);
setListAdapter(fc);
Example i found on the net
ListActivity有自己的适配器实现,您可以通过正确的方式来完成它,但是如果您要使用自己的Adapter类实例,则需要将活动文件扩展为扩展Activity,然后可以使用适配器的getView方法
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.