2013-07-02 33 views
-1

我想打電話給startUpload(position);方法loginbutton.setOnClickListener(new View.OnClickListener() {}這樣的:如何申報方法在全球範圍內

loginbutton.setOnClickListener(new View.OnClickListener() { 

    @Override 
public void onClick(View v) { 
    SaveData();  
    alertDialog.dismiss(); 
    startUpload(position); 
} 

但總是越來越位置不能被解析爲一個變量

所以在這裏我我做錯了,我該如何解決這個問題..?

//Upload 
public void startUpload(final int position) {  
    Runnable runnable = new Runnable() { 

     public void run() { 
      handler.post(new Runnable() { 
       public void run() { 
        View v = lstView.getChildAt(position - lstView.getFirstVisiblePosition()); 
        // Show ProgressBar 
        ProgressBar progress = (ProgressBar)v.findViewById(R.id.progressBar); 
        progress.setVisibility(View.VISIBLE); 

        // Status 
        TextView status = (TextView)v.findViewById(R.id.ColStatus); 
        status.setText("Uploading.."); 

        new UploadFileAsync().execute(String.valueOf(position)); 
       } 
      }); 
     } 
    }; 
    new Thread(runnable).start(); 
} 


// Async Upload 
public class UploadFileAsync extends AsyncTask<String, Void, Void> { 

    String resServer; 
    int position; 

    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     position = Integer.parseInt(params[0]); 

     int bytesRead, bytesAvailable, bufferSize; 
     byte[] buffer; 
     int maxBufferSize = 1 * 1024 * 1024; 
     int resCode = 0; 
     String resMessage = ""; 

     String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 

     // File Path 
     strSDPath = ImageList.get(position).toString(); 

     // Upload to PHP Script 
     String strUrlServer = "http://10.0.2.2/res/uploadFile.php";    

     try { 
      /** Check file on SD Card ***/ 
      File file = new File(strSDPath); 
      if(!file.exists()) 
      { 
       resServer = "{\"StatusID\":\"0\",\"Error\":\"Please check path on SD Card\"}"; 
       return null; 
      } 

      FileInputStream fileInputStream = new FileInputStream(new File(strSDPath)); 

      URL url = new URL(strUrlServer); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 
      conn.setUseCaches(false); 
      conn.setRequestMethod("POST"); 

      conn.setRequestProperty("Connection", "Keep-Alive"); 
      conn.setRequestProperty("Content-Type", 
        "multipart/form-data;boundary=" + boundary); 

      DataOutputStream outputStream = new DataOutputStream(conn 
        .getOutputStream()); 
      outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
      outputStream 
      .writeBytes("Content-Disposition: form-data; name=\"filUpload\";filename=\"" 
        + strSDPath + "\"" + lineEnd); 
      outputStream.writeBytes(lineEnd); 

      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      buffer = new byte[bufferSize]; 

      // Read file 
      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

      while (bytesRead > 0) { 
       outputStream.write(buffer, 0, bufferSize); 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      } 

      outputStream.writeBytes(lineEnd); 
      outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

      // Response Code and Message 
      resCode = conn.getResponseCode(); 
      if(resCode == HttpURLConnection.HTTP_OK) 
      { 
       InputStream is = conn.getInputStream(); 
       ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

       int read = 0; 
       while ((read = is.read()) != -1) { 
        bos.write(read); 
       } 
       byte[] result = bos.toByteArray(); 
       bos.close(); 

       resMessage = new String(result); 
      } 

      Log.d("resCode=",Integer.toString(resCode)); 
      Log.d("resMessage=",resMessage.toString()); 

      fileInputStream.close(); 
      outputStream.flush(); 
      outputStream.close(); 

      resServer = resMessage.toString(); 

     } catch (Exception ex) { 
      // Exception handling 
      return null; 
     } 
     return null; 
    } 

    protected void onPostExecute(Void unused) { 
     statusWhenFinish(position,resServer); 
    } 
} 

// when upload finish 
protected void statusWhenFinish(int position, String resServer) { 

    View v = lstView.getChildAt(position - lstView.getFirstVisiblePosition()); 

    // Show ProgressBar 
    ProgressBar progress = (ProgressBar)v.findViewById(R.id.progressBar); 
    progress.setVisibility(View.GONE); 


    // Status 
    TextView status = (TextView)v.findViewById(R.id.ColStatus); 
    /*** Default Value ***/ 
    String strStatusID = "0"; 
    String strMessage = "Unknow Status!"; 

    try {  

     JSONObject c = new JSONObject(resServer); 
     strStatusID = c.getString("StatusID"); 
     strMessage = c.getString("Message"); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    // Prepare Status  
    if(strStatusID.equals("0")) 
    { 
     // When update Failed 
     status.setText(strMessage); 
     status.setTextColor(Color.RED); 

     // Enabled Button again 
     Button btnUpload = (Button) v.findViewById(R.id.btnUpload); 
     btnUpload.setText("Already Uploaded"); 
     btnUpload.setTextColor(Color.RED); 
     btnUpload.setEnabled(true); 
    } 
    else 
    { 
     status.setText("Upload Completed."); 
     status.setTextColor(Color.GREEN); 
    } 
} 
    } 
+3

位置變量的聲明在哪裏? –

回答

0

位置的聲明是關鍵。

確保它是包含此loginButton.onClickListener代碼,並確保它被聲明爲final

+0

檢查我的上面的代碼,並告訴我,我怎麼能做到這一點在我的情況 – Sun

+0

你還沒有顯示足夠的代碼,你顯示的代碼只是試圖引用一個變量,稱爲posiiton,這是沒有任何聲明。你需要解釋你期望從哪裏獲得職位。 – cowls

0

如果可以解析爲一個變量,那是因爲你的方法不行, 它的方法,因爲position不是屬性。

所以,delcare它:

int position = -1; 

然後將其設置爲使用方法。

+0

檢查我的上面的代碼,並告訴我,我怎麼能做到這一點在我的情況 – Sun

0

引用變量時,它們必須在方法範圍內是確定的。

因此,對於方法,請確保它作爲參數傳遞,或者它被定義爲類中的全局變量。

0

TRY IT 創建一個全局變量POSITION,然後在你的startUpload()方法中分配POSITION = position,它會起作用。

+0

我想你是對的,但我已經嘗試了好幾次,你能告訴我我應該如何寫在我的代碼 – Sun

+0

只是decalre POSITION變量在全局範圍意味着外部方法然後//上傳 public void startUpload(final int position){POSITION = position; Runnable runnable = new Runnable(){ public void run(){ – GOLDEE