3

我正在尝试为微调器和列表做一个简单的编码,这只是为了显示数据。但是,我不知道为什么我为列表视图编写的数据不显示。谁能帮我解决这个问题?

这是我的编码。

MainActivity.java

public class MainActivity extends Activity {

    String[] Category = {
    "Science Trail",
    "Megabug Return"

};

String [] months = {
    "June",
    "July",
    "August",
    "Septemeber",
    "November",
    "December",


};

Spinner s1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //GridView
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Category)); 

        //SpinnerView
        s1 = (Spinner) findViewById(R.id.spinner1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, months);
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
                int index = s1.getSelectedItemPosition();
                Toast.makeText(getBaseContext(), "You have seleted item :" + months[index] , Toast.LENGTH_SHORT).show();
            }
            public void onNothingSelected(AdapterView<?>arg0) {}
            });
        }


        private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
        // TODO Auto-generated method stub

    }


        public void onListItemClick(ListView parent, View v, int position,long id)
        {
        Toast.makeText(this, "You have selected " + Category[position], Toast.LENGTH_SHORT).show();
        }
        }

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

 <ListView
     android:id="@+id/android:list"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/spinner1"
     android:layout_marginTop="60dp" >

                    </ListView>
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/listView1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="22dp" />

</RelativeLayout>
4

3 回答 3

3

你需要做两件事。

  1. 扩展 ListActivity 而不是 Activity。
  2. 删除方法 setListAdapter。
于 2013-07-28T08:13:22.943 回答
2

您的 Activity 必须扩展ListActivity

使用以下

      public class MainActivity extends ListActivity {

    String[] Category = {
    "Science Trail",
    "Megabug Return"

};

String [] months = {
    "June",
    "July",
    "August",
    "Septemeber",
    "November",
    "December",


};

Spinner s1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //GridView
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Category)); 

        //SpinnerView
        s1 = (Spinner) findViewById(R.id.spinner1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, months);
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
                int index = s1.getSelectedItemPosition();
                Toast.makeText(getBaseContext(), "You have seleted item :" + months[index] , Toast.LENGTH_SHORT).show();
            }
            public void onNothingSelected(AdapterView<?>arg0) {}
            });
        }


}

删除这个

     private void setListAdapter(ArrayAdapter<String> arrayAdapter) {

     }

public void setListAdapter (ListAdapter adapter)// ListActivity 方法

在 API 级别 1 中添加

为列表视图提供光标。

更多信息 @

http://developer.android.com/reference/android/app/ListActivity.html

我的设备上的快照

在此处输入图像描述

于 2013-07-28T08:21:13.573 回答
0

你的 setListAdapter 方法是空的......你有 2 个选项。1. 将 ListView 添加到您的布局中并使用 findViewById 来初始化列表视图的适配器。2. 正如 Raghunandan 所说,扩展 ListActivity 并调用 ListActivity 的 setListAdapter,而不是您的自定义和空的。

于 2013-07-28T08:15:12.857 回答