1

I am working on a project. In my project when user clicks on a list item, it downloads a file from some source and saves it into internal storage.

I want to show an image in list items of listview if file is already download.

The code below is working fine but it is also showing image for the list items whose file is yet not downloaded.

Here is my adapter class code.

package life.quran.com.quranlife;

public class SurahBaseSearchAdapter extends BaseAdapter {

    Context mContext;
    LayoutInflater mInflator;
    private List<Surah> surahList = null;
    private ArrayList<Surah> arrayList;
    Surah surah;
    ArrayList<String> imgfileLocation = null;

    String searchhighlightString = "";

    public SurahBaseSearchAdapter(Context context, List<Surah> list) {

        mContext = context;
        surahList = list;
        mInflator = LayoutInflater.from(context);
        arrayList = new ArrayList<Surah>();
        arrayList.addAll(surahList);

    }

    public class ViewHolder {
        TextView sname;
        TextView sno;
        ImageView dlimg;
    }

    @Override
    public int getCount() {
        return surahList.size();
    }

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

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

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

        imgfileLocation = new ArrayList<>();
        surah = surahList.get(position);
        File f = mContext.getFilesDir();
        String filepath =  f.getAbsolutePath();
        File _file = new File(filepath+"/surah_"+surah.getSurah_id()+".json");



        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflator.inflate(R.layout.item_template,null);
            holder.sname = (TextView) convertView.findViewById(R.id.txt_surahName);
            holder.sno = (TextView) convertView.findViewById(R.id.txtsuraNo);
            holder.dlimg = convertView.findViewById(R.id.dlimg);


            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.sname.setText(surah.getSurah_name());
        holder.sno.setText(surah.getSurah_no());

        if (_file.exists()) {

            holder.dlimg.setImageResource(R.drawable.newdlimg);
        }


        String hSurahName = surah.getSurah_name().toLowerCase(Locale.getDefault());
        if(hSurahName.contains(searchhighlightString)) {
            int startpos = hSurahName.indexOf(searchhighlightString);
            int endpos = startpos + searchhighlightString.length();

            Spannable spanText = Spannable.Factory.getInstance().newSpannable(holder.sname.getText());
            spanText.setSpan(new ForegroundColorSpan(Color.BLUE),startpos,endpos,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            holder.sname.setText(spanText,TextView.BufferType.SPANNABLE);
        }



        return convertView;
    }

    public void filter(String charText) {

        searchhighlightString = charText;


        charText = charText.toLowerCase(Locale.getDefault());
        surahList.clear();
        if(charText.length() == 0) {
            surahList.addAll(arrayList);
        } else {
            for(Surah surah : arrayList) {
                if(surah.getSurah_name().toLowerCase(Locale.getDefault()).contains(charText)) {
                    surahList.add(surah);
                }
            }
        }
        notifyDataSetChanged();
    }
}

Let me know if anything is unclear

jcuenod
  • 55,835
  • 14
  • 65
  • 102

2 Answers2

2

You require to set default drawable if file not exist condition. Add else part to this if condition like

if (_file.exists()) {
    holder.dlimg.setImageResource(R.drawable.newdlimg);
}else{
    holder.dlimg.setImageResource(R.drawable.<default_image>);
}
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
1

You should clear image in ViewHolder after previous usage. Just change your code to something like this:

if (_file.exists()) {

    holder.dlimg.setImageResource(R.drawable.newdlimg);
} else {
    //clear image from previous usage
    //do not sure is it legal to set null. You need to check it.
    holder.dlimg.setImageResource(null)
}
MyDogTom
  • 4,486
  • 1
  • 28
  • 41