单击ImageView从列表中删除项目

时间:2013-11-23 16:20:37

标签: android

我正在尝试通过单击图像视图从创建的列表中删除项目。我只想设置特定的图像视图来执行此任务。 (目前根本没有回应)

同时,如果用户在列表的同一项目中单击不同的图像视图,则会打开新活动。 这是代码:

private void registerClickCallBack() {
    // TODO Auto-generated method stub

    ListView list = (ListView) findViewById(R.id.lvMyDiaryFood);
    /*nextImage.setOnClickListener(new AdapterView.OnClickListener() {

        @Override
        public void onClick(View v ) {
            // TODO Auto-generated method stub

        }

    });*/
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked,
                int position, long id) {
            // TODO Auto-generated method stub
            MyDiaryConsumedFoodListItem clickedRecName = myConsumedFood
                    .get(position);
            String message = "" + clickedRecName.getRecipe_name_diary();
            // String message = "You clicked position " + position
            // + " of which recipe name is " + clickedFav.getRecipe_name();
            Toast.makeText(MyDiaryConsumedFood.this, message,
                    Toast.LENGTH_SHORT).show();
            // Can use also: Toast.makeText(getApplicationContext(),
            // message, Toast.LENGTH_LONG).show();

            String rec_name = myConsumedFood.get(position)
                    .getRecipe_name_diary();
            String rec_id = myConsumedFood.get(position).getRecipeId();

                Intent i = new
            Intent(MyDiaryConsumedFood.this,RecipesDetails.class);
            i.putExtra("rec_name", recipe_name);
            System.out.println(rattype);
            i.putExtra("rec_id", recipe_id);

            startActivity(i);


            deleteView = (ImageView) findViewById(R.id.ivDeleteMyDiary);
            deleteView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext());
                    builder.setMessage("Do you want to remove?");
                    builder.setCancelable(false);
                    builder.setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    //List<String> addedRecipe = _AddedRecipeToADay.get(_DayOfTheWeekHeader.get(groupPosition));


                                    //addedRecipe.remove(childPosition);

                                    //plannerCalendar.deleteRecipeFromADay(recipeID, _DayOfTheWeekHeader.get(groupPosition));
                                    //_DayOfTheWeekHeader.get(groupPosition) = date("Mon 04/11/2013")
                                    //_AddedRecipeToADay.get(_DayOfTheWeekHeader.get(groupPosition)).get(childPosition) = Recipe Name in the selected position


                                    //notifyDataSetChanged();
                                }
                            });
                    builder.setNegativeButton("No",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {

                                    dialog.cancel();
                                    dialog.dismiss();
                                }
                            });
                    AlertDialog alertDialog = builder.create();
                    try {
                    alertDialog.show();
                    } catch(Exception e){
                           // WindowManager$BadTokenException will be caught and the app would not display 
                           // the 'Force Close' message
                         }
                }

            });

        }

    });

}

不确定为什么删除没有响应.......

1 个答案:

答案 0 :(得分:0)

你的问题出在startActivity(i) 在调用startActivity时调用其他活动会中断所有后续代码的执行;

我的建议是;

private void registerClickCallBack() {
// TODO Auto-generated method stub

ListView list = (ListView) findViewById(R.id.lvMyDiaryFood);
/*nextImage.setOnClickListener(new AdapterView.OnClickListener() {

    @Override
    public void onClick(View v ) {
        // TODO Auto-generated method stub

    }

});*/
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View viewClicked,
            int position, long id) {
        // TODO Auto-generated method stub
        MyDiaryConsumedFoodListItem clickedRecName = myConsumedFood
                .get(position);
        String message = "" + clickedRecName.getRecipe_name_diary();
        // String message = "You clicked position " + position
        // + " of which recipe name is " + clickedFav.getRecipe_name();
        Toast.makeText(MyDiaryConsumedFood.this, message,
                Toast.LENGTH_SHORT).show();
        // Can use also: Toast.makeText(getApplicationContext(),
        // message, Toast.LENGTH_LONG).show();

        String rec_name = myConsumedFood.get(position)
                .getRecipe_name_diary();
        String rec_id = myConsumedFood.get(position).getRecipeId();          


        deleteView = (ImageView) findViewById(R.id.ivDeleteMyDiary);
        deleteView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder builder = new AlertDialog.Builder(getBaseContext());
                builder.setMessage("Do you want to remove?");
                builder.setCancelable(false);
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                //List<String> addedRecipe = _AddedRecipeToADay.get(_DayOfTheWeekHeader.get(groupPosition));


                                //addedRecipe.remove(childPosition);

                                //plannerCalendar.deleteRecipeFromADay(recipeID, _DayOfTheWeekHeader.get(groupPosition));
                                //_DayOfTheWeekHeader.get(groupPosition) = date("Mon 04/11/2013")
                                //_AddedRecipeToADay.get(_DayOfTheWeekHeader.get(groupPosition)).get(childPosition) = Recipe Name in the selected position


                                //notifyDataSetChanged();
                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                dialog.cancel();
                                dialog.dismiss();
                            }
                        });
                AlertDialog alertDialog = builder.create();
                try {
                alertDialog.show();
                } catch(Exception e){
                       // WindowManager$BadTokenException will be caught and the app would not display 
                       // the 'Force Close' message
                     }
            }

        });

        Intent i = new
        Intent(MyDiaryConsumedFood.this,RecipesDetails.class);
        i.putExtra("rec_name", recipe_name);
        System.out.println(rattype);
        i.putExtra("rec_id", recipe_id);

        startActivity(i);
    }

});

}