I have finally found answer to this.
Here is the ListAdapter Class:
class ListAdapter extends ArrayAdapter<Modal> {
private final Context context;
private int img;
private List<Modal> myList;
private File filenames[];
private List<Integer> selectedItems=new ArrayList<Integer>();
private CheckBox cb;
public ListAdapter(Context context, List<Modal> list, File filenames[]) {
super(context, R.layout.list_item, list);
this.context = context;
this.myList = list;
this.filenames=filenames;
}
@Override
public int getViewTypeCount() {
if(getCount()>0){
return getCount();
}
return 1;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
static class ViewHolder {
TextView tv;
CheckBox cb;
static ImageView iv;
}
public int getImage(File filename){
if(String.valueOf(filename).contains(".rar")||String.valueOf(filename).contains(".zip")){
return R.drawable.archive;
}
else if(String.valueOf(filename).contains(".doc")||String.valueOf(filename).contains(".docx")){
return R.drawable.doc;
}
else if(String.valueOf(filename).contains(".pdf")){
return R.drawable.pdf;
}
else if(String.valueOf(filename).contains(".txt")){
return R.drawable.txt;
}
else if(String.valueOf(filename).contains(".ppt")||String.valueOf(filename).contains(".pptx")){
return R.drawable.ppt;
}
else if(String.valueOf(filename).contains(".xls")||String.valueOf(filename).contains(".xlsx")){
return R.drawable.xls;
}
else if((String.valueOf(filename).contains(".jpeg")||String.valueOf(filename).contains(".jpg")||String.valueOf(filename).contains(".png")||String.valueOf(filename).contains(".bmp")||String.valueOf(filename).contains(".webp")||String.valueOf(filename).contains(".gif"))){
return R.drawable.image;
}
else if((String.valueOf(filename).contains(".mp4")||String.valueOf(filename).contains(".mkv")||String.valueOf(filename).contains(".webm")||String.valueOf(filename).contains(".3gp")||String.valueOf(filename).contains(".3gpp"))||String.valueOf(filename).contains(".MOV")){
return R.drawable.video;
}
else if((String.valueOf(filename).contains(".mp3")||String.valueOf(filename).contains(".wav")||String.valueOf(filename).contains(".wma")||String.valueOf(filename).contains(".MP2")||String.valueOf(filename).contains(".AAC")||String.valueOf(filename).contains(".AC3")||String.valueOf(filename).contains(".AU")||String.valueOf(filename).contains(".OGG")||String.valueOf(filename).contains(".FLAC"))){
return R.drawable.music;
}
else {
return R.drawable.unknown;
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
File file=new File(String.valueOf(filenames[position]));
if(file.isDirectory()){
img=R.drawable.folder;
}
else{
img=getImage(filenames[position]);
}
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = LayoutInflater.from(getContext());
convertView = inflator.inflate(R.layout.list_item, null);
viewHolder = new ViewHolder();
viewHolder.tv = (TextView) convertView.findViewById(R.id.textView1);
viewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
viewHolder.iv= (ImageView)convertView.findViewById(imageView);
viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
myList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.textView1, viewHolder.tv);
convertView.setTag(R.id.checkBox1, viewHolder.cb);
convertView.setTag(R.id.checkBox1, viewHolder.iv);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.iv.getLayoutParams().height = 150;
viewHolder.iv.getLayoutParams().width = 150;
viewHolder.cb.setTag(position);
final ViewHolder ViewHolder= viewHolder;
ViewHolder.tv.setText(myList.get(position).getName());
ViewHolder.cb.setChecked(myList.get(position).isSelected());
viewHolder.iv.setImageResource(img);
Log.v("this","viewHolderMT: "+String.valueOf(viewHolder)+" position: "+position);
myTask m=new myTask(position,viewHolder.iv);
m.execute();
return convertView;
}
class myTask extends AsyncTask<Integer,Bitmap,Bitmap>{
int pos;
ImageView iv;
public myTask(int pos,ImageView iv){
this.pos=pos;
this.iv=iv;
iv.setTag(pos);
}
@Override
protected Bitmap doInBackground(Integer... params) {
Bitmap thumb=null;
if((String.valueOf(filenames[pos])).contains(".jpeg")||String.valueOf(filenames[pos]).contains(".jpg")||String.valueOf(filenames[pos]).contains(".png")||String.valueOf(filenames[pos]).contains(".bmp")||String.valueOf(filenames[pos]).contains(".webp")||String.valueOf(filenames[pos]).contains(".gif")){
thumb = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(String.valueOf(filenames[pos])), 150, 150);
}
else if((String.valueOf(filenames[pos])).contains(".mp4")||String.valueOf(filenames[pos]).contains(".mkv")||String.valueOf(filenames[pos]).contains(".webm")||String.valueOf(filenames[pos]).contains(".3gp")||String.valueOf(filenames[pos]).contains(".3gpp")||String.valueOf(filenames[pos]).contains(".MOV")){
thumb=ThumbnailUtils.createVideoThumbnail(filenames[pos].getPath(), MediaStore.Video.Thumbnails.MINI_KIND);
}
return thumb;
}
@Override
protected void onProgressUpdate(Bitmap... thumb){
}
@Override
protected void onPostExecute(Bitmap thumb){
if(thumb!=null && (Integer)iv.getTag()==this.pos) {
if ((String.valueOf(filenames[pos])).contains(".jpeg") || String.valueOf(filenames[pos]).contains(".jpg") || String.valueOf(filenames[pos]).contains(".png") || String.valueOf(filenames[pos]).contains(".bmp") || String.valueOf(filenames[pos]).contains(".webp") || String.valueOf(filenames[pos]).contains(".gif")) {
iv.setImageBitmap(thumb);
}
else if ((String.valueOf(filenames[pos])).contains(".mp4") ||String.valueOf(filenames[pos]).contains(".MOV")|| String.valueOf(filenames[pos]).contains(".mkv") || String.valueOf(filenames[pos]).contains(".webm") || String.valueOf(filenames[pos]).contains(".3gp") || String.valueOf(filenames[pos]).contains(".3gpp")) {
iv.setImageBitmap(thumb);
}
}
}
}
}
Mistakes I was making:
- Move AsyncTask outside getview()
- Give a reference to the imageView inside the async constructor
- Also Implement the getItemViewType() and getItemId() functions
- Was not checking the position tag in onPostExecute so the images were shuffling.
This took a while for me to figure out. Hope it helps others!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…