图像在列表视图中不可见

时间:2014-08-27 05:38:57

标签: android android-listview

我有一个带有图像和文本的自定义列表视图。现在,每当我打开列表视图时,图像都是可见的。现在,当我滚动列表时,图像是可见的。此外,我已经设置了应该显示哪个图像的条件什么时候,但在开始时所有的图像是相同的,图像只有在我滚动列表时才会改变。我知道这个问题与回收列表有关,但我无法解决。请帮助我

代码

public class ShareWithMeAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<SharedWithMeModel> data;

    public ShareWithMeAdapter(Context context, ArrayList<SharedWithMeModel> data) {
        this.context = context;
        this.data = data;
    }

    @Override

    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        final ViewHolder holder;

        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.share_with_me_custom_list, viewGroup, false);
            holder.tvTitle = (TextView) view.findViewById(R.id.tv_sharedwithme_title);
            holder.tvName = (TextView) view.findViewById(R.id.tv_sharedwithme_name);
            holder.tvSharedOn = (TextView) view.findViewById(R.id.tv_sharedwithme_on);
            holder.image = (ImageView) view.findViewById(R.id.iv_sharedwithme_image);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }


        holder.tvTitle.setText(data.get(i).getTitle());
        if (data.get(i).getName().equals("")) {
            holder.tvName.setText("Shared With " + data.get(i).getName());
        } else {
            holder.tvName.setText("Shared With " + data.get(i).getName());
        }
        holder.tvSharedOn.setText(data.get(i).getDate());

        if (data.get(i).getExtension().equals(".jpg") || data.get(i).getExtension().equals(".png") || data.get(i).getExtension().equals(".jpeg") || data.get(i).getExtension().equals(".gif") || data.get(i).getExtension().equals(".tiff")) {
            holder.image.setBackgroundResource(R.drawable.image);
        }

        if (data.get(i).getExtension().equals(".docx") || data.get(i).getExtension().equals(".doc")) {
            holder.image.setBackgroundResource(R.drawable.docx);
        }

        if (data.get(i).getExtension().equals(".xls") || data.get(i).getExtension().equals(".xlxs")) {
            holder.image.setBackgroundResource(R.drawable.excel);
        }

        if (data.get(i).getExtension().equals(".pdf")) {
            holder.image.setBackgroundResource(R.drawable.pdf);
        }

//        if (data.get(i).getExtension().equals(".ppt")) {
//            holder.image.setBackgroundResource(R.drawable.);
//        }


        return view;
    }

    private class ViewHolder {
        TextView tvTitle, tvName, tvSharedOn;
        ImageView image;
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个,希望它可能有帮助

替换所有

holder.image.setBackgroundResource(R.drawable.filename);

holder.image.setImageResource(R.drawable.filename);

或更改这些方法

@Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

答案 1 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

public class ShareWithMeAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<SharedWithMeModel> data;

    public ShareWithMeAdapter(Context context, ArrayList<SharedWithMeModel> data) {
        this.context = context;
        this.data = data;
    }

    @Override

    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        final ViewHolder holder;

        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.share_with_me_custom_list, null);
            holder.tvTitle = (TextView) view.findViewById(R.id.tv_sharedwithme_title);
            holder.tvName = (TextView) view.findViewById(R.id.tv_sharedwithme_name);
            holder.tvSharedOn = (TextView) view.findViewById(R.id.tv_sharedwithme_on);
            holder.image = (ImageView) view.findViewById(R.id.iv_sharedwithme_image);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }


        holder.tvTitle.setText(data.get(i).getTitle());
        if (data.get(i).getName().equals("")) {
            holder.tvName.setText("Shared With " + data.get(i).getName());
        } else {
            holder.tvName.setText("Shared With " + data.get(i).getName());
        }
        holder.tvSharedOn.setText(data.get(i).getDate());

        if (data.get(i).getExtension().equals(".jpg") || data.get(i).getExtension().equals(".png") || data.get(i).getExtension().equals(".jpeg") || data.get(i).getExtension().equals(".gif") || data.get(i).getExtension().equals(".tiff")) {
            holder.image.setImageResource(R.drawable.image);
        }else if (data.get(i).getExtension().equals(".docx") || data.get(i).getExtension().equals(".doc")) {
            holder.image.setImageResource(R.drawable.docx);
        }else if (data.get(i).getExtension().equals(".xls") || data.get(i).getExtension().equals(".xlxs")) {
            holder.image.setImageResource(R.drawable.excel);
        }else{
            holder.image.setImageResource(R.drawable.pdf);
        }

        return view;
    }

    private class ViewHolder {
        TextView tvTitle;
        TextView tvName;
        TextView tvSharedOn;
        ImageView image;
    }
}