2012-09-18 134 views
2

請指教爲什麼如果我嘗試獲取數組的長度,我的小應用程序總是出錯。在java中獲取數組的長度

請在下面找到我的代碼:

1串分析器

package dl.data.background; 

    import java.util.ArrayList; 
     public class StringParser { 

     public StringParser() 
     { 

     } 

     public ArrayList<Object> Parse (String input) { 
      ArrayList<Object> output = new ArrayList <Object>(); 

      int pointer = 0; 
      String data = ""; 

      while (pointer <= input.length()-1) 
      { 
       if(input.charAt(pointer) == '#'){ 
        output.add(data); 
        data = ""; 
        pointer++; 
       } 
       if(pointer < input.length()) 
       { 
        data += input.charAt(pointer); 
       } 
       pointer++; 
      } 
      return output; 
     } 
    } 

2.下載數據

package dl.data.background; 

    import java.io.BufferedReader; 
    import java.io.InputStream; 
    import java.io.InputStreamReader; 
    import java.util.ArrayList; 

    import org.apache.http.HttpResponse; 
    import org.apache.http.client.methods.HttpGet; 
    import org.apache.http.impl.client.DefaultHttpClient; 

    import android.app.Activity; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.TextView; 

    public class BackgroundDataActivity extends Activity { 
    private TextView textView; 



    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    textView = (TextView) findViewById(R.id.TextView01); 

    } 

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) { 
     String response = ""; 
     for (String url : urls) { 
     DefaultHttpClient client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(url); 
     try { 
      HttpResponse execute = client.execute(httpGet); 
      InputStream content = execute.getEntity().getContent(); 

      BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); 
      String s = ""; 
      while ((s = buffer.readLine()) != null) { 
      response += s; 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     } 
     return response; 
    } 

    protected void onPostExecute(String result) { 
     StringParser stringParser = new StringParser(); 
      ArrayList<Object> arrayList = stringParser.Parse(result); 

      Object [] output = arrayList.toArray(); 

      // how to get length of output array?? 
     textView.setText(output.length); 

     } 
    } 

    public void readWebpage(View view) { 
     DownloadWebPageTask task = new DownloadWebPageTask(); 
     String url1 = "http://atmbersama.16mb.com/atmlocator.php?ct=SEL_Id&lat=-6.302161&lng=106.743679&radius=20"; 

     task.execute(new String[] {url1}); 
     } 
    } 

如何得到輸出數組的長度?

textView.setText(output.length); 

回答

8

lengthInteger,和setText(CharSequence)期望一個CharSequence

textView.setText(String.valueOf(output.length)); 

代碼編譯精細,因爲事實上一個重載方法setText(int resid)實際取整數。但是,此方法將嘗試使用給定的ID加載資源。