Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 创建多级listview应用程序_Android_Listview_Hierarchy - Fatal编程技术网

Android 创建多级listview应用程序

Android 创建多级listview应用程序,android,listview,hierarchy,Android,Listview,Hierarchy,这是代码。这是不言自明的。我唯一的问题是保留int-level的值。它每次都会丢失。我需要的价值被保留,它将工作。这是一个快照。它应该可以扩展到尽可能多的层次,每个层次可以扩展到尽可能多的项目 XML代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width=

这是代码。这是不言自明的。我唯一的问题是保留int-level的值。它每次都会丢失。我需要的价值被保留,它将工作。这是一个快照。它应该可以扩展到尽可能多的层次,每个层次可以扩展到尽可能多的项目

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;
}

}
package com.example.kjkjsdkjdsjdfs;
导入android.app.ListActivity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
公共类MainActivity扩展了ListActivity{
字符串categoryselected=“main”;
公共int级别=0;
@抑制警告(“空”)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
字符串[]数组;
接收到的意图=getIntent();
//设置为主列表视图
if(received==null | |!received.hasExtra(“数组”)){
数组=新字符串[]{“1”、“2”、“3”};
}
//设置为子列表视图
否则{
array=received.getStringArrayExtra(“数组”);
}
if(received!=null | |!received.hasExtra(“级别”)){
级别=已接收。getIntExtra(“级别”,0);
}
setListAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,数组));
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
级别=级别+1;
意图启动=新意图(MainActivity.this、MainActivity.class);
开关(电平){
案例1:
开关(位置){
案例0:
starting.putExtra(“数组”,新字符串[]{“1a”、“1b”、“1c”});
启动。putExtra(“级别”,级别);
打破
案例1:
starting.putExtra(“数组”,新字符串[]{“2a”、“2b”、“2c”});
启动。putExtra(“级别”,级别);
打破
案例2:
starting.putExtra(“数组”,新字符串[]{“3a”、“3b”、“3c”});
启动。putExtra(“级别”,级别);
打破
}
打破
案例2:
开关(位置){
案例0:
starting.putExtra(“数组”,新字符串[]{“1aa”,“1ab”});
启动。putExtra(“级别”,级别);
打破
}
打破
案例3:
开关(位置){
案例0:
starting.putExtra(“数组”,新字符串[]{“1aaa”,“1aab”});
启动。putExtra(“级别”,级别);
打破
}
打破
}
起动性;
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.activity\u主菜单);
返回true;
}
}

如下更新您的代码:

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 ListItems extends ListActivity {
    String categoryselected = "main";
    public int level = 0;

    @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(ListItems.this, ListItems.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;
    }

}
导入android.app.ListActivity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
公共类ListItems扩展了ListActivity{
字符串categoryselected=“main”;
公共int级别=0;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
字符串[]数组;
接收到的意图=getIntent();
//设置为主列表视图
if(received==null | |!received.hasExtra(“数组”)){
数组=新字符串[]{“1”、“2”、“3”};
}
//设置为子列表视图
否则{
array=received.getStringArrayExtra(“数组”);
}
if(received!=null | |!received.hasExtra(“级别”)){
级别=已接收。getIntExtra(“级别”,0);
}
setListAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,数组));
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
级别=级别+1;
意图开始=新意图(ListItems.this,ListItems.class);
开关(电平){
案例1:
开关(位置){
案例0:
starting.putExtra(“数组”,新字符串[]{“1a”、“1b”、“1c”});
启动。putExtra(“级别”,级别);
打破
案例1:
starting.putExtra(“数组”,新字符串[]{“2a”、“2b”、“2c”});
启动。putExtra(“级别”,级别);
打破
案例2:
starting.putExtra(“数组”,新字符串[]{“3a”、“3b”、“3c”});
启动。putExtra(“级别”,级别);
打破
}
打破
案例2:
开关(位置){
案例0:
starting.putExtra(“数组”,新字符串[]{“1aa”,“1ab”});
启动。putExtra(“级别”,级别);
打破
}
打破
案例3:
开关(位置){
案例0:
starting.putExtra(“数组”,新字符串[]{“1aaa”,“1aab”});
启动。putExtra(“级别”,级别);
打破
}
打破
}
起动性;
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.activity\u主菜单);
返回true;
}
}

您是说要保留此变量的值吗
public int level=0
?是的,因此当我重复单击事件时,它似乎总是一,而它应该递增一。似乎永远不会达到第二个级别(案例2)。如果我使用listview中的第一个位置(也称为案例0)测试它,它会直接从1到1aaa,而不是1a。我只会复制所有内容,但这些注释部分的字符数似乎有限。这不适合我。它直达t