在我的应用中,我在每个列表行中都使用了上传Button
和Progressbar
,我也能够将图像上传到服务器,但是每当我点击时都会面临一个小问题在列表项的上传按钮上,它总是在最后一个列表项行显示Progressbar
,这是可见的......为什么?
然而必须在同一行显示Progressbar
,我要为其上传图片...
例如:我在列表中有3个图像,即03.jpg,02.jpg和01.jpg,所以每当我点击03.jpg或02.jpg或01.jpg的上传按钮时,每次我我在01.jpg排上获得进度条!
并且在我的列表中只能看到三条记录,以查看更多需要滚动...
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();
}
答案 0 :(得分:1)
看起来你在startUpload中使用runnable中的holder
变量,但是你永远不会初始化它,所以基本上你正在重用分配给{的最后一个ViewHolder
实例{1}}变量。
尝试这样的事情:
holder
答案 1 :(得分:0)
你在哪里发起
holder.uploadProgressBar?
我认为
position
是你的问题..
你总是得到最后一项进度条,因为你的
position
始终是最后一个项目的位置。 你需要发起
uploadProgress
内
(if (convertView == null) {}) method
让每个人都进行回收
getView
迭代..
您可以使用我的代码作为参考:
@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;
}
答案 2 :(得分:0)
尝试使用Tag for按钮,如下所示。
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
}
});