需要在android應用中顯示listView。我沒有繼承listActivity,不知道是不是這個引起錯誤。
代碼:
public class Home extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
startLocation();
createMenuTabs();
final String[] datos = new String[]{"Lista 1","Lista 2","Lista 3","Lista 4","Lista 5"};
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datos);
ListView urban_lines_list = (ListView)findViewById(R.id.urban_lines_list);
urban_lines_list.setAdapter(adaptador);
}
}
其中XML文件:
<LinearLayout android:id="@+id/Urbano"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="@+id/urban_lines"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/list_of_lines"/>
<ListView android:id="@+id/urban_lines_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
因為你的xml文件中TextView的高度設置為match_parent,導致ListView不顯示。
<TextView android:id="@+id/urban_lines"
android:layout_width="match_parent"
android:layout_height="match_parent"