表格行长按

发布于 2024-12-17 11:43:57 字数 419 浏览 1 评论 0原文

我有一个表,它是根据存在的数据量(如果有的话)动态构建的。

我希望能够长按表格行上的任意位置,以便能够获得一些用于删除或编辑等的选项。这可能吗?请记住,我需要在动态构建时不设置任何 XML 来完成所有这些操作。

这与我想要实现的目标相关吗?

@override

public boolean onKeyLongPress(int keyCode, KeyEvent event) {

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

任何建议表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初熏 2024-12-24 11:43:57

将上下文菜单注册到动态创建的 Table Row 。

调用registerForContextMenu(tableRow)并重写onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo)。
了解上下文菜单

Register context menu to dynamically created Table Row .

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

零度° 2024-12-24 11:43:57

这是我从 Mark Murphy 的书中改编的一些示例代码。我的列表基于数据库。

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

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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文