我正在尝试为微调器和列表做一个简单的编码, 这只是为了显示数据。 但是,我不知道, 为什么我为列表视图写的数据不显示。 任何人都可以帮我解决这个问题吗?
这是我的编码。
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>
答案 0 :(得分:3)
你需要做两件事。
答案 1 :(得分:2)
您的活动必须延长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
在我的设备上快照
答案 2 :(得分:0)
你的setListAdapter方法是空的...... 你有2个选择。 1.将ListView添加到布局中,并使用findViewById初始化列表视图的适配器。 2.正如Raghunandan所提到的,扩展ListActivity并调用ListActivity的setListAdapter,而不是自定义和空的。