質問

I have a table which is built dynamically based on how much data is present, if at all.

I want to be able to long press anywhere on a the table row to be able to get some options to delete or edit etc. Is this possible? Remember I need to do all this without setting any XML as its dynamically built.

Is this relevant to what I want to achieve?

@override

public boolean onKeyLongPress(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) 
    {
        // do your stuff here
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}
`

any advice is appreciated.

役に立ちましたか?

解決

Register context menu to dynamically created Table Row .

call registerForContextMenu(tableRow) and override onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo). Read about Context Menu

他のヒント

Here is some example code that I adapted from Mark Murphy's books. My list is based on a database.

private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent,View view, int position,long id) {
        String selection= "_id=" + String.valueOf(id);
        Cursor c = db.query("commentlist", columns, selection, null, null, null, null);
        c.moveToFirst();
        checkDelete(c.getLong(0),c.getString(1));
    }
};
private void checkDelete(final long id, final String record) {
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder
        .setTitle("Delete task")
        .setMessage("Do you want to delete this task " + record + " ?")
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dlg, int sumthin) {
            }
        })
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dlg, int sumthin) {
            db.execSQL("DELETE FROM commentlist WHERE _id=" + id);
            model.requery();
            }
        })
        .show();
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top