android删除listview删除最后一个元素的任何项目

时间:2013-01-11 18:47:21

标签: android listview android-activity

我制作了一个列表视图你可以通过触摸该列表视图的行来删除一个项目,但它总是删除最后一个项目,并且在一些删除时间之后它会显示索引超出范围的错误

系统是当你打开一个项目时,打开一个对话框你点击打印然后项目将删除该项目并打开一个活动几秒钟。

这是列表适配器类

   @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final int positionplayer = position;
        ViewHolderaway1 holder;

        if (convertView == null) {

            View row = inflater.inflate(R.layout.readonlyvendorlist, parent,
                    false);

            row.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    // set title
                    try {
                        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                context);

                        alertDialogBuilder.setTitle(values.get(positionplayer).Voucherref
                                + "  Sell");

                        // set dialog message
                        alertDialogBuilder
                                .setMessage(
                                        values.get(positionplayer)
                                                .getVoucherref() + "For Print")
                                .setCancelable(false)
                                .setPositiveButton("Print Voucher",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int id) {
                                                // if this button is clicked,
                                                // close

                                                PrintTestAcitvity.printettext = values
                                                        .get(positionplayer).PrinterText;
                                                ListService.printerlist
                                                        .remove(positionplayer);

                                                Datasync.storedataprint(context);
                                                notifyDataSetChanged();
                                                Intent ia = new Intent(context,
                                                        PrintTestAcitvity.class);

                                                context.startActivity(ia);

                                            }
                                        })
                                .setNegativeButton("Cancell",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int id) {
                                                // if this button is clicked,
                                                // just close
                                                // the dialog box and do nothing
                                                Toast.makeText(
                                                        context,
                                                        "Printing Data store for locally",
                                                        Toast.LENGTH_LONG)
                                                        .show();

                                                dialog.cancel();
                                            }
                                        });

                        // create alert dialog
                        AlertDialog alertDialog = alertDialogBuilder.create();

                        // show it
                        alertDialog.show();
                    } catch (IndexOutOfBoundsException e) {
                        // TODO: handle exception
                        notifyDataSetChanged();
                    }
                }
            });

            holder = new ViewHolderaway1();

            holder.ProductItemID = (TextView) row
                    .findViewById(R.id.ProductItemID);

            holder.VoucherCost = (TextView) row.findViewById(R.id.VoucherCost);

            holder.SupplierID = (TextView) row.findViewById(R.id.SupplierID);

            Log.d("goru", "gadha");

            row.setTag(holder);

            holder = (ViewHolderaway1) row.getTag();
//          Printer Productsdata = values.get(positionplayer);
//          if (Productsdata != null) {
//              holder.ProductItemID.setText("Print");
//              holder.VoucherCost.setText(Productsdata.getVoucherref());
//              // holder.SupplierID.setText(resid)
//              // holder.SupplierID.setVisibility(View.GONE);
//
//              if (Productsdata.getVoucherref().contains("Voda")) {
//                  holder.VoucherCost.setBackgroundColor(Color.RED);
//                  holder.VoucherCost.setTextColor(Color.WHITE);
//                  holder.SupplierID.setBackgroundDrawable(getContext()
//                          .getResources().getDrawable(R.drawable.voda));
//              }
//              if (Productsdata.getVoucherref().contains("Eco")) {
//                  holder.VoucherCost.setBackgroundColor(Color.BLUE);
//                  holder.VoucherCost.setTextColor(Color.WHITE);
//                  holder.SupplierID.setBackgroundDrawable(getContext()
//                          .getResources().getDrawable(R.drawable.eco));
//              }
//
//          }
            convertView = row;
        }else
        {
            holder = (ViewHolderaway1) convertView.getTag();
        }
            Printer Productsdata = values.get(positionplayer);
            if (Productsdata != null) {
                holder.ProductItemID.setText("Print");
                holder.VoucherCost.setText(Productsdata.getVoucherref());
                // holder.SupplierID.setText(resid)
                // holder.SupplierID.setVisibility(View.GONE);

                if (Productsdata.getVoucherref().contains("Voda")) {
                    holder.VoucherCost.setBackgroundColor(Color.RED);
                    holder.VoucherCost.setTextColor(Color.WHITE);
                    holder.SupplierID.setBackgroundDrawable(getContext()
                            .getResources().getDrawable(R.drawable.voda));
                }
                if (Productsdata.getVoucherref().contains("Eco")) {
                    holder.VoucherCost.setBackgroundColor(Color.BLUE);
                    holder.VoucherCost.setTextColor(Color.WHITE);
                    holder.SupplierID.setBackgroundDrawable(getContext()
                            .getResources().getDrawable(R.drawable.eco));
                }

            }
        return convertView;
    }

1 个答案:

答案 0 :(得分:2)

getView()中的逻辑存在一些主要缺陷,这是最大的缺陷:

    if (convertView != null)
    {  
        holder = new ViewHolderaway1();


        return convertView;
    }

这将返回已回收的行而不进行更改...至少需要更改布局的书面内容。

查看此答案中的代码:https://stackoverflow.com/a/4145996/1267661。您应该将其用作模板。它演示了如何正确使用ViewHolder并在从ListView的RecycleBin返回视图后显示准确的数据。


<强>加成
这个新代码很多更有效率!另外我想我明白为什么只删除最后一行。在您的AlertDialog中,您引用了positionplayer,但onClick()代码在 getView()完成后运行,因此positionplayer在此处无效。由于OnClickListener引用整行,因此您应该在Activity中使用OnItemClickListener,这将有助于删除相应的行:

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Move your AlertDialog code here and use position instead of positionplayer
    }
});