2011-10-20 39 views
0

在我的應用程序中,我有一個字符串數組,我在其中存儲我的字符串。我行android,陣列適配器不顯示行的內容

private static String[] tokens = new String[1024]; 

格式,我在XML文件中創建,它是:

<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:shrinkColumns="*" 
android:stretchColumns="*" > 
    <TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:layout_marginTop="10dip" 
    android:layout_marginBottom="10dip" > 
     <TextView 
      android:id="@+id/outstanding_contractdate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#ffffff" 
      android:textSize="15sp" /> 
     <TextView 
      android:id="@+id/outstanding_contractno" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#ffffff" 
      android:textSize="15sp" /> 
     <TextView 
      android:id="@+id/outstanding_contractamount" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:textColor="#ffffff" 
      android:textSize="15sp" /> 
    </TableRow> 

</TableLayout> 

在的onCreate(),我定義爲:

lvOutstanding = (ListView) findViewById(R.id.outstanding_list); 
     myListAdapter = new MyListAdapter(); 
     lvOutstanding.setAdapter(myListAdapter); 

,這是我的適配器:

class MyListAdapter extends ArrayAdapter<String>{ 
     MyListAdapter(){ 
      super(MSOutStanding.this, R.layout.list_outstanding, tokens); 
     } 

     public View getView(int position, View convertView, ViewGroup parent){ 
      View row = convertView; 

      if(row == null){ 
       LayoutInflater inflater = getLayoutInflater(); 
       row = inflater.inflate(R.layout.list_outstanding, parent, false); 
      } 

      TextView tvContDate = (TextView) row.findViewById(R.id.outstanding_contractdate); 
      TextView tvContNo = (TextView) row.findViewById(R.id.outstanding_contractno); 
      TextView tvContAmount = (TextView) row.findViewById(R.id.outstanding_contractamount); 

      if(position == 0) 
       count = 0; 
      else 
       count = 3; 

      if(arrayLoop>0){ 
       tvContDate.setText(tokens[position * count]); 
       Log.i("Count:", tokens[position * count]); 

       tvContNo.setText(tokens[position * count + 1]); 
       Log.i("Count:", tokens[position * count +1]); 

       tvContAmount.setText(tokens[position * count + 2]); 
       Log.i("Count:", tokens[position * count+2]); 

       arrayLoop -= 3; 
       Log.i("Count:", String.valueOf(arrayLoop)); 
      } 

      return(row); 
     } 
    } 

正如你所看到的,在每一行我有三個textView,我想在每一行顯示每個三項數組。 「arrayLoop」是和int變量,保存了「tokens []」數組中的項目數。

現在,當我運行應用程序時,仿真器顯示1024行而沒有任何數據。我的錯誤在哪裏? 當我檢查logcat(感謝谷歌新的美麗的設計logCat!),沒有問題,並在數組中的前12項目有參數和那裏(12/3 = 4),我應該看到4行信息。不過,我有1024行,而不信息:(

enter image description here

回答

0

你arrayLoop變量必須小於或等於0,我認爲你的問題是,你已經把arrayLoop在你的意思是把數if語句。但我不能看到你定義arrayLoop或已分配給它的東西。

+0

arrayLoop是全局變量,其內容是我從服務器獲取的項目數。例如(在這種情況下),我從服務器中獲取12個項目並將其存儲在令牌數組中。 arrayLoop告訴我使用了tokens.lingth中的12個項目。 – Hesam

1

你需要寫如下的令牌令牌數組不是字符串類型。

if(arrayLoop>0){ 

     tvContDate.setText(String.valueOf(tokens[position * count])); 
     Log.i("Count:", tokens[position * count]); 

     tvContNo.setText(String.valueOf(tokens[position * count])); 
     Log.i("Count:", tokens[position * count +1]); 

     tvContAmount.setText(String.valueOf(tokens[position * count])); 
     Log.i("Count:", tokens[position * count+2]); 

     arrayLoop -= 3; 
     Log.i("Count:", String.valueOf(arrayLoop)); 
    } 

因爲當你使用tvContAmount.setText(tokens[position * count]); 它會認爲它是資源ID。它不會得到這個,因此它不會顯示任何輸出。

+0

謝謝。這不是問題,因爲令牌[]是字符串,我想在每個視圖中設置字符串。 – Hesam

+0

你有來自日誌的數據,我們可以得出結論它正在循環正確。問題是你無法看到數據。通過放置'tvContDate.setText(「Test」)來檢查;'它可以顯示文本。如果是,那麼問題出現在'tokens []'數組中 – Vivek