0

您好,我有一个带有 CustomBaseAdapter 的 ListView。listview 内容 EditText 和 DeleteButton。现在我想删除按钮单击事件上的行。我尝试了adapter.removeViewAt(int) 方法,但它给了我AdapterView 不支持removeViewAt(int) 的异常。我尝试了许多解决方案,但没有一个有效,请帮助我。

我得到以下错误日志 -

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)

我的适配器是 -

公共类 LazyAdapter 扩展 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;

}

}

4

4 回答 4

5

试试这个代码,

 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();
    }
});

此方法用于notifyDataSetChanged()从 BaseAdapter 刷新到 getView() 方法。

于 2012-12-01T08:21:04.780 回答
0

试试这个,

holder.delete.setOnClickListener(new OnClickListener() 

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

            notifyDataSetChanged();
        }
    });

notifyDataSetChanged是刷新视图和更新数据。

于 2012-12-01T06:34:43.957 回答
0
myHolder.btn_delete.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
      // remove data from List and call 
      notifyDataSetChanged(); <-- will remove old data
    }
});
于 2012-12-01T06:36:44.677 回答
-1

我试图将其从 中删除Arraylist,但实际上我将其存储在Hashmap. 我不得不从那里删除它:hashmap.remove(position);解决了我的问题。

于 2012-12-03T05:24:55.347 回答