我有一个ListAdapter,我将一个列表传递给了它。当我遍历它并尝试将值分配给TextViews和ImageViews时,仅显示列表中最后一项的元素。
这是我的代码:-
public class LazyAdapter extends BaseAdapter
{
private Activity activity;
private List<News> data;
private static LayoutInflater inflater = null;
public Context myContext;
public ImageLoader imageLoader;
public Context mContext;
public ProgressDialog pDialog;
String myTitle, description, pubdate, pubName, mycomments, myviews,bitmapurl;
static class LazyViewHolder
{
TextView title;
TextView description;
ImageView thumb_image;
WebView imageWebView;
TextView pubDate;
}
public LazyAdapter(Context ctx, List<News> newsListy)
{
mContext = ctx;
data=newsListy;
inflater =LayoutInflater.from(ctx);
imageLoader=new ImageLoader(ctx);
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
@Override
public boolean isEnabled(int position)
{
// TODO Auto-generated method stub
return true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi=convertView;
LazyViewHolder viewHolder;
if(convertView==null)
vi = inflater.inflate(com.zevenpooja.attini.R.layout.list_row,null);
viewHolder = new LazyViewHolder();
viewHolder.title = (TextView)vi.findViewById(com.zevenpooja.attini.R.id.title); // title
viewHolder.description = (TextView)vi.findViewById(com.zevenpooja.attini.R.id.description); // artist name
viewHolder.pubDate = (TextView)vi.findViewById(com.zevenpooja.attini.R.id.txtPubDate);
// List<News> song = new ArrayList<News>();
for (int i = 0; i < data.size(); i++)
{
myTitle = data.get(i).getTitle().toString();
description = data.get(i).getDescription().toString();
bitmapurl = data.get(i).getNewsBigImage().toString();
pubdate = data.get(i).getPublishedDate().toString();
StringBuilder sb = new StringBuilder(myTitle);
int j = 0;
while ((j = sb.indexOf(" ", j + 40)) != -1) {
sb.replace(j, j + 1, "\n");
}
viewHolder.title.setText(sb.toString());
//DEscription
description = description.substring(0, Math.min(description.length(), 50));
viewHolder.description.setText(description);
//Date
final String OLD_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
final String NEW_FORMAT = "MMMM dd, yy";
String newDate ="";
SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
try
{
Date d = sdf.parse(pubdate);
sdf.applyLocalizedPattern(NEW_FORMAT);
newDate = sdf.format(d);
viewHolder.pubDate.setText(newDate);
}
catch (java.text.ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//IMAGE
viewHolder.thumb_image=(ImageView)vi.findViewById(com.zevenpooja.attini.R.id.list_image); // thumb image
imageLoader.DisplayImage(bitmapurl, viewHolder.thumb_image);
}
return vi;
}
删除for
循环并替换data.get(i)
为data.get(position)
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句