注册 登录
编程论坛 Android开发

关于ListView的问题

yu1543054075 发布于 2015-11-20 22:12, 3267 次点击
strings.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ListView</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string-array name="weekday">
        <item>星期日</item>
        <item>星期一</item>
        <item>星期二</item>
         <item>星期三</item>
          <item>星期四</item>
           <item>星期五</item>
            <item>星期六</item>
    </string-array>

</resources>

activity_main.xml文件
<LinearLayout xmlns:android="http://schemas.
    xmlns:tools="http://schemas.
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="20dp"
        android:drawSelectorOnTop="false" ></ListView>

</LinearLayout>

package com.example.listview;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {
    private TextView textView;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView)findViewById(R.id.text);
        listView = getListView();//getListView()是ListActivity中的方法,返回一个ListView
        listView.setTextFilterEnabled(true);
        
        ArrayAdapter<CharSequence> adap = ArrayAdapter.createFromResource(this, R.array.weekday, android.R.layout.simple_list_item_1);
        setListAdapter(adap);
        
        AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                textView.setText(((TextView)view).getText());//设置用户单击的项目的文字显示在textView上
            }
        };
        listView.setOnItemClickListener(listener);
   
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
        
    }
}
结果一运行,就自动退出了,不知道哪里出错了,还请各位帮帮忙,谢谢了
3 回复
#2
tlliqi2015-11-20 22:41
帮顶
#3
kevin_012015-11-29 21:26
android:id="@+id/list"  这个listview 的id 有问题 ,
你既然用了listActivity 来得到listview ,那么listview 的id 应该是  android:id="@android:id/list" , 这个必须有注意的
#4
yu15430540752015-12-02 21:28
非常感谢
1