2011-12-06 20 views
3

我已經嘗試過關於SO的類似問題的參考,但尚未得到相應的解決方案。多列列表中的nullPointerException

我試圖從網頁中獲取數據並以包含4列的行的格式顯示它。

出現在網頁數據:

SBIN ;1916.00;1886.85;[email protected] ;1315.50;1310.30;[email protected] ;1180.00;1178.00;[email protected] ;1031.30;1005.95;[email protected] ;1000.35;992.35;[email protected] ;931.90;916.35;[email protected] ;400.00;398.45;[email protected]

我想diaplay它的形式

SBIN.........1916.00.....1886.85.....1.54 

LT...........1315.50.....1310.30.....0.40 and so on. 

注意,我不想點,我想每個值是一個單獨的行內的列。

我的數據由7行組成。

當我運行下面的代碼,我得到這個輸出Screenshot

values[0] values[1] values[2] values[3] 

values[1] values[2] values[3] values[4] 

values[2] values[3] values[4] values[5] 

(它打印出所有4周的cols第1行,那麼COL第一排和第二排的COL1 2-4,然後第一排和第二排的山坳1-2等等的cols 3-4 ...)

ReadWebpageAsyncTask.java

public class ReadWebpageAsyncTask extends Activity { 
    private EditText ed; 
    private ListView lv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ed = (EditText) findViewById(R.id.ed); 
     lv = (ListView) findViewById(R.id.list); 
     DownloadWebPageTask task = new DownloadWebPageTask(); 
     task.execute(new String[] { "http://abc.com/default.aspx?id=G" }); 
    } 

    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; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      int sub = result.lastIndexOf('@', result.length() - 1); 
      String s1 = result.substring(0, sub + 2); 
      String temp[]; 
      String subarr[] = new String[100]; 
      ; 
      Log.v("data = ", s1); 
      // String s = s1.replace(";", " - "); 
      final String arr[] = s1.split("@"); 

      for (int i = 0; i < arr.length; i++) { 
       Log.v("arr" + i, arr[i] + " " + arr.length); 
      } 

      for (int i = 0; i < arr.length - 1; i++) 

      { 
       temp = arr[i].split(";"); 
       subarr[(4 * i)] = temp[0]; 
       subarr[(4 * i) + 1] = temp[1]; 
       subarr[(4 * i) + 2] = temp[2]; 
       subarr[(4 * i) + 3] = temp[3]; 
         } 
     lv.setAdapter(new MyAdapter(ReadWebpageAsyncTask.this, subarr)); 
     } 
    } 
} 

main.xml中

  <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    <EditText android:id="@+id/ed" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Search"> 
    </EditText> 

    <ListView android:id="@+id/list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    </ListView> 

    </LinearLayout> 

MyAdapter.java

public class MyAdapter extends ArrayAdapter<String> { 
    private final Context context; 
    private final String[] values; 

    private String item1, item2, item3, item0; 
    int x = 0, i = 1, y = 1; 

    public MyAdapter(Context context, String[] values) { 
     super(context, R.layout.row, values); 
     this.context = context; 
     this.values = values; 
    } 

    @Override 
    public String getItem(int position) { 
     return values[position]; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.row, parent, false); 
     TextView tv1 = (TextView) rowView.findViewById(R.id.col1); 
     TextView tv2 = (TextView) rowView.findViewById(R.id.col2); 
     TextView tv3 = (TextView) rowView.findViewById(R.id.col3); 
     TextView tv4 = (TextView) rowView.findViewById(R.id.col4); 

     if (y < 8) { 
      item0 = getItem(position); 
      Log.v("pos = ", "" + position); 
      item1 = getItem(position + 1); 
      item2 = getItem(position + 2); 
      item3 = getItem(position + 3); 

      tv1.setText(item0); 
      tv2.setText(item1); 
      tv3.setText(item2); 
      tv4.setText(item3); 

     } else { 
      Log.v("y= ", "" + y); 
     } 
     return rowView; 
    } 
} 

row.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" > 

      <TextView android:id="@+id/col1" 
      android:layout_width="150dip" 
      android:layout_height="wrap_content"/> 

      <TextView android:id="@+id/col2" 
      android:layout_width="70dip" 
      android:layout_height="wrap_content"/> 

       <TextView android:id="@+id/col3" 
      android:layout_width="70dip" 
      android:layout_height="wrap_content"/> 

       <TextView android:id="@+id/col4" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

    </LinearLayout> 

任何幫助APPRICIATED

回答

2

嘗試添加ListView控件到你的XML,並將在新的XML 上表中的內容通過擴展ArrayAdapter創建一個適配器,並設置你的ListView此適配器。

+0

請檢查http://stackoverflow.com/questions/8385428/display-data-in-seperate-columns-in-list我已經通過使用listview做到了這一點。但顯示格式不正確。 – GAMA

+0

你總是可以根據你的需要創建一個自定義的xml。這就是我的意思。我的意思是用4個textview創建一個Layout,每個按照你的需求分開。使用tableView以編程方式可以非常忙碌。 –

+0

請看上面的代碼和幫助,如果你可以! – GAMA

0

不要在Java代碼中添加視圖,因爲它們是基於XML文件添加的。它看起來像我的你的Java代碼重複了你在XML中所做的這是非常岌岌可危的...

我不明白你爲什麼不能刪除Java代碼並使用setContentView來使用XML佈局定義。使用的ListView

相反TableLayout的

+0

我不想要一行。 我想要創建多行,每行4列,並在運行時將數據添加到每個列。 – GAMA

+0

啊...我明白了。所以你正在循環中運行該代碼?你能發佈更多的代碼來展示你如何做這件事嗎?代碼更新了 – Craigy

+0

。目前我沒有使用循環,因爲我想插入數據從網頁中顯示在我的前面的問題http://stackoverflow.com/questions/8385428/display-data-in-seperate-columns-in-list – GAMA

0

將寬度設置爲0,併爲所有測試視圖添加權重1,它將爲該行中的每個項目均勻分配空間。

textView1.setWidth(0); 
    textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f)); 
+0

完成,但無法正常工作。仍然得到相同的錯誤。 – GAMA