-1

我无法正确实现后退按钮。我见过的所有代码片段都不适合我。当我按下后退按钮时,我想要做的只是回到上一个列表。几乎我在一个列表中有一个列表,我只是希望它回到上一个列表。我该怎么做呢?这是我的清单,每个项目都有一个单独的清单。假设您单击武器,然后获得不同武器类型的列表等等

final String[] weapons = getResources().getStringArray(R.array.weapons);
                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, weapons));
                lv.setOnItemClickListener(new OnItemClickListener() 
                {
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) 
                    {
                        System.out.println("item clicked: "+weapons[position]);
                        switch(position)
                        {
                            case 0:
                                final String[] axes = getResources().getStringArray(R.array.axes);
                                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, axes));
                                break;
                            case 1:
                                final String[] clubs = getResources().getStringArray(R.array.clubs);
                                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, clubs));
                                break;
                            case 2:
                                final String[] daggers = getResources().getStringArray(R.array.daggers);
                                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, daggers));
                                break;
                            case 3:
                                final String[] great_axes = getResources().getStringArray(R.array.great_axes);
                                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, great_axes));
                                break;
                            case 4:
                                final String[] great_katana = getResources().getStringArray(R.array.great_katana);
                                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, great_katana));
                                break;
                            case 5:
                                final String[] great_swords = getResources().getStringArray(R.array.great_swords);
                                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, great_swords));
                                break;
                            case 6:
                                final String[] hand_to_hand = getResources().getStringArray(R.array.hand_to_hand);
                                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, hand_to_hand));
                                break;
                            case 7:
                                final String[] katana = getResources().getStringArray(R.array.katana);
                                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, katana));
                                break;
                            case 8:
                                final String[] polearms = getResources().getStringArray(R.array.polearms);
                                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, polearms));
                                break;
                            case 9:
                                final String[] scythes = getResources().getStringArray(R.array.scythes);
                                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, scythes));
                                break;
                            case 10:
                                final String[] staves = getResources().getStringArray(R.array.staves);
                                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, staves));
                                break;
                            case 11:
                                final String[] swords = getResources().getStringArray(R.array.swords);
                                setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, swords));
                                break;
                        }
                    }
                });
4

2 回答 2

0

您想覆盖后退按钮以加载传递给您的 ListView 的上一个适配器...
我建议创建一个类变量来存储以前的适配器选择:

List<Integer> previousAdapters = new ArrayList<Integer>();

重写onItemClick()以保存索引并调用新方法:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    previousAdapters.add(position);
    changeAdapter(position);
}

将您的 switch 语句移动到changeAdapter()

public void changeAdapter(int position) {
    switch(position)
    {
    case 0:
        final String[] axes = getResources().getStringArray(R.array.axes);
        setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, axes));
        break;
    case 1:
        final String[] clubs = getResources().getStringArray(R.array.clubs);
        setListAdapter(new ArrayAdapter<String>(ffxidirectory.this, R.layout.list_item, clubs));
        break;
    // etc
    }
}

onBackPressed()像这样覆盖:

public void onBackPressed() {
    if(previousAdapters.size() > 1)
        changeAdapter(previousAdapters.remove(previousAdapters.size() - 2));
    else
        super.onBackPressed();
}

此外,您不需要每次changeAdapter()调用时都重新创建资源数组。您应该在onCreate().

于 2012-11-10T22:58:02.650 回答
0

试试这个代码

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

它也可以通过覆盖 onBackPressed 方法在 android 2.0+ 中完成

@Override
public void onBackPressed(){
//do whatever you want the back button to do
}
于 2012-11-10T22:58:19.567 回答