我正在尝试实现删除功能以从列表视图中删除项目。单击文本“删除项目”项应从ListView中删除。 我试过但是,我在代码中遇到错误。 我对如何纠正错误没有任何想法。
这是我的代码:
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;
}
确切错误位于以下代码中:
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);
}
});
请帮我解决这个问题。谢谢..
这是我的Logcat错误:
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)
答案 0 :(得分:2)
您需要的只是删除用于填充适配器的数据集项,然后使用notifyDataSetChanged()
holder.deleteButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
yourdataset.remove(position);
notifyDataSetChanged();
}
});