Reputation: 5405
Im using a BaseAdapter and passing to it a List of objects that have their own data sets. In each row of the listview Id like an image, description, ect. of each object. When I use the BaseAdapter as I have it set now I dont get any errors and Ive checked over and over in the debugger and logcat that my List of objects are valid and not null. But for some reason the list builds with the correct number of objects but not one of the ImageViews, TextViews, or anything else takes on any of the data. The only thing in each row is the same default data I use in the xml layout for the list row. Its like the call to each View's set method is either being ignored for some reason or the content for the view is being passed to another context onscreen or something wacky. Anyway heres my BaseAdapter class:
class MaItemAdapter extends BaseAdapter{
List<Ma> mas = null;
Context cntx = null;
//LayoutInflater myInflater;
int itemPosition =0;
ViewHolder holder;
public MarkItemAdapter(Activity cntx,List<Ma> mas){
super();
this.mas = mas;
this.cntx = cntx;
//myInflater = LayoutInflater.from(cntx);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mas.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mas.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
itemPosition = position;
RelativeLayout rl = null;
Mark ma = mas.get(itemPosition);
if (convertView==null) {
rl = (RelativeLayout) LayoutInflater.from(cntx).inflate(
R.layout.ma_row_item, parent, false);
holder = new ViewHolder();
try{
holder.description = (TextView) rl.findViewById(R.id.ma_description);
holder.distance = (TextView) rl.findViewById(R.id.ma_distance);
holder.ma_image = (ImageView) rl.findViewById(R.id.ma_image);
holder.view_ma = (ImageButton) rl.findViewById(R.id.view_ma);
holder.rate_ma = (RatingBar) rl.findViewById(R.id.rate_ma);
holder.description.setText(ma.title);
holder.distance.setText(ma.getDistanceString());
holder.view_ma.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
//dostuff
}
});
if(ma.textImg!=null &&
!ma.textImg.isRecycled()){
holder.ma_image.setImageBitmap(ma.textImg);
}else{
holder.ma_image.setImageResource(R.raw.skyma);
}}catch(NullPointerException e){}
}else{
rl = (RelativeLayout) convertView;
}
//add logic for rating bar
return rl;
}
}
//convenience class to create views
private class ViewHolder{
ImageView ma_image;
TextView description;
TextView distance;
ImageButton view_ma;
RatingBar rate_ma;
public ViewHolder(){
ma_image = new ImageView(getApplicationContext());
description = new TextView(getApplicationContext());
distance = new TextView(getApplicationContext());
view_ma = new ImageButton(getApplicationContext());
rate_ma = new RatingBar(getApplicationContext());
}
}
}
Ive run through a few tutorials and they work fine but when I use this set up in my own app its a no go. Some help in the right direction would be awesome. One other thing I should mention is that if I push on the rating bar i see the same rating pop up every four or five rows. Any help would be much appreciated.
Upvotes: 0
Views: 666
Reputation: 253
Try this in your getView method...this is how my custom adapters are setup.
itemPosition = position;
View v = convertView;
Mark ma = mas.get(itemPosition);
if (v == null) {
LayoutInflater inflater = (LayoutInflater) cntx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.ma_row_item, null);
holder = new ViewHolder();
}
holder.description = (TextView) v.findViewById(R.id.ma_description);
holder.distance = (TextView) v.findViewById(R.id.ma_distance);
holder.ma_image = (ImageView) v.findViewById(R.id.ma_image);
holder.view_ma = (ImageButton) v.findViewById(R.id.view_ma);
holder.rate_ma = (RatingBar) v.findViewById(R.id.rate_ma);
holder.description.setText(ma.title);
holder.distance.setText(ma.getDistanceString());
holder.view_ma.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
//dostuff
}
});
if(ma.textImg!=null && !ma.textImg.isRecycled()) {
holder.ma_image.setImageBitmap(ma.textImg);
} else {
holder.ma_image.setImageResource(R.raw.skyma);
}
If that doesn't work, I could also suggest just initializing your individual views from within the getView method body instead of in ViewHolder. Something like:
TextView description = (TextView) v.findViewById(R.id.ma_description);
description.setText(ma.title);
Upvotes: 1