如何仅使用数组适配器和getview创建简单的列表视图。不在Android中使用ListActivity和setAdapter。请详细说明示例代码。
修改: 我是初学者,很困惑,不熟悉有关堆栈溢出的问题。 实际上我找到了在任何活动类中动态创建适配器和列表视图的解决方案。希望downvoted将再次重新计算!!!
答案 0 :(得分:1)
也许你在找这个?
<强> SampleListActivity.java 强>
package com.academy;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SampleListActivity extends Activity {
private String[] mStrings = {
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
"Acorn", "Adelost", "Affidelice au Chablis"};
private ListView listView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mStrings));
}
}
<强> main.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="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>