OnClicking在Android的ListView中删除项目

OnClicking在Android的ListView中删除项目

本文介绍了OnClicking在Android的ListView中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现删除功能,从列表视图中删除的项目。点击文本删除项目项目应在ListView中删除。
我已经试过,但我在code得到一个错误。
我没有对如何纠正错误的想法。

下面是我的code:

 公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
    {
      最后ViewHolder持有人;        如果(convertView == NULL)
        {
            convertView = inflater.inflate(R.layout.selecteditemlistview,NULL);
            持有人=新ViewHolder();            holder.textViewSelectedText =(TextView中)convertView.findViewById(R.id.selectedtext);
            holder.price =(TextView中)convertView.findViewById(R.id.selectitemprice);
            holder.image =(ImageView的)convertView.findViewById(R.id.selectitemimage);
            holder.qty =(EditText上)convertView.findViewById(R.id.selectqty);
            holder.total =(TextView中)convertView.findViewById(R.id.price);            holder.deleteButton =(TextView中)convertView.findViewById(R.id.delete);
            convertView.setTag(保持器);
        }
        其他
        {
            支架=(ViewHolder)convertView.getTag();
        }
        串金额= holder.qty.getText()的toString()。
        最终的双重价格1 = Double.parseDouble(itemprice.get(位置));
        INT QUT =的Integer.parseInt(holder.qty.getText()的toString());
        双总=(价格1 * QUT);
        holder.textViewSelectedText.setText(arr1.get(位置));
        holder.price.setText(itemprice.get(位置));
        holder.image.setImageBitmap(itemimage.get(位置));
        holder.total.setText(将String.valueOf(总));        最后CartItem mCartItem =(CartItem)的getItem(位置);
        holder.mTextView.setText(mCartItem.product_name);
        holder.deleteButton.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                对象cartItems;
                CustomAdapter myAdapter =新CustomAdapter(CustomAdapter.this,R.layout.selecteditemlistview,cartItems);
                ListView控件列表视图=(ListView控件)findViewById(R.id.delete);
                listview.setAdapter(myAdapter);
                        cartItems.remove(对象);
                        myAdapter.notifyDatasetChanged();
            }        });        返回convertView;
    }    类ViewHolder
    {
         公众的TextView deleteButton;
         公众的TextView mTextView;        TextView的textViewSelectedText = NULL;
        TextView的价格= NULL;
        ImageView的图像= NULL;
        的EditText数量= NULL;
        TextView的总= NULL;
    }
    公共类CartItem {
        公众诠释身份证;
        公共字符串PRODUCT_NAME;
    }

确切的错误是在低于code:

  holder.deleteButton.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                对象cartItems;
                CustomAdapter myAdapter =新CustomAdapter(CustomAdapter.this,R.layout.selecteditemlistview,cartItems);
                ListView控件列表视图=(ListView控件)findViewById(R.id.delete);
                listview.setAdapter(myAdapter);                对象的文档,删除= arrayAdapter.getItem([位置]);
                arrayAdapter.remove(文档,删除);            }        });

请帮我解决这个问题。谢谢。

这是我的logcat错误:

  12月5日至18日:38:04.630:我/的System.out(514):打印
       12月5日至18日:38:04.711:我/的System.out(514):152.00]
       12月5日至18日:38:04.711:我/的System.out(514):ARR:1
       12月5日至18日:38:04.721:我/的System.out(514):152.0
       12月5日至18日:38:04.781:D / AndroidRuntime(514):关闭VM
       12月5日至18日:38:04.781:W / dalvikvm(514):主题ID = 1:螺纹未捕获的异常退出(组= 0x4001d800)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):致命异常:主要
       12月5日至18日:38:04.811:E / AndroidRuntime(514):java.lang.ClassCastException:java.lang.String中
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在com.customer.CustomAdapter.getView(CustomAdapter.java:106)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.widget.AbsListView.obtainView(AbsListView.java:1315)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.widget.ListView.makeAndAddView(ListView.java:1727)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.widget.ListView.fillDown(ListView.java:652)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.widget.ListView.fillFromTop(ListView.java:709)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.widget.ListView.layoutChildren(ListView.java:1580)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.widget.AbsListView.onLayout(AbsListView.java:1147)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.view.View.layout(View.java:7035)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.view.View.layout(View.java:7035)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.view.View.layout(View.java:7035)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.view.View.layout(View.java:7035)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.widget.FrameLayout.onLayout(FrameLayout.java:333)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.view.View.layout(View.java:7035)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.os.Handler.dispatchMessage(Handler.java:99)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.os.Looper.loop(Looper.java:123)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在android.app.ActivityThread.main(ActivityThread.java:4627)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在java.lang.reflect.Method.invokeNative(本机方法)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在java.lang.reflect.Method.invoke(Method.java:521)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
       12月5日至18日:38:04.811:E / AndroidRuntime(514):在dalvik.system.NativeStart.main(本机方法)


解决方案

