2015-04-01 33 views
3

我正在上传数据到服务器,并且如果数据成功上传到服务器,那么我显示“已保存”,就像您可以看到“已上传”图像一样。查看持有人稳定性问题

但问题是,我已经存储的数据的第一个项目行,而得到不同的行项目“拯救”文本

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

您是否使用标头?这将是您的适配器中的第一个元素。 – Booger 2015-04-01 07:32:08

+0

你在哪里调用'setAdapter()'或'setListAdapter()'? – 2015-04-01 07:36:53

+0

我已经发布整个代码,请现在检查 – Sun 2015-04-01 07:43:01

回答

3

问题是你的适配器不知道那一行上传到服务器的数据。你需要告诉你的适配器。至于“如何告诉适配器?”这个问题,你已经有一个列表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); 

      } 
     }); 

请发表评论,如果您需要任何解释。

+0

的行仍然会出现同样的问题 – Sun 2015-04-07 12:26:27

+0

@Sun你是如何将位置传递给你的'UploadData'类的? – DroidDev 2015-04-07 13:50:42

+0

我已经根据上面的建议检查更新了我的代码,现在我正在使用类似下面的内容:new UploadData(position).execute();我已经在全班声明了int位置... – Sun 2015-04-08 04:51:00

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; 

      } 
     } 

..after初始化holder.textDataStatus里面的你的适配器类的getView方法

+0

嘿,请检查我发布的代码,并让我知道的方式... – Sun 2015-04-01 07:42:44

+0

@孙看编辑代码 – 2015-04-01 07:50:00

+0

使用您的代码获取异常/ .. – Sun 2015-04-01 09:14:25

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; 

    } 

希望得到这个帮助!

+0

@sun,你有没有试过这个解决方案 – Xcihnegn 2015-04-08 16:01:21

+1

使用位置作为标签比使用全局位置变量更好,因为它总是指向正确的位置。像这样的东西我会打字,但Xchinegn打我:) – 2015-04-09 01:04:02

+0

@AshtonEngberg我同意你的意见。使用全局变量是我想要做的最后一件事,但在OP的情况下,他们需要通过多种方法传递行的位置。所以,或者在那个机制中有很多改变(显示对话框等(现在从问题中删除哪个部分))或全局变量。 – DroidDev 2015-04-21 12:42:07