0

I have a class which extends ArrayAdapter<String>. I want to have a delete image button deletes the particular row.. This is my code:

public class ViewCartList extends ArrayAdapter<String> {

    private String[] cart_item_name;
    private String[] cart_item_quan;
    private String[] cart_item_price;
    private Activity context;


    public ViewCartList(Activity context,  String[] cartitemquan, String[] cartitemname,String[] cartitemprice){
        super(context,R.layout.viewcartlist,cartitemquan);
        this.context = context;
        this.cart_item_quan = cartitemquan;
        this.cart_item_name = cartitemname;
        this.cart_item_price = cartitemprice;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        final View listViewItem = inflater.inflate(R.layout.viewcartlist, null, true);

        TextView textViewItemQuan = (TextView) listViewItem.findViewById(R.id.cart_quan);
        TextView textViewItemName = (TextView) listViewItem.findViewById(R.id.cart_item_name);
        TextView textViewItemPrice = (TextView) listViewItem.findViewById(R.id.cart_item_price);
        ImageButton imcut = (ImageButton) listViewItem.findViewById(R.id.remove_row);

        textViewItemQuan.setText(cart_item_quan[position]);
        textViewItemName.setText(cart_item_name[position]);
        textViewItemPrice.setText(cart_item_price[position]);
        imcut.setVisibility(View.VISIBLE);

        imcut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // I want to delete that particular row
            }
        });

        return listViewItem;
    }
}

And This is my Basket class where I am using this above adapter class. What to do if i want to remove a particular row from the ListView from that delete button given above..

public class Basket extends AppCompatActivity implements View.OnClickListener {

    TextView cartview;
    MyCartDatabse myDatabase;
    SQLiteDatabase sql;
    ContentValues cv;
    String wr;
    ListView list;
    public static String[] quan =null;
    public static  String[] itemname=null;
    public static String[] baseprice=null;
    TextView edit_order;
    public boolean imrow;
    ViewCartList vc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.basket);


        myDatabase = new MyCartDatabse(this,"mydb",null,1);
        sql = myDatabase.getWritableDatabase();
        cv = new ContentValues();
        list = (ListView)findViewById(R.id.listcart);
        getRecords();

        edit_order = (TextView)findViewById(R.id.edit_order);
        edit_order.setOnClickListener(this);
    }

    public void getRecords(){
         sql = myDatabase.getReadableDatabase();
         Cursor cursor = sql.rawQuery("select * from cart ",null);
         quan = new String[cursor.getCount()];
         itemname = new String[cursor.getCount()];
         baseprice = new String[cursor.getCount()];

        int i = 0;
        if(cursor.getCount()>0){
            while(cursor.moveToNext()){
                String uquan = cursor.getString(5);
                String uname = cursor.getString(1);
                String uprice = cursor.getString(4);

                quan[i] = uquan;
                itemname[i] = uname;
                baseprice[i] = uprice;
                i++;

            }
            vc = new ViewCartList(this,quan,itemname,baseprice);
            list.setAdapter(vc);
        }
        else{
            // Do something
        }

        cursor.close();
    }

    @Override
    public void onClick(View view) {
        edit_order.setText("Apply Changes");
    }
}
2
  • 1
    You cannot remove any item from String Array, Arrays in Java are not dynamic, you can use an ArrayList instead. Commented Sep 5, 2016 at 18:32
  • 1
    You should change the name of the class, to something like ViewCartAdapter (your class represents an adapter, not a list) Commented Sep 5, 2016 at 18:34

4 Answers 4

1

The elegant approach for your problem is to pass an ArrayList of object to your Adapter and then handle the delete action.

So you might consider creating an object like this.

public class Cart {
    public String cart_item_name;
    public String cart_item_quan;
    public String cart_item_price;
}

Now take an ArrayList of that class and populate the ArrayList to pass to your Adapter.

public class ViewCartList extends BaseAdapter {

    private Context context;
    private ArrayList<Cart> mCartList;

