我最近对 Android 编码感兴趣,并通过几个教程来学习。但是,现在我卡了一天,希望你们能帮助我。
基本上我想为每个主菜单条目创建一个主菜单和一个子菜单。(现在这只是第一个条目的情况)
对于主菜单,我使用了具有不同图标的 ListView(通过自定义适配器实现)。选择其中一个菜单项通常可以正常工作,但是如果我选择配置 onitemclick 以启动一个新活动,这是另一个使用 ArrayAdapter 的(简单)ListView,它会在切换到子菜单活动后中断。
奇怪的是,如果我在两个 ListViews 中都使用 ArrayAdapter 没问题;在两者中使用 CustomAdapter 时也是如此。
这是代码,在此先感谢您的帮助;)
主菜单:
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array contentmenu
String[] contentmenu = getResources().getStringArray(R.array.mainmenu_cats);
// storing icons in Array
TypedArray iconarray = getResources().obtainTypedArray(R.array.mainmenu_icons);
// Binding resources Array to ListAdapter
this.setListAdapter(new ImageAndTextAdapter(this, R.layout.list_item, contentmenu, iconarray));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
switch( position )
{
case 0: Intent newActivity0 = new Intent(getApplicationContext(),SubActivity.class);
startActivity(newActivity0);
break;
case 1: Intent newActivity1 = new Intent(getApplicationContext(),SubActivity2.class);
startActivity(newActivity1);
break;
}
}
});
}
子菜单:
public class SubActivity extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] submen_1 = getResources().getStringArray(R.array.submenu_1);
// Binding resources Array to ListAdapter
setListAdapter(new ArrayAdapter<String>(ctx, R.layout.list_item1, R.id.submen1, submen_1));
ListView sublv1 = getListView();
// listening to single list item on click
sublv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Launching new Activity on selecting single List Item
Intent newActivity = new Intent(getApplicationContext(),Page1.class);
// sending data to new activity
newActivity.putExtra("position",position);
startActivity(newActivity);
}
});
}
xml 主菜单:list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/option_icon"
android:layout_width="48dp"
android:layout_height="fill_parent"/>
<TextView
android:id="@+id/option_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
子菜单:list_item1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/icon1"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:src="@drawable/ic_action_google_play" />
<TextView
android:id="@+id/submen1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp" >
</TextView>