【问题标题】:ListView displaying image on rows that shouldn't have oneListView 在不应该有的行上显示图像
【发布时间】:2017-12-07 13:42:43
【问题描述】:

我有一个使用我创建的自定义适配器实现的 listView,其中包括一个 imageView。在整个列表中,每个项目可能附有图片,也可能不附有图片(这不是必需的)。 要将图像加载到 imageView 中,我使用 Picasso 库,在 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 对其进行更新。

非常感谢!

提前谢谢你! 干杯

【问题讨论】:

  • 这是因为视图被重用,当您没有任何图像的行时,您必须从您之前设置的转换视图中删除现有图像。

标签: android listview picasso


【解决方案1】:

因为 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);;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多