所以这里是代码。它非常不言自明。我遇到的唯一问题是保留 int 级别的值。每次都输了。我需要保留价值,它会起作用。这是一张快照。它应该可以扩展到尽可能多的深度和每个级别的项目。
XML 代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
Java代码:
package com.example.kjkjsdkjdsjkdfs;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity {
String categoryselected = "main";
public int level = 0;
@SuppressWarnings("null")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] array;
Intent received = getIntent();
// Setup as main ListView
if (received == null || !received.hasExtra("array")) {
array = new String[] { "1", "2", "3" };
}
// Setup as sub ListView
else {
array = received.getStringArrayExtra("array");
}
if(received != null || !received.hasExtra("level")) {
level = received.getIntExtra("level", 0);
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
level = level + 1;
Intent starting = new Intent(MainActivity.this, MainActivity.class);
switch (level) {
case 1:
switch (position) {
case 0:
starting.putExtra("array", new String[] { "1a", "1b", "1c" });
starting.putExtra("level", level);
break;
case 1:
starting.putExtra("array", new String[] { "2a", "2b", "2c" });
starting.putExtra("level", level);
break;
case 2:
starting.putExtra("array", new String[] { "3a", "3b", "3c" });
starting.putExtra("level", level);
break;
}
break;
case 2:
switch (position) {
case 0:
starting.putExtra("array", new String[] { "1aa", "1ab" });
starting.putExtra("level", level);
break;
}
break;
case 3:
switch (position) {
case 0:
starting.putExtra("array", new String[] { "1aaa", "1aab" });
starting.putExtra("level", level);
break;
}
break;
}
startActivity(starting);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}