1

リストビューを作成しましたが、そのリストビューの行に触れることでアイテムを削除できますが、常に最後のアイテムを削除し、いくつかの削除回数の後、インデックスが範囲外のエラーを示します

システムは、アイテムをタッチしてダイアログボックスを開き、印刷をクリックすると、アイテムがアイテムを削除し、数秒間アクティビティを開きます。

ここにリストアダプタークラスがあります

   @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;
    }
4

1 に答える 1

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 は行全体を参照するため、代わりにアクティビティで 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
    }
});
于 2013-01-11T18:52:49.213 に答える