Android为listView.setOnItemClickListener添加是/否框

时间:2014-04-23 20:40:05

标签: android listview onitemclicklistener dialog

我一直在尝试很多东西来尝试让它工作,但基本上我有一个显示SQLiteDb的列表视图。数据库的每一行都是可点击的,因为我希望能够将其复制到数据库中的另一个表中。这个功能工作正常。我的问题是我想要一个Yes / No框出现,以便在复制之前确认它。我已经经历了很多教程并一直在这里寻找解决方案,但没有一个我可以适应。这是代码我坐在我的displayListView()方法中。提前谢谢!

    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> listView, View view,
                int position, long id) {

            // Get the cursor, positioned to the corresponding row in the
            // result set
            Cursor cursor = (Cursor) listView.getItemAtPosition(position);

            dbHelper.addToPlanner(
                    cursor.getString(cursor.getColumnIndexOrThrow("band")),
                    cursor.getString(cursor.getColumnIndexOrThrow("day")),
                    cursor.getString(cursor.getColumnIndexOrThrow("stage")),
                    cursor.getDouble(cursor.getColumnIndexOrThrow("stime")),
                    cursor.getDouble(cursor.getColumnIndexOrThrow("ftime")),
                    cursor.getString(cursor.getColumnIndexOrThrow("planner")));

            String displayMe =
                    cursor.getString(cursor.getColumnIndexOrThrow("band"));

            Toast.makeText(getApplicationContext(),
                    displayMe, Toast.LENGTH_SHORT).show();

        }
    });

休息几分钟后,我想出来了!我不得不将我的对话框声明和方法分开,并在这里和那里声明事物。这就是我如何运作,希望它可以帮助其他人。

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);

    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> listView, View view, 
                int position, long id) {        


            // Get the cursor, positioned to the corresponding row in the result set
            Cursor cursor = (Cursor) listView.getItemAtPosition(position);
            final String band = cursor.getString(cursor.getColumnIndexOrThrow("band"));
            final String day = cursor.getString(cursor.getColumnIndexOrThrow("day"));
            final String stage = cursor.getString(cursor.getColumnIndexOrThrow("stage"));
            final double stime = cursor.getDouble(cursor.getColumnIndexOrThrow("stime"));
            final double ftime = cursor.getDouble(cursor.getColumnIndexOrThrow("ftime"));
            final String planner = cursor.getString(cursor.getColumnIndexOrThrow("planner"));

            builder.setTitle("Add To Planner")
            .setMessage("Add To Planner")
            .setIcon(android.R.drawable.ic_dialog_alert);
            builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                    dbHelper.addToPlanner(band, day, stage, stime, ftime, planner);
                }
            }).setNegativeButton("No", null).show();




            String countryCode = 
                    cursor.getString(cursor.getColumnIndexOrThrow("band"));


            Toast.makeText(getApplicationContext(),
                    countryCode, Toast.LENGTH_SHORT).show();

        }
    });

2 个答案:

答案 0 :(得分:0)

我建议您创建一个警告对话框。这是一个很好的帮助链接:http://www.mkyong.com/android/android-alert-dialog-example/

答案 1 :(得分:0)

将此方法放入您的班级并调用列表项

public void alertDilog(final int position)     {

    // TODO Auto-generated method stub

    // Creating alert Dialog with two Buttons

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(FavoritesProperty.this);

    // Setting Dialog Title
    alertDialog.setTitle("Confirm Delete...");

    // Setting Dialog Message
    alertDialog.setMessage("Are you sure you want delete this?");

    // Setting Icon to Dialog
    alertDialog.setIcon(R.drawable.delete);

    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("YES",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {

            // Write your code here to execute after dialog

        }
    });
    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // Write your code here to execute after dialog

            dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();



}