您需要的是简单地删除您从中填充适配器的数据集项,然后使用通知适配器 notifyDataSetChanged()

  holder.deleteButton.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                 yourdataset.remove(位置);
                 notifyDataSetChanged();
            }
        });

I am trying to implement Delete functional to delete item from the listview. Click On the Text "Remove Item" Item should be removed from the ListView.I have tried but, I am getting an error in the code.I dont have any Idea of how to correct the errors.

Here is my code:

   public View getView(final int position, View convertView, ViewGroup parent)
    {


      final  ViewHolder holder;

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.selecteditemlistview, null);
            holder = new ViewHolder();

            holder.textViewSelectedText = (TextView)convertView.findViewById(R.id.selectedtext);
            holder.price=(TextView)convertView.findViewById(R.id.selectitemprice);
            holder.image=(ImageView)convertView.findViewById(R.id.selectitemimage);
            holder.qty=(EditText)convertView.findViewById(R.id.selectqty);
            holder.total=(TextView)convertView.findViewById(R.id.price);

            holder.deleteButton = (TextView) convertView.findViewById(R.id.delete);


            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        String amount=holder.qty.getText().toString();
        final Double price1=Double.parseDouble(itemprice.get(position));
        int qut=Integer.parseInt(holder.qty.getText().toString());
        Double total=(price1*qut);
        holder.textViewSelectedText.setText(arr1.get(position));
        holder.price.setText(itemprice.get(position));
        holder.image.setImageBitmap(itemimage.get(position));
        holder.total.setText(String.valueOf(total));

        final CartItem mCartItem = (CartItem) getItem(position);


        holder.mTextView.setText(mCartItem.product_name);
        holder.deleteButton.setOnClickListener(new OnClickListener() {


            public void onClick(View v) {
                Object cartItems;
                CustomAdapter myAdapter = new CustomAdapter(CustomAdapter.this, R.layout.selecteditemlistview, cartItems);
                ListView listview = (ListView) findViewById(R.id.delete);
                listview.setAdapter(myAdapter);
                        cartItems.remove(object);
                        myAdapter.notifyDatasetChanged();


            }

        });

        return convertView;
    }

    class ViewHolder
    {
         public TextView deleteButton;
         public TextView mTextView;

        TextView textViewSelectedText = null;
        TextView price=null;
        ImageView image=null;
        EditText qty=null;
        TextView total=null;
    }
    public class CartItem {
        public int id;
        public String product_name;
    }

The Exact error is in the below code:

     holder.deleteButton.setOnClickListener(new OnClickListener() {


            public void onClick(View v) {
                Object cartItems;
                CustomAdapter myAdapter = new CustomAdapter(CustomAdapter.this, R.layout.selecteditemlistview, cartItems);
                ListView listview = (ListView) findViewById(R.id.delete);
                listview.setAdapter(myAdapter);

                Object toRemove = arrayAdapter.getItem([POSITION]);
                arrayAdapter.remove(toRemove);

            }

        });

Please Help me to solve this problem. Thank you..

This are my Logcat errors:

       05-18 12:38:04.630: I/System.out(514): Printing
       05-18 12:38:04.711: I/System.out(514): [152.00]
       05-18 12:38:04.711: I/System.out(514): arr: 1
       05-18 12:38:04.721: I/System.out(514): 152.0
       05-18 12:38:04.781: D/AndroidRuntime(514): Shutting down VM
       05-18 12:38:04.781: W/dalvikvm(514): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
       05-18 12:38:04.811: E/AndroidRuntime(514): FATAL EXCEPTION: main
       05-18 12:38:04.811: E/AndroidRuntime(514): java.lang.ClassCastException: java.lang.String
       05-18 12:38:04.811: E/AndroidRuntime(514):   at com.customer.CustomAdapter.getView(CustomAdapter.java:106)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.widget.AbsListView.obtainView(AbsListView.java:1315)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at  android.widget.ListView.makeAndAddView(ListView.java:1727)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.widget.ListView.fillDown(ListView.java:652)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.widget.ListView.fillFromTop(ListView.java:709)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.widget.ListView.layoutChildren(ListView.java:1580)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.widget.AbsListView.onLayout(AbsListView.java:1147)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.view.View.layout(View.java:7035)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.view.View.layout(View.java:7035)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.view.View.layout(View.java:7035)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.view.View.layout(View.java:7035)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.view.View.layout(View.java:7035)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.os.Handler.dispatchMessage(Handler.java:99)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.os.Looper.loop(Looper.java:123)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at android.app.ActivityThread.main(ActivityThread.java:4627)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at java.lang.reflect.Method.invokeNative(Native Method)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at java.lang.reflect.Method.invoke(Method.java:521)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
       05-18 12:38:04.811: E/AndroidRuntime(514):   at dalvik.system.NativeStart.main(Native Method)
解决方案

What you need is simply remove the dataset item from which you are populating your Adapter and then notify the adapter using notifyDataSetChanged()

        holder.deleteButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 yourdataset.remove(position);
                 notifyDataSetChanged();
            }
        });

这篇关于OnClicking在Android的ListView中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:43