在不应包含图像的行上显示图像的列表视图



我有一个列表视图,它是用我创建的自定义适配器实现的,其中包括一个imageView。在整个列表中,每个项目可能附加也可能没有附加图像(这不是必需的(。 为了将图像加载到imageView中,我在getView方法中使用毕加索库。

当涉及到具有关联图像的行时,我的代码工作正常。 问题是在显示列表时,不应具有图像的行正在显示图像。

这是我的getView((方法:

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ReportHolder holder = null;
if(convertView==null){
LayoutInflater inflater=((Activity)context).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId,null);
holder = new ReportHolder();
holder.imageReportView=(ImageView)convertView.findViewById(R.id.reportImage);
holder.reportLocation=(TextView) convertView.findViewById(R.id.report_location);
holder.reportDescription=(TextView) convertView.findViewById(R.id.report_description);
holder.reportStatus=(TextView) convertView.findViewById(R.id.report_status);
convertView.setTag(holder);
}
else
holder=(ReportHolder)convertView.getTag();
ReportData data = reportDataList.get(position);
holder.reportLocation.setText(data.address);
holder.reportDescription.setText(data.description);
holder.reportStatus.setText(data.status);
Picasso picasso = Picasso.with(this.context);
if(data.url!=null)
picasso.load("https://fourth-landing-159416.appspot.com/gcs/"+data.url+"_thumbnail").into(holder.imageReportView);
return convertView;
}

我知道我正在很好地获取我的信息,因为除了图片之外,行之间的任何信息都不会重复。那么我在这里错过了什么?

FMI 我的适配器是在嵌套的 ASyncTask 中创建的,因为我需要通过 HTTP 连接获取信息,然后才能将其插入适配器:

@Override
protected void onPostExecute(final String result) {
mFeedTask = null;
mFeed.removeFooterView(mProgressView);
if(result!=null){
if(result.contains("HTTP error code: 403")){
Toast.makeText(mContext,"Token invalid. Please login again.", Toast.LENGTH_SHORT).show();
}
else if(result.equals("[]"))
Toast.makeText(mContext,"Nothing more to show.", Toast.LENGTH_SHORT).show();
else{
try {
Gson gson = new Gson();
JSONArray reports = new JSONArray(result);
LinkedList<ReportData> tmpList = new LinkedList<ReportData>();
for(int i=0; i < reports.length(); i++){
ReportData data = gson.fromJson(reports.getString(i), ReportData.class);
tmpList.add(data);
}
reportDataList.addAll(tmpList);
if(reportDataList.size()==tmpList.size()){
// First, we set the empty adapter and set up the item click listeners
adapter = new CustomAdapter(mContext,R.layout.custom_feed_row,reportDataList);
mFeed.setAdapter(adapter);
}
else {
updateTriggered=false;
adapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}

由于我要传输的信息量相对较大,因此我必须多次调用此任务。在第一次调用时,将创建适配器,然后调用 notifyDataSetChanged 来更新它。

非常感谢帮助!

我提前谢谢你! 干杯

因为 listview 回收相同的视图,所以如果此行不应包含图像,则应删除图像

在你的getView中做这样的事情:

if(data.url!=null)
picasso.load("https://fourth-landing-159416.appspot.com/gcs/"+data.url+"_thumbnail").into(holder.imageReportView);
else {
holder.imageReportView.setImageDrawable(null);;
}

最新更新