2015-04-01 70 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