    public ViewCartList(Context context, ArrayList<Cart> mCartList) {
        this.context = context;
        this.mCartList = mCartList;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        final View listViewItem = inflater.inflate(R.layout.viewcartlist, null, true);

        TextView textViewItemQuan = (TextView) listViewItem.findViewById(R.id.cart_quan);
        TextView textViewItemName = (TextView) listViewItem.findViewById(R.id.cart_item_name);
        TextView textViewItemPrice = (TextView) listViewItem.findViewById(R.id.cart_item_price);
        ImageButton imcut = (ImageButton) listViewItem.findViewById(R.id.remove_row);

        textViewItemQuan.setText(mCartList.get(position).cart_item_quan);
        textViewItemName.setText(mCartList.get(position).cart_item_name);
        textViewItemPrice.setText(mCartList.get(position).cart_item_price);

        // Remove the following and set the visibility true from the layout.
        // imcut.setVisibility(View.VISIBLE);

        imcut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Delete the row
                mCartList.remove(position);
                notifyDatasetChanged();
            }
        });

        return listViewItem;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

position should be final as you can not access this in inner class
Thank you for the comment. Edited the answer.
Thank you for your answer. But please help me I have updated my code.
If you go with my answer, you better change your String arrays to an ArrayList of objects as I suggested.
1

If you want to completely delete the item you should consider using List<String> instead of Array

Here is updated code for List

public class ViewCartList extends ArrayAdapter<String> {

    private List<String> cart_item_name;   // Use List here
    private List<String> cart_item_quan;   // Use List here
    private List<String> cart_item_price;   // Use List here
    private Activity context;

    public ViewCartList(Activity context,  List<String> cartitemquan,    List<String> cartitemname,List<String> cartitemprice){
         super(context, R.layout.viewcartlist,cartitemquan);
         this.context = context;
         this.cart_item_quan = cartitemquan;
         this.cart_item_name = cartitemname;
         this.cart_item_price = cartitemprice;
     }

     @Override
     public View getView(final int position, View convertView, ViewGroup parent) {
         LayoutInflater inflater = context.getLayoutInflater();
         final View listViewItem = inflater.inflate(R.layout.viewcartlist, null, true);

         TextView textViewItemQuan = (TextView) listViewItem.findViewById(R.id.cart_quan);
         TextView textViewItemName = (TextView) listViewItem.findViewById(R.id.cart_item_name);
         TextView textViewItemPrice = (TextView) listViewItem.findViewById(R.id.cart_item_price);
         ImageButton imcut = (ImageButton) listViewItem.findViewById(R.id.remove_row);

         textViewItemQuan.setText(cart_item_quan.get(position));
         textViewItemName.setText(cart_item_name.get(position));
         textViewItemPrice.setText(cart_item_price.get(position));
         imcut.setVisibility(View.VISIBLE);

         imcut.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 // I want to delete that particular row
                 cart_item_quan.remove(position);
                 cart_item_name.remove(position);
                 cart_item_price.remove(position);

                 notifyDataSetChanged();
             }
        });

        return listViewItem;
    }
}

1 Comment

Thanku Sir for ur reply. But please see my updated code and help me.
0

Arrays are fixed length and can not be resized once created. You can set an element to null to remove an object reference;

imcut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // I want to delete that particular row
                 cart_item_name[position] = null;
                 cart_item_quan[position]= null;
                 cart_item_price[position] = null;
                 notifydatasetchanged();
            }
        });

Comments

0
public class ViewCartList extends ArrayAdapter<String> {

    private String[] cart_item_name;
    private String[] cart_item_quan;
    private String[] cart_item_price;
    private Activity context;


    public ViewCartList(Activity context,  String[] cartitemquan, String[] cartitemname,String[] cartitemprice){
        super(context,R.layout.viewcartlist,cartitemquan);
        this.context = context;
        this.cart_item_quan = cartitemquan;
        this.cart_item_name = cartitemname;
        this.cart_item_price = cartitemprice;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        final View listViewItem = inflater.inflate(R.layout.viewcartlist, null, true);

        TextView textViewItemQuan = (TextView) listViewItem.findViewById(R.id.cart_quan);
        TextView textViewItemName = (TextView) listViewItem.findViewById(R.id.cart_item_name);
        TextView textViewItemPrice = (TextView) listViewItem.findViewById(R.id.cart_item_price);
        ImageButton imcut = (ImageButton) listViewItem.findViewById(R.id.remove_row);

        textViewItemQuan.setText(cart_item_quan[position]);
        textViewItemName.setText(cart_item_name[position]);
        textViewItemPrice.setText(cart_item_price[position]);
        imcut.setVisibility(View.VISIBLE);
        imcut.setTag(position);
        imcut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // I want to delete that particular row
            int pos = Integer.parseInt(view.getTag().toString());
            mCartList.remove(position);
            notifyDatasetChanged();
            }
        });

        return listViewItem;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.