android-视图持有人稳定性问题
作者:互联网
我正在将数据上传到服务器,并且如果数据成功上传到服务器,那么我会显示“已保存”,就像您可以看到图像的“已上传”一样.
但是问题是,我已经为第一行存储了数据,而在不同的行项目中获取了“已保存”文本
holder.textDataStatus.setText(ImageList.get(position).getData());
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
try {
// Prepare Save Data
if(strStatusId.equals("0"))
{
Toast.makeText(UploadImagesActivity.this, "Unable to upload", Toast.LENGTH_SHORT).show();
}
else if (strStatusId.equals("1"))
{
Toast.makeText(UploadImagesActivity.this, "Data Uploaded Successfully", Toast.LENGTH_SHORT).show();
ImageList.get(position).setData("Saved");
Log.d("strData:", fileName); // getting correct tapped item
mAdapter.notifyDataSetChanged();
}
else
{
Toast.makeText(UploadImagesActivity.this, "Unable to upload", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// TODO: handle exception
}
if (file_url != null){
Toast.makeText(UploadImagesActivity.this, file_url, Toast.LENGTH_LONG).show();
}
解决方法:
问题是您的适配器不知道该行的数据已上载到服务器.您需要告诉适配器.至于问题“如何告诉适配器?”,您已经有一个列表ImageList.我们只需要编辑它.
现在,向您的MyData类添加另一个布尔值,例如:boolean Uploaded = false;并为其创建getter setter.
将以下行添加到您的getView()中:
if(ImageList.get(position).isUploaded()){
holder.btnUpload.setText("Save");
}else{
holder.btnUpload.setText("Upload");
}
现在,我们需要在上传完成后将此值设置为true.我们应该只从UploadData类中执行此操作.为此,我们需要将位置发送到UploadData类.我们可以通过如下构造函数来做到这一点:
class UploadData extends AsyncTask<String, String, String> {
private ProgressDialog pDialog;
int position;
//constructor to pass position of row, on which button was clicked to class
public UploadData(int position){
this.position=position;
}
/**
* Before starting background thread Show Progress Dialog
* */
.
.
.
.
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
//after upload is done, set value to true
ImageList.get(position).setUploaded(true);
//now we need to notify our adapter so that it can reflect changes
mAdapter.notifyDataSetChanged();
.
.
现在,根据您当前的代码,我认为通过构造函数将位置值传递给UploadData确实很困难.因此,您可以通过在类的全局变量中设置一个值来尝试.
编辑1:
将位置传递给您的holder.btnData.SetOnClickListener中的全局变量,如下所示:
holder.btnData.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
//pass position into activity's global variable
pos = position/*position from adapter*/;
strPath = ImageList.get(position).getImages().toString();
fileName = strPath.substring(strPath.lastIndexOf('/') + 1, strPath.length());
showDialog(DIALOG_DATA);
}
});
如果您需要任何解释,请发表评论.
标签:android-viewholder,listview,android 来源: https://codeday.me/bug/20191028/1952966.html