notifyDataSetChanged()方法不起作用

时间:2018-08-14 04:18:23

标签: android

我创建了一个自定义列表视图,从而创建了一个应用程序。当用户单击自定义列表视图中存在的下载按钮时,将下载文件,然后我要禁用或隐藏该按钮,并且能够隐藏该按钮,但是当用户再次按下并再次按下该按钮时,我想隐藏该按钮用户单击下载按钮,应先下载文件,然后立即隐藏该按钮。 这是我的自定义列表适配器的代码

     @Override
              public View getView(final int position, View convertView,  
              ViewGroup parent) 
            {

        ViewHolder holder;
        if(convertView==null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_layout, null);
Typeface.createFromAsset(getContext().getAssets(),"DroidSans-Bold.ttf");
            holder.textViewName = convertView.findViewById(R.id.textViewName);
            holder.textViewurl = convertView.findViewById(R.id.textViewUrl);
           holder.download = convertView.findViewById(R.id.downloadimage);
            convertView.setTag(holder);
        }
        else
        {
            holder=(ViewHolder)convertView.getTag();
        }
      holder.textViewName.setText(pdfList.get(position).getName());
      holder.textViewurl.setText(pdfList.get(position).getUrl());
        File file = new File(Environment.getExternalStorageDirectory() + "/"
                + "android"+"/"+"data"+"/"+"foldername"+"/"+ pdfList.get(position).getName()+".pdf");
        if(file.exists())
        {
            holder.download.setVisibility(View.GONE);

        }
        else
        {
            holder.download.setVisibility(View.VISIBLE);
            holder.download.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(position);
                }
            });

        }
        notifyDataSetChanged();
        return convertView;

    }

1 个答案:

答案 0 :(得分:1)

您的notifyDataSetChanged()位置错误,请在单击下载按钮时尝试调用它。

 @Override
          public View getView(final int position, View convertView,  
          ViewGroup parent) 
        {

    ViewHolder holder;
    if(convertView==null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.list_layout, null);
        Typeface.createFromAsset(getContext().getAssets(),"DroidSans-Bold.ttf");
        holder.textViewName = convertView.findViewById(R.id.textViewName);
        holder.textViewurl = convertView.findViewById(R.id.textViewUrl);
       holder.download = convertView.findViewById(R.id.downloadimage);
        convertView.setTag(holder);
    }
    else
    {
        holder=(ViewHolder)convertView.getTag();
    }
  holder.textViewName.setText(pdfList.get(position).getName());
  holder.textViewurl.setText(pdfList.get(position).getUrl());
    File file = new File(Environment.getExternalStorageDirectory() + "/"
            + "android"+"/"+"data"+"/"+"foldername"+"/"+ pdfList.get(position).getName()+".pdf");
    if(file.exists())
    {
        holder.download.setVisibility(View.GONE);

    }
    else
    {
        holder.download.setVisibility(View.VISIBLE);
        holder.download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onItemClick(position);
                notifyDataSetChanged();
            }
        });

    }
    return convertView;

}