如何在列表项长按时弹出确认删除对话框?

2024-01-31

我正在学习在线教程,并尝试自己实现一些功能。 当检测到长按列表项时,如何弹出对话框来提醒用户?以下是该教程中的一些代码:

public class FriendList extends ListActivity 
{


private static final int ADD_NEW_FRIEND_ID = Menu.FIRST;

private static final int EXIT_APP_ID = Menu.FIRST + 1;
private IAppManager imService = null;
private FriendListAdapter friendAdapter;

public String ownusername = new String();

private class FriendListAdapter extends BaseAdapter 
{       
    class ViewHolder {
        TextView text;
        ImageView icon;
    }
    private LayoutInflater mInflater;
    private Bitmap mOnlineIcon;
    private Bitmap mOfflineIcon;        

    private FriendInfo[] friends = null;


    public FriendListAdapter(Context context) { // Constructor of Class "FriendListAdapter"
        super();            

        mInflater = LayoutInflater.from(context);

        mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
        mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);

    }

    public void setFriendList(FriendInfo[] friends)
    {
        this.friends = friends;
    }


    public int getCount() {  // get how many row are in the listview

        return friends.length;
    }


    public FriendInfo getItem(int position) { // get item from the row

        return friends[position];
    }

    public long getItemId(int position) {

        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) { // For modify the content of row
        // A ViewHolder keeps references to children views to avoid unneccessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is no need
        // to reinflate it. We only inflate a new View when the convertView supplied
        // by ListView is null.
        if (convertView == null) 
        {
            convertView = mInflater.inflate(R.layout.friend_list_screen, null);

            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.text);
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);                                       

            convertView.setTag(holder);
        }   
        else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        holder.text.setText(friends[position].userName);
        holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);

        return convertView;
    }

}

使用代码为:

this.getListView().setLongClickable(true);
 this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
    //Do your tasks here


            AlertDialog.Builder alert = new AlertDialog.Builder(
                    Activity.this);
            alert.setTitle("Alert!!");
            alert.setMessage("Are you sure to delete record");
            alert.setPositiveButton("YES", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                                               //do your work here                      
                    dialog.dismiss();

                }
            });
            alert.setNegativeButton("NO", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                }
            });

            alert.show();

    return true;
  }
  });

