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

标签 android listview android-alertdialog delete-row

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

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

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

关于android - 如何在用户长按列表项时弹出对话框确认删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23195208/

相关文章:

android - 蓝牙权限请求对话框 - 在屏幕旋转时重新创建(方向更改)

Android 布局 - 如何实现固定/卡住的标题和列

android 如何在用户单击通知时启动 Activity ?

android - 启动然后销毁前台服务不会停止 VS 调试器

android - 我/flutter (17109): Another exception was thrown: A RenderFlex overflowed by 80 pixels on the right

php - 如何在android和php中将选定的多个复选框值插入到mysql数据库的不同行中

.net - 绑定(bind)到代码隐藏中的方法

android - 是否可以在警报窗口中提供超链接?

java - 两个按钮具有相同的 "onClick"- Android

Android:AlertDialog 未在 Cancel() 或 Dismiss() 上关闭