2011-03-26 79 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