2012-11-10 612 views
-1

我無法正確執行後退按鈕。我所見過的所有代碼片段都不適合我。當我按下後退按鈕時,我正在嘗試執行的操作是回到上一個列表。幾乎我在列表中有一個列表,我只想讓它回到之前的列表。我將如何去做這件事?這是我的列表,每個項目都有一個單獨的列表。比方說,你點擊武器,你再拿到不同的武器類型列表等後退按鈕android

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

回答

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

移動你的開關語句來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()中初始化它們。

0

試試這個代碼

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

它CON也可以在Android中通過重寫onBackPressed方法

@Override 
public void onBackPressed(){ 
//do whatever you want the back button to do 
} 
做2.0+