我正在将数据上传到服务器,如果数据成功上传到服务器,那么我显示“已保存”,就像您可以看到“已上传”的图片一样。
但问题是,我已存储第一个项目行的数据,而在不同的行项目中获取“已保存”文本
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();
}
答案 0 :(得分:3)
问题是您的适配器不知道哪一行上传到服务器的数据。您需要告诉适配器。至于“如何告诉适配器?”这个问题,您已经有了一个列表ImageList
。我们只需要编辑它。
现在,将另一个bool添加到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();
.
.
现在,根据您当前的代码,我认为通过构造函数将position
传递给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);
}
});
如果您需要任何解释,请发表评论。
答案 1 :(得分:1)
尝试此修改后的getView(...)
,请检查//added codes
。关键是如何在点击按钮时获取项目位置:
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
// Avoid unneccessary calls to findViewById() on each row, which is
// expensive!
holder = null;
if (convertView == null) {
... ...
} else {
holder = (ViewHolder) convertView.getTag();
}
//added codes
holder.btnUpload.setTag(new Integer(position));
holder.btnData.setTag(new Integer(position));
... ...
// btnUpload
holder.btnUpload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Upload
//if you need position here, get it by:
//added codes
//Button clickBtn = (Button)v;
//int position = ((Integer)clickBtn.getTag()).intValue();
..........
}
});
holder.btnData.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//added codes
Button clickBtn = (Button)v;
int position = ((Integer)clickBtn.getTag()).intValue();
strPath = ImageList.get(position).getImages().toString();
fileName = strPath.substring(strPath.lastIndexOf('/') + 1, strPath.length());
showDialog(DIALOG_DATA);
}
});
if(ImageList.get(position).isUploaded()){
holder.textDataStatus.setText("Saved");
}else{
holder.textDataStatus.setText("");
}
return convertView;
}
希望这有帮助!
答案 2 :(得分:0)
尝试包含
public View getView(final int position, View convertView,
ViewGroup parent) {
// Avoid unneccessary calls to findViewById() on each row, which is
// expensive!
holder = null;
int crntposition = position;
if (convertView == null) {
convertView = getLayoutInflater().inflate(
R.layout.adapter_upload_images, null);
holder = new ViewHolder(convertView);
// Create a ViewHolder and store references to the children
// views
holder.textName = (TextView) convertView
.findViewById(R.id.textName);
holder.thumbnail = (ImageView) convertView
.findViewById(R.id.thumbnail);
holder.textStatus = (TextView) convertView
.findViewById(R.id.textStatus);
holder.textDataStatus = (TextView) convertView
.findViewById(R.id.textDataStatus);
holder.btnUpload = (Button) convertView
.findViewById(R.id.btnUpload);
holder.btnData = (Button) convertView
.findViewById(R.id.btnData);
// The tag can be any Object, this just happens to be the
// ViewHolder
convertView.setTag(holder);
convertView.setTag(R.id.textDataStatus, holder.textDataStatus);
holder.textDataStatus.setTag(crntposition);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnUpload.setEnabled(ImageList.get(position)
.isStatusEnable());
holder.textStatus.setText(ImageList.get(position).getMessage());
holder.textDataStatus.setText(ImageList.get(Integer.parseInt(holder.textDataStatus.getTag().toString())).getData());
strPath = ImageList.get(crntposition)).getImages().toString();
Log.e("IMAGE CLICK",strPath);
// Get File Name
fileName = strPath.substring(strPath.lastIndexOf('/') + 1, strPath.length());
holder.btnData.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
strPath = ImageList.get(position).getImages().toString();
fileName = strPath.substring(strPath.lastIndexOf('/') + 1, strPath.length());
showDialog(DIALOG_DATA);
}
});
return convertView;
}
}
..在适配器类的getView方法中初始化holder.textDataStatus之后