我正在开展一个项目。在我的项目中,当用户单击列表项时,它会从某个源下载文件并将其保存到内部存储中。
如果文件已经下载,我想在listview
的列表项中显示图像。
下面的代码工作正常,但它也显示了尚未下载文件的列表项的图像。
这是我的adapter class
代码。
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();
}
}
如果有什么不清楚,请告诉我
答案 0 :(得分:2)
如果文件不存在,则需要设置默认drawable。如果条件如
,则将其他部分添加到此处if (_file.exists()) {
holder.dlimg.setImageResource(R.drawable.newdlimg);
}else{
holder.dlimg.setImageResource(R.drawable.<default_image>);
}
答案 1 :(得分:1)
您应该在上次使用后清除ViewHolder
中的图像。只需将您的代码更改为以下内容:
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)
}