我有一个ListAdapter,我将List传递给它。当我遍历它并尝试将值分配给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;
}
答案 0 :(得分:0)
删除for
循环并将data.get(i)
替换为data.get(position)
。
答案 1 :(得分:0)
删除您正在迭代所有数据元素的内容。
public View getView(int position, View convertView, ViewGroup parent) {
final News news = data.get(position);
// ...
}
答案 2 :(得分:0)
尝试以下代码: -
<强> getView 强>
View row = convertView;
// get adapter value
News newsListy = this.data.get(i)
ViewHolder viewHolder = null;
if (row == null)
{
row = inflater.inflate(R.layout.item_post_wall, parent, false);
viewHolder = new ViewHolder();
row.setTag(viewHolder);
}
viewHolder = (ViewHolder) row.getTag();
答案 3 :(得分:0)
错误:您的代码是循环评估。在最后,你的myTitle也是dataList的最后一项数据。