开发者社区> 问答> 正文

安卓使用listview时activity中的setadapter()方法报错问题

另外系统为什么找不到setListAdapter()方法,还有activity可以继承自AppCompatActivity吗
这是activity代码:

public class MainActivity extends ListActivity {
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SimpleAdapter adapter=new SimpleAdapter(this,getData(),R.layout.item,new String[]{"name","grade"},new int[]{R.id.item1,R.id.item2});
       listView.setAdapter(adapter);
    }
    private List<Map<String,Object>> getData()
    {
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        Map<String,Object> map =new HashMap<String,Object>();
        map.put("name","张三");
        map.put("grade", "大一");
        list.add(map);
        map.put("name","李四");
        map.put("grade", "大二");
        list.add(map);
        map.put("name","王五");
        map.put("grade", "大三");
        list.add(map);
        map.put("name","赵六");
        map.put("grade", "大四");
        list.add(map);
        map.put("name","TOM");
        map.put("grade", "退学");
        list.add(map);
        return list;
    }
}

xml代码:

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

    <TextView
        android:id="@+id/item1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/item2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

这是manifest.xml代码

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.application" >
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

展开
收起
蛮大人123 2016-03-06 16:50:39 4647 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    就第一段代码来说,这是一段错误代码,布局文件未加载,listView变量未进行初始化就使用,这都是比较明显而低级的错误。另外,系统ListView不存在setListAdapter(),有setAdapter()相关方法。

    2019-07-17 18:54:52
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载