Android 当用户长按列表项时,如何弹出确认删除的对话框?

Android 当用户长按列表项时,如何弹出确认删除的对话框?,android,listview,android-alertdialog,delete-row,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 imSer

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

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;
  }
  });
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(新的OnItemLongClickListener()){
公共布尔值长单击(AdapterView父对象、视图v、整型位置、长id){
//在这里完成你的任务
AlertDialog.Builder alert=新建AlertDialog.Builder(
活动(本节);
alert.setTitle(“alert!!”);
alert.setMessage(“您确定要删除记录吗”);
alert.setPositiveButton(“是”,新的OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//你在这里工作吗
dialog.dismise();
}
});
alert.setNegativeButton(“否”,新的OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
});
alert.show();
返回true;
}
});
您可以根据需要自定义警报对话框…

希望有帮助:

 selectRoom.setOnItemLongClickListener(new OnItemLongClickListener(){
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
            int position, long arg3) {
        // TODO Auto-generated method stub
         Dialog();
 }
}

您应该使用ListView在活动中执行以下操作

ListView myListView;
myListView = (ListView) findViewById(R.id.my_list_view);

myListView.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        //show your dialog
        //like this
        CustomDialog dialog = new CustomDialog();
        dialog.show(getSupportFragmentManager(), TAG)
        return false;
    }
});

你应该使用上下文菜单…为上下文菜单注册你的列表项我想你正在尝试做一件比你能做的复杂得多的事情。你应该从android开发者网站的培训部分开始。谢谢!这正是我需要的:)谢谢你的建议,我已经解决了我的问题:)
ListView myListView;
myListView = (ListView) findViewById(R.id.my_list_view);

myListView.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        //show your dialog
        //like this
        CustomDialog dialog = new CustomDialog();
        dialog.show(getSupportFragmentManager(), TAG)
        return false;
    }
});