具有可选项的ListActivity

时间:2012-02-16 14:43:45

标签: android

为了提高盲人用户的可用性,我需要一个带有OnItemSelected方法的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          
    }
};

现在,当我创建新的Class SelectableListActivity的实例并覆盖onListItemSelected方法时,仍然没有任何反应。有任何想法吗?提前谢谢了!

1 个答案:

答案 0 :(得分: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,因此可以用于例如盲人与人互动。