Reputation: 6888
In my app I am using upload Button
and Progressbar
in each and every list row, and I am able to upload images to server as well, but facing a small issue, whenever I do tap on upload button for a list item, it always show Progressbar
at the last list item row which is visible... why?
Whereas it has to be show Progressbar
in a same row, for which i am uploading image...
For example: I have 3 images in a list, namely 03.jpg, 02.jpg and 01.jpg, so whenever I do tap on upload button of 03.jpg or 02.jpg or 01.jpg, everytime i am getting progressbar on 01.jpg row !
and in my list only three records are visible, to view more need to scroll...
ViewHolder holder ;
View v ;
/*** Get Images from SDCard ***/
ImageList = getSD();
// ListView and imageAdapter
lstView = (ListView) findViewById(R.id.listView1);
lstView.setAdapter(new ImageAdapter(this));
}
public static List <String> getSD()
{
List <String> it = new ArrayList <String>();
String string = "/mnt/sdcard/Pictures/SamCam/";
f = new File (string+ CameraLauncherActivity.folder+ "/");
files = f.listFiles ();
/***
* to show last taken image on top using lastModified
* to sort data
* to refresh data after (remove or update)
*/
Arrays.sort(files, new Comparator<Object>()
{
public int compare(Object o1, Object o2) {
if (((File)o1).lastModified() > ((File)o2).lastModified()) {
return -1;
} else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
return +1;
} else {
return 0;
}
}
});
// <<<<<<<<< END >>>>>>>>>>>
for (int i = 0; i < files.length; i++)
{
file = files[i];
Log.d("Count",file.getPath());
it.add (file.getPath());
}
return it;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Avoid unneccessary calls to findViewById() on each row, which is expensive!
holder = null;
// If this item is to be synced
if(flags.get(position)) {
startUpload(position);
// Mark as synced
flags.put(position, false);
}
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_upload, null);
holder = new ViewHolder(convertView);
holder.uploadProgressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.uploadProgressBar.setVisibility(View.GONE);
..................................
// btnUpload
holder.uploadImageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Upload
startUpload(position);
}
});
return convertView;
}
}
//Upload
public void startUpload(final int position) {
Runnable runnable = new Runnable() {
public void run() {
handler.post(new Runnable() {
public void run() {
v = lstView.getChildAt(position - lstView.getFirstVisiblePosition());
holder.uploadProgressBar.setVisibility(View.VISIBLE);
holder.statusImageView.setImageResource(R.drawable.bullet_button);
new UploadFileAsync().execute(String.valueOf(position));
}
});
}
};
new Thread(runnable).start();
}
Upvotes: 0
Views: 803
Reputation: 111
It looks like you're using the holder
variable in your runnable in startUpload, but you're never initializing it so basically you're reusing the last ViewHolder
instance that was assigned to the holder
variable.
Try something like this:
public void run() {
v = lstView.getChildAt(position - lstView.getFirstVisiblePosition());
holder = (ViewHolder) v.getTag();
holder.uploadProgressBar.setVisibility(View.VISIBLE);
holder.statusImageView.setImageResource(R.drawable.bullet_button);
new UploadFileAsync().execute(String.valueOf(position));
}
});
}
};
Upvotes: 1
Reputation: 6728
just try to use Tag for button as following.
holder.uploadImageButton.setTag(position); //add this line
holder.uploadImageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Upload
startUpload((Integer)holder.uploadImageButton.getTag()); //change this line
OR
startUpload((Integer)v.getTag()); //change this line
}
});
Upvotes: 0
Reputation: 389
Where do you initiate
holder.uploadProgressBar?
I think
position
is your problem..
you always get the last item progressbar because your
position
is always the last item's position. You need to initiate
uploadProgress
inside
(if (convertView == null) {}) method
and let it be rcycled every
getView
iteration..
You can use my code for reference:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Sub_Category sub_category = getItem(position);
ViewHolder holder;
if (convertView == null) {
//create new view
convertView = LayoutInflater.from(getContext()).inflate(R.layout.hit_me_sub_row,parent,false);
holder = new ViewHolder();
holder.uploadProgressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(SOME CONDITION)
holder.uploadProgressBar .setVisibility(8);
else if(OTHER CONDITION)
{
holder.uploadProgressBar .setVisibility(8);
}
return convertView;
}
Upvotes: 0