删除ListView中的行[android应用程序]

时间:2012-04-28 19:22:44

标签: java android listview adapter

如何在alertDialog按钮中按YES按钮后删除listview中的行?!

在我的代码中Cancel是我想要的按钮,如果我按下它,它应该做两件事:

  1. 告诉我alertDialog:我通过调用函数ShowDialogCancel()
  2. 来做到这一点
  3. 按下后:YES中的YESDialog ==>该行应该删除!!
  4. 我尝试了很多方法,但在我按下alertDialog

    中的YES按钮之前删除了该行

    任何建议

    Monerah ......

    我的适配器类:

    public class MyCasesListAdapter extends BaseAdapter {
        private MyPage myPage;
        private List<MyCaseClass> listOfCases;
        private Activity parentActivity;
    
        // TODO test
        MyCaseClass entry;
    
        // TODO delete it not imp.
        public MyCasesListAdapter() {
    
            super();
    
        }
    
        public MyCasesListAdapter(MyPage mypage, List<MyCaseClass> listOfCaseParameter, Activity parentActivity) {
            this.myPage = mypage;
            this.listOfCases = listOfCaseParameter;
            this.parentActivity = parentActivity;
        }
    ...
        public View getView(int position, View convertView, ViewGroup viewGroup) {
    
             entry = listOfCases.get(position);
             //this.getitem(position)
            if (convertView == null) {
    
                LayoutInflater inflater = (LayoutInflater) myPage
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.mypage_row, null);
              }
    
    
            // this is row items..
            // Set the onClick Listener on this button
            Button ConfExpandRegion = (Button) convertView.findViewById(R.id.expand);
            Button Cancelb = (Button) convertView.findViewById(R.id.cancelCase);
            TextView tvCase = (TextView) convertView.findViewById(R.id.mypage_name);
    
            // To be a clickable button
            ConfExpandRegion.setFocusableInTouchMode(false);
            ConfExpandRegion.setFocusable(false);
    
    
            ConfExpandRegion.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    myPage.ShowingDialogExpand();
                }
            });
    
            // To be a clickable button
            Cancelb.setFocusableInTouchMode(false);
            Cancelb.setFocusable(false);
            Cancelb.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    entry = (MyCaseClass) v.getTag();
                    int caseid= entry.getID();
                    myPage.ShowingDialogCancel(caseid);
    
                    Toast.makeText(myPage, "inside calling", 0).show();
    
                    //MyCaseClass entry = (MyCaseClass) v.getTag();
                    //listOfCases.remove(entry);
    
                    // listPhonebook.remove(view.getId());
                    notifyDataSetChanged();
                }
            });
    
            // Set the entry, so that you can capture which item was clicked and
            // then remove it
            // As an alternative, you can use the id/position of the item to capture
            // the item
            // that was clicked.
            ConfExpandRegion.setTag(entry);
            Cancelb.setTag(entry);
    
            // btnRemove.setId(position);
    
            return convertView;
        }
    
        public void onClick(View view) {
            MyCaseClass entry = (MyCaseClass) view.getTag();
            listOfCases.remove(entry);
            // listPhonebook.remove(view.getId());
            notifyDataSetChanged();
    
        }
    
    
    }
    

    从我的页面类:

    public void  ShowingDialogCancel(){
            final AlertDialog alertDialog2 = new AlertDialog.Builder(this).create();
            alertDialog2.setTitle("Conformation?");
            alertDialog2.setMessage("Are you sure you want to cancel x cases?");
    
    
            alertDialog2.setButton("Yes", new DialogInterface.OnClickListener() {
                   public  void onClick(DialogInterface dialog, int which) {
    
                         CancelMsg = "Case_ID cancel";
    
    
                       if (!b) {
                            try {
                                // Should write server number here + the chatting must be pushed above 
                                sendSMS("0000", CancelMsg);
                                Toast.makeText(MyPage.this, "Cancelation request sent", Toast.LENGTH_LONG)
                                        .show();
    
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                Toast.makeText(MyPage.this, e.getMessage(),
                                        Toast.LENGTH_LONG).show();
                            }
    
                        }
                       }
                });
    
                alertDialog2.setButton2("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                    // here you can add functions
                    // Do nothing 
    
                       Toast.makeText(MyPage.this, "inside No", 0)
                        .show();
    
    
                   }
                });
    
                alertDialog2.setIcon(android.R.drawable.ic_dialog_alert);
                alertDialog2.show();
    
        }
    
    }
    

3 个答案:

答案 0 :(得分:0)

您如何填充列表视图?如果您使用的是阵列,则可以从阵列中删除数据并重新加载列表视图。

答案 1 :(得分:0)

如果您有要删除的职位,可以致电

listOfCases.remove(position);
notifyDataSetChanged();

答案 2 :(得分:0)

使用您拥有的代码,如果您将引用传递给ShowingDialogCancel(MyCasesListAdapter adapter, View view),那么您可以添加以下内容:

adapter.onClick(view);

到“是”按钮。