0

Hello I have a ListView with CustomBaseAdapter.The listview contents EditText and DeleteButton .Now i want to delete the row on the button click event. I tried adapter.removeViewAt(int) method but it gives me exception that removeViewAt(int) not supported by AdapterView. I tried many solutions but none are work please help me.

I get Following ErrorLog -

12-01 12:28:53.499: W/System.err(464): java.lang.UnsupportedOperationException: removeView(View) is not supported in AdapterView
12-01 12:28:53.499: W/System.err(464):  at android.widget.AdapterView.removeView(AdapterView.java:489)
12-01 12:28:53.499: W/System.err(464):  at android.view.View.performClick(View.java:2408)
12-01 12:28:53.499: W/System.err(464):  at android.view.View$PerformClick.run(View.java:8816)
12-01 12:28:53.499: W/System.err(464):  at android.os.Handler.handleCallback(Handler.java:587)
12-01 12:28:53.499: W/System.err(464):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 12:28:53.509: W/System.err(464):  at android.os.Looper.loop(Looper.java:123)
12-01 12:28:53.519: W/System.err(464):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-01 12:28:53.519: W/System.err(464):  at java.lang.reflect.Method.invokeNative(Native Method)
12-01 12:28:53.519: W/System.err(464):  at java.lang.reflect.Method.invoke(Method.java:521)
12-01 12:28:53.519: W/System.err(464):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-01 12:28:53.519: W/System.err(464):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-01 12:28:53.519: W/System.err(464):  at dalvik.system.NativeStart.main(Native Method)

and my adapter is -

public class LazyAdapter extends BaseAdapter {

private Activity activity;   
private static LayoutInflater inflater=null;
private int noofrows;
private int LayoutRid;


static Map<Integer, String> Amt = new LinkedHashMap<Integer, String>();
static String AmtValue;


public LazyAdapter(Activity a, int rows, int Layoutid) {
    activity = a;       
    noofrows = rows;
    LayoutRid = Layoutid;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
}


public int getCount() {
    return noofrows;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}


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

    ViewHolder holder = new ViewHolder();


    convertView = inflater.inflate(_LayoutRid, null );  

        holder.Amount = (EditText)convertView.findViewById(R.id.SESDedCCetDedAmount);

        holder.btnDel = (Button)convertView.findViewById(R.id.btnMinus);

        convertView.setTag(holder);     


        holder.Amount.setText(AmtValue == null? "" : AmtValue);
        holder.btnDel.setText(CodeValue == null? "" : CodeValue);


        holder.Amount.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            public void afterTextChanged(Editable s) {
                Amt.put(position, s.toString());
            }
        });


        holder.btnDel.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Take List from parent view
                ParentActivity.list.removeViewAt(position);
                notifyDataSetChanged();
            }
        });


    return convertView;
}

static class ViewHolder {

    EditText Amount;
    Button btnDel;

}

}

nyt23
  • 63
  • 1
  • 1
  • 9

4 Answers4

5

Just try for this code,

 btnOrImage.setOnClickListener(new OnClickListener() 
 {
    @Override
    public void onClick(View v) 
    {
        // delete query fire here whatever related to requirements.
        mList.remove(position);//you can delete your item here
        notifyDataSetChanged();
    }
});

This method used for notifyDataSetChanged() refresh to getView() method from your BaseAdapter.

Najib.Nj
  • 3,706
  • 1
  • 25
  • 39
  • Use can also used for it arraylist and listview both. – Najib.Nj Dec 01 '12 at 09:22
  • obeously working fine for BaseAdapter and what issue for your apps just see to all code step by step. – Najib.Nj Dec 01 '12 at 09:41
  • I tried but it gives me error that remove() is not applicable for listview. – nyt23 Dec 01 '12 at 09:47
  • I tried such solution (removing data from the adapter arraylist and then refreshing) but didn't work. Opened another issue here: http://stackoverflow.com/questions/38250987/removing-an-item-from-the-dropdown-list-of-a-baseadapter – Marco Zanetti Jul 08 '16 at 09:26
0

Try this,

holder.delete.setOnClickListener(new OnClickListener() 

    {
        @Override
        public void onClick(View v) 
        {
            your_item.remove(position);//you can delete your item here

            notifyDataSetChanged();
        }
    });

notifyDataSetChanged is to refresh view and updated the data.

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
0
myHolder.btn_delete.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
      // remove data from List and call 
      notifyDataSetChanged(); <-- will remove old data
    }
});
MAC
  • 15,799
  • 8
  • 54
  • 95
-1

I was trying to remove it from Arraylist, but actually I stored it in Hashmap. I had to remove it from there: hashmap.remove(position); solved my issue.

double-beep
  • 5,031
  • 17
  • 33
  • 41
nyt23
  • 63
  • 1
  • 1
  • 9