您可以根据需要自定义警报对话框...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在列表项长按时弹出确认删除对话框? 的相关文章

  • 启动画面反应本机后出现白屏

    编辑 似乎是因为 MainActivity 加载太重而生成白屏 我设法首先使用本机启动屏幕来解决 然后在本机被杀死后立即基于下一个插件的 js 实现 我做了一些修改完美匹配两个启动画面https github com crazycodebo
  • 关于android Sqlite在多进程情况下的安全性

    在我的应用程序中 存在多个进程 并且在每个进程中 我需要访问同一个SQLite数据库 当然 这意味着超过2个线程 所以我不仅担心SQLite的线程安全性 还担心SQLite的线程安全性 还有过程安全 这种情况的一种解决方案是使用内容提供者
  • 如何设置itemTouchHelper选择性滑动?

    所以我在 recyclerview rv 上实现了这个 itemtouchhelper simple 回调 现在 在这个 rv 中 我根据内容类型将 2 种布局设置为一行 因此 当我在 rv 上设置此 touchhelper 时 它正在这两
  • Android,无法从谷歌API获取天气

    以下代码之前运行良好 class RetreiveWeatherTask extends AsyncTask
  • 找不到 RecyclerView 类 android.support.v7.recyclerview.R$styleable

    我在我的应用程序上使用 RecyclerView 但在运行时应用程序崩溃并且我的 logcat 读取 java lang RuntimeException Unable to start activity ComponentInfo com
  • 使用 ArrayAdapter 的 ListView 中的按钮

    我有一个使用 POJO 类填充的 ArrayAdapter 列表视图由 2 个布局组成 1 代表菜单项 1 代表类别 带分隔符的列表视图很好 后来我尝试在每个菜单项行中添加一个按钮来编辑其中的详细信息 当我尝试获取单击按钮的行的位置时 我遇
  • 要在进程中运行 dex,Gradle 守护进程需要更大的堆。目前有 910 MB

    实际上主要错误是 java exe完成非零退出值 1 首先我告诉你安装 studio 后遇到的每个问题 三天前 我刚刚安装了 android studio 并创建了新项目 1 首先它抛出错误 插件太旧 请更新到更新的版本 在谷歌上搜索后我改
  • 使用 appcelerator titan 在 android 中后退按钮退出应用程序

    我是钛开发的新手 在本机 android 中 如果我们按下后退按钮 则仅当前活动将被关闭 并且它将返回到上一个活动 但是当我使用 Titanium 在 Android 中按下后退按钮时 它会从应用程序退出 我怎样才能改变这种行为 有两种类型
  • Android Activity 和 Service 关系 - 暂停后、停止后

    假设创建了 Activity A 然后 A 启动了一个 Service S 并将其自身绑定到 S S 通知 A 更新 这将导致 A 的状态发生变化 Android 暂停或停止 A 后 A 和 S 会发生什么 例如 暂停 A 是否会自动解除它
  • Android 可检查子菜单选项

    所以我有一个用于选项菜单项的子菜单 我想要一个可检查条目的列表 用户可以根据需要选择 取消选择多个条目 我无法解决的唯一问题是如何防止单击其中一个复选框时关闭选项菜单 我看到 PerformShortcut 有一个 FLAG PERFORM
  • 如何在android中的应用程序小部件中找到哪个按钮被点击?

    我想设计一个简单的应用程序小部件 它有两个文本视图和两个用于上一个 下一个的按钮 我很难处理应用程序小部件中的按钮单击 实际上我的愿望是 如果用户单击上一个按钮 我想显示以前的值 如果用户单击下一个按钮 我想显示数据库中的下一个值 如何知道
  • 在InputMethodService 外部调用InputMethodManager.setInputMethod(IBinder token, String id)。哪里可以找到代币?

    我想通过单击按钮在我的 EditText 上显示 Google 语音输入 IME 所以 根据this http android developers blogspot ru 2011 12 add voice typing to your
  • 为什么设置 MediaRecorder 时显示错误 IllegalStateException?

    我的代码设置 MediaRecorder 它显示行集质量错误 mMediaRecorder new MediaRecorder Step 1 Unlock and set camera to MediaRecorder mCamera st
  • Android Youtube API 可用吗? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 有适用于 Android 的 YouTube API 吗 如果不是 除了通过网络浏览器之外 如何从 Yo
  • android sqlite 如果不存在则创建表

    创建新表时遇到一点问题 当我使用 CREATE TABLE 命令时 我的新表按应有的方式形成 但是当我退出活动时 应用程序崩溃 并且我在 logcat 中得到一个表已存在 如果我使用 CREATE TABLE IF NOT EXISTS 则
  • 如何将 currentTimeMillis 转换为可读的日期格式? [复制]

    这个问题在这里已经有答案了 我想用currentTimeMillis两次 这样我就可以计算持续时间 但我也想以用户可读的格式显示时间和日期 我遇到了麻烦currentTimeMillis有利于计算 但我看不到内置函数可以转换为合适的时间或时
  • Webview 中的 Java 空指针异常

    我试图搜索这个问题 但这个错误看起来与这个错误 https stackoverflow com questions 21866459 android nullpointerexception on webview 我的 google pla
  • 如何为背景图像添加内边距

    我有一个LinearLayout其中有一个背景图像 一个 9 修补的 png 文件 如何向左和右添加填充 以使背景图像不占据整个宽度 我努力了android paddingLeft and android paddingRight 但这并没
  • 更改 Android 中突出显示文本的颜色

    我不确定这是否可能 也许有人可以纠正我 我在 Android 应用程序中有一个 EditText 视图 该视图在蓝色背景上有白色文本 当选择文本时 通过长按和编辑对话框 我希望突出显示为白色并将文本颜色更改为黑色 令人烦恼的是 似乎没有办法
  • Android 使用非公历

    我正在创建一个DatePickerDialogFragment用户将在其中选择出生日期 我想确保我可以处理非公历日期 我无法更改在我的设备上使用的日历类型 Android 是否允许用户切换日历类型 如果是的话 步骤是什么 到目前为止我还没有

随机推荐