2011-03-26 28 views
0

我正在學習在android中構建UI,我正試圖學習如何使用ListViews。我看到了d.android.com教程,但它使用ListActivity而不是在Activity中使用ListView。Android列表視圖forceclosing

所以我開始試驗,但我的應用程序被強制關閉。誰能告訴我什麼是錯的?

的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="fill_parent" 
    android:layout_height="fill_parent"> 

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

</LinearLayout> 

row_item.xml:

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

    <TextView android:text="TextView" android:id="@+id/label" 
     android:layout_width="fill_parent" android:layout_height="wrap_content"> 
    </TextView> 

</LinearLayout> 

ListTest.java:

public class ListTest extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     String items[] = {"harry", "tony", "solo"}; 

     ListView lv = (ListView) findViewById(R.id.theList); 
     lv.setAdapter(new ArrayAdapter<String>(this, R.layout.row_item, items)); 

    } 
} 

我與Android初學者,我不能夠找到任何明顯錯誤的代碼。任何幫助?

謝謝:)

+0

帖子來自'LogCat'視圖的錯誤日誌。 – 2011-03-26 10:15:08

回答

0

問題是因爲您使用ArrayAdapter來填充您的row_item中的數據。

ArrayAdapter只要您使用Android提供的內置佈局就可以工作。

所以,

lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items)); 

這應該工作,報告回來,如果它不與logcat的細節。

如果你堅持使用你自己的row_item佈局的行,你將不得不使你的custom adapter

更新2012年9月20日:如果您的佈局只有一個TextView的,那麼你仍然可以使用香草ArrayAdapter,在此構造

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

int resourceR.layout.row_itemint textViewResourceIDR.id.label您打算在其中顯示數據的文本視圖。

0

好吧,我知道你犯了什麼錯誤。 main.xml中文件,在ListView中更改行從機器人:ID = 「@ + ID /的thelist」到以下,

android:id="@+id/list" 
android:layout_width="fill_parent" 

也改變這一行公共類ListTest延伸活動{至以下,

public class ListTest extends ListActivity { 

試試這個,它會工作。

如果你想了解列表視圖,這裏是漂亮的文章,http://blog.sptechnolab.com/2011/02/01/android/android-custom-listview-items-and-adapters/

+0

這不是必須的,因爲他使用的是Activity,如果他使用的是ListActivity,那麼你的答案就會成立。 – st0le 2011-03-26 09:27:52

+0

如果你只想使用「Activity」,那麼你不能使用lv.setAdapter(new ArrayAdapter (this,R.layout.row_item,items));因爲它會產生錯誤。 – user609239 2011-03-26 09:42:29

+0

我知道,我回答了這個問題。 – st0le 2011-03-26 09:55:45

0

ArrayAdapter

「要使用比TextViews爲陣顯示器以外的東西,比如,ImageViews,或有一些數據除了的toString()結果填補意見,覆蓋getView(INT,查看,ViewGroup)返回你想要的視圖類型。「

所以,你需要給你自己的自定義ArrayAdapter類,並將其設置爲ListView。

0

如果你想顯示字符串數組,你可以使用此代碼:

你的main.xml文件應該是這樣:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"> 
<ListView android:id="@+id/ListView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 
</LinearLayout> 

內MainActivity類別:

public class ListviewExample extends Activity 
{ 

private ListView lv1; 
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"}; 
@Override 
public void onCreate(Bundle icicle) 

{ 
super.onCreate(icicle); 

setContentView(R.layout.main); 

lv1=(ListView)findViewById(R.id.ListView01); 
// By using setAdpater method in listview we an add string array in list. 
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr)); 
} 
} 
+0

但是,如果您想要自定義列表視圖,就像您的帖子一樣,請參閱本文中的示例[here](http://www.vogella.de/articles/AndroidListView/article.html#listsactivity_different)。 – 2011-03-26 10:15:25