ListView 在不应该有的行上显示图像
ListView displaying image on rows that shouldn't have one
我有一个 listView,它是使用我创建的自定义适配器实现的,其中包括一个 imageView。在整个列表中,每个项目可能会或可能不会附加图像(这不是必需的)。
为了将图像加载到 imageView 中,我在 getView 方法中使用了 Picasso 库。
当涉及到与图像相关联的行时,我的代码工作正常。
问题是当显示列表时,不应该有图像的行显示了一个。
这是我的 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 来更新它。
非常感谢您的帮助!
先谢谢你了!
干杯
因为列表视图回收相同的视图,所以如果该行不应包含图像,您应该删除图像
在你的 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);;
}
我有一个 listView,它是使用我创建的自定义适配器实现的,其中包括一个 imageView。在整个列表中,每个项目可能会或可能不会附加图像(这不是必需的)。 为了将图像加载到 imageView 中,我在 getView 方法中使用了 Picasso 库。
当涉及到与图像相关联的行时,我的代码工作正常。 问题是当显示列表时,不应该有图像的行显示了一个。
这是我的 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 来更新它。
非常感谢您的帮助!
先谢谢你了! 干杯
因为列表视图回收相同的视图,所以如果该行不应包含图像,您应该删除图像
在你的 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);;
}