sebbl.sche
sebbl.sche

Reputation: 321

onClick Listener for CursorAdapter

I created a customized CursorAdapter and want to select a list item, in order to start an action in onOptionsItemSelected.

Creating List View:

public void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate called");
    super.onCreate(savedInstanceState);

    Log.d(TAG, "create DatabaseOpenHelper");
    DatabaseOpenHandler helper = new DatabaseOpenHandler(this);

    Log.d(TAG, "get writeable database access");
    database = helper.getWritableDatabase();

    Log.d(TAG, "create Cursor for database access");
    Cursor data = database.query(DatabaseConstants.TABLE_NOTES, fields,
            null, null, null, null, null);

    Log.d(TAG, "set NoteCursorAdapeter");
    setListAdapter(new NoteCursorAdapter(this, data));  
}

onOptionItemSelected:

public boolean onOptionsItemSelected(MenuItem item) {
    Log.d(TAG, "onOptionItemSelected called");
    switch (item.getItemId()) {
    case R.id.conference_note_menu_new:
        Toast.makeText(this, "Es sind keine Einstellungen verfügbar",
                Toast.LENGTH_LONG).show();
        return true;

    case R.id.conference_note_menu_edit:
        Toast.makeText(this, "Es sind keine Einstellungen verfügbar",
                Toast.LENGTH_LONG).show();
        return true;

    case R.id.conference_note_menu_delete:
        Toast.makeText(this, "Es sind keine Einstellungen verfügbar",
                Toast.LENGTH_LONG).show();
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

Can't find any useful information on the internet.

Upvotes: 4

Views: 5330

Answers (4)

Shubhayu
Shubhayu

Reputation: 13552

Use OnLongClickListener() to populate a menu and then use your code to start your action on the selected item.

Upvotes: 0

matejs
matejs

Reputation: 3536

this works for me:

       list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

            }
        });

Upvotes: 2

Alex Klimashevsky
Alex Klimashevsky

Reputation: 2495

onOptionItemSelected is for menu. you need set onItemClickListener for your ListView like this:

getListView().setOnItemClickListener(this);

and implements:

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
}

Upvotes: 2

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

Use the below line for item click listerner:;

public void onListItemClick(ListView parent, View v, int position, long id) {
}

because

public boolean onOptionsItemSelected(MenuItem item) {
}

is for menu item selection

Upvotes: 2

Related Questions