Reputation: 53
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)
Upvotes: 0
Views: 328
Reputation: 67286
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();
}
});
Upvotes: 2