2012-02-16 48 views
0

爲了提高盲人用戶的可用性,我需要帶有OnItemSelected方法的ListActivity,以便在選擇特定的listitem時添加振動或聲音等內容。 因此,我已經擴展了ListActivity類,並增加了以下方法:具有可選項目的ListActivity

protected void onListItemSelected(ListView parent, View v, int position, long id) { 
} 

private AdapterView.OnItemSelectedListener mOnSelectedListener = new AdapterView.OnItemSelectedListener() { 

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { 
     onListItemSelected((ListView)parent, v, position, id);   
    } 

    public void onNothingSelected(AdapterView<?> parent) { 
     // TODO Auto-generated method stub   
    } 
}; 

現在,當我創建我的新類SelectableListActivity的實例並重寫onListItemSelected方法,仍然沒有任何反應。有任何想法嗎?提前謝謝了!

+0

新Android開發人員常見的問題是依賴於ListView.getSelectedItemPosition()。在觸摸模式下,此方法將返回INVALID_POSITION。您應該改爲使用點擊偵聽器(請參閱setOnItemClickListener(android.widget.AdapterView.OnItemClickListener))或選擇模式(請參閱setChoiceMode(int))。 http://developer.android.com/resources/articles/touch-mode.html – jsaye 2012-02-16 15:27:04

+0

在這種特殊情況下,我需要一個監聽器來進行選擇,因爲視圖是通過物理鍵來操作的,我需要給出某種形式的通過點擊選擇項目之前的反饋。 – 2012-02-16 15:30:14

+0

您的列表項目的內容是什麼?某些控件(如複選框)將攔截onClick。 – jsmith 2012-02-16 15:34:49

回答

0

問題已解決。我不得不重寫onContentChanged()並將我的偵聽器分配給ListView。

public class MyListActivity extends ListActivity { 

private Vibrator vibrator; 
private ListView mList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); 
    setContentView(R.layout.station_list); 
    // do some other stuff 
} 

@Override 
public void onContentChanged() { 
    super.onContentChanged(); 
    mList = (ListView)findViewById(android.R.id.list); 
    mList.setOnItemSelectedListener(mOnSelectedListener); 
} 

private AdapterView.OnItemSelectedListener mOnSelectedListener = new AdapterView.OnItemSelectedListener() { 
    public void onItemSelected(AdapterView parent, View v, int position, long id) { 
     onListItemSelected((ListView)parent, v, position, id);   
    } 

    public void onNothingSelected(AdapterView parent) { 
     // nothing to do here...    
    } 
}; 

    protected void onListItemSelected(ListView parent, View v, int position, long id) { 
    vibrator.vibrate(100); 
} 

    @Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    vibrator.vibrate(100); 
    Cursor c = cursor; 
    c.moveToPosition(position); 
    Intent i = new Intent(this, SomeOtherActivity.class); 
    i.putExtra(StationsDbAdapter.KEY_ROWID, id); 
    startActivity(i); 
} 

@Override 
public boolean dispatchKeyEvent(KeyEvent event) { 
    int action = event.getAction(); 
    int keyCode = event.getKeyCode(); 
    switch (keyCode) { 
    case KeyEvent.KEYCODE_VOLUME_DOWN: 
     if (action == KeyEvent.ACTION_DOWN) { 
      dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN)); 
      dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN)); 
     } 
     return true; 
    case KeyEvent.KEYCODE_VOLUME_UP: 
     if (action == KeyEvent.ACTION_DOWN) { 
      dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP)); 
      dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_UP)); 
     } 
     return true; 
    case KeyEvent.KEYCODE_MENU: 
     if (action == KeyEvent.ACTION_DOWN) { 
      dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER)); 
      dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER)); 
     } 
     return true; 
    default: 
     return super.dispatchKeyEvent(event); 
    } 
} 

}

這樣我能夠創建一個ListView即沒有觸摸屏可用並且因此可能例如盲人與之互動。