查看支架稳定性问题



我正在上传数据到服务器,如果数据成功上传到服务器,那么我会显示"已保存",就像你可以看到图像的"已上传"一样。

但问题是,我已经存储了第一行项目的数据,而在不同的行项目中获得"已保存"文本

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类中添加另一个bool,如: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);
            }
        });

如果您需要任何解释,请留下评论。

尝试此修改的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;
    }

希望得到帮助!

尝试包含

    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后

最新更新