Search code examples
javaandroidparse-platform

How to delete a row from parse and update custom listview for current user?


i am using parse server for my app. i created a Parseobject "Notification" with fields : 1.price 2. name

then i have a custom listview with textviews containing those price and name. There is also button and some EDITTEXT in that listview, on clicking that button i am saving the edittext in new Parseobject and deleting that position of the listview and the row containing that name. After that i am updating the listview.

the problem is that the listview is changed for all the users and i don't know how to update it for the current user.

here is my code:

public class Notification_shop extends AppCompatActivity {
ListView listView;
String gets;
ArrayList<String> prices,product_name_array;

EditText shop_product_price, shop_product_offer,quantity ;
TextView username, product_name, product_price;
Button submit, cancel;
ParseQuery<ParseObject> query;
ParseObject user_notifications;
adapter csadapter;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate ( savedInstanceState );
    setContentView ( R.layout.activity_notification_shop );
    prices = new ArrayList<> (  );
    product_name_array = new ArrayList<> (  );
   listView = findViewById ( R.id.listview );
   csadapter = new adapter ( );
    query = new ParseQuery<ParseObject> ( "Notification" );

    query.findInBackground ( new FindCallback<ParseObject> ( ) {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                for (ParseObject object : objects) {
                    prices.add ( object.getString ( "price" ) );
                    product_name_array.add ( object.getString ( "name" ) );




                }
                listView.setAdapter ( csadapter );

            } else {
                e.printStackTrace ( );
            }
        }
    } );


}
class adapter extends BaseAdapter {

    @Override
    public int getCount() {
        return prices.size ( );
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        view = getLayoutInflater ( ).inflate ( R.layout.custom1_layout, null );
        shop_product_price = (EditText) view.findViewById ( R.id.shop_product_price );
        shop_product_offer = (EditText) view.findViewById ( R.id.shop_product_offer );
        quantity = (EditText) view.findViewById ( R.id.quantity );
        product_name = (TextView) view.findViewById ( R.id.name );
        product_price = (TextView) view.findViewById ( R.id.prices );
        submit = (Button) view.findViewById ( R.id.button3 );
        cancel = (Button) view.findViewById ( R.id.cancel );
        submit.setTag ( position );
        user_notifications = new ParseObject ( "Users_Notifications" );

        product_price.setText ( prices.get ( position ) );
        product_name.setText ( product_name_array.get ( position ) );
        submit.setOnClickListener (  new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {

                user_notifications.put ( "Shop_price",shop_product_price.getText ().toString () );
                user_notifications.put ( "shop_offer",shop_product_offer.getText ().toString () );
                user_notifications.put ( "product_quantity",quantity.getText ().toString () );
                user_notifications.put ( "parent",ParseUser.getCurrentUser () );
                user_notifications.saveInBackground ( new SaveCallback ( ) {
                    @Override
                    public void done(ParseException e) {
                        if(e==null){
                            Toast.makeText ( Notification_shop.this, "details sent ", Toast.LENGTH_SHORT ).show ( );
                        }
                    }
                } );

               gets = product_name_array.get( position );
                query.whereEqualTo ( "name",gets );
                query.findInBackground ( new FindCallback<ParseObject> ( ) {
                    @Override
                    public void done(List<ParseObject> objects, ParseException e) {
                        if(e==null ){
                            for (ParseObject obj: objects){
                                obj.deleteInBackground ();
                            }
                        }else {

                        }
                    }
                } );

               // Toast.makeText ( Notification_shop.this, "button is clicked", Toast.LENGTH_SHORT ).show ( );

                v.setVisibility ( View.GONE );
                notifyDataSetChanged ();

                int positionToRemove = (int)v.getTag();
                prices.remove ( positionToRemove );
                Log.i ( "nice",String.valueOf ( positionToRemove ) );





            }
        } );
        return view;

    }

}

i tried to find solution everywhere but unable to find a proper answer. it's been 4 days now and i am still stuck in that question. i really need help. thanks in advance.


Solution

  • public class Notification_shop extends AppCompatActivity {
    ListView listView;
    String gets,objectid,sat;
    ArrayList<String> prices,product_name_array,Notification_id;
    String[] stockArr;
    EditText shop_product_price, shop_product_offer,quantity ;
    TextView username, product_name, product_price;
    Button submit, cancel;
    ParseQuery<ParseObject> query;
    ParseQuery<ParseUser> parseQuery;
    ParseObject user_notifications;
    adapter csadapter;
    int i,j;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_notification_shop );
        prices = new ArrayList<> (  );
        product_name_array = new ArrayList<> (  );
        Notification_id = new ArrayList<> (  );
       listView = findViewById ( R.id.listview );
       csadapter = new adapter ( );
        user_notifications = new ParseObject ( "Users_Notifications" );
    
     parseQuery = ParseUser.getQuery ();
     parseQuery.selectKeys ( Arrays.asList ( "list" ) );
     parseQuery.whereExists ( "code" );
     parseQuery.whereEqualTo ( "cateogory", "Book" );
    parseQuery.getInBackground ( ParseUser.getCurrentUser ( ).getObjectId ( ), new GetCallback<ParseUser> ( ) {
        @Override
        public void done(ParseUser object, ParseException e) {
            if(e==null){
                ArrayList<String> userTastesGot = (ArrayList<String>) object.get("list");
                stockArr = new String[userTastesGot.size()];
    
                stockArr = userTastesGot.toArray(stockArr);
                for( String s : stockArr) {
                    sat = s;
    
    
                    query = new ParseQuery<ParseObject> ( "Notification" );
                    query.whereContainedIn ( "objectId", Arrays.asList ( sat ) );
                    query.findInBackground ( new FindCallback<ParseObject> ( ) {
                        @Override
                        public void done(List<ParseObject> objects, ParseException e) {
                            if (e == null) {
    
                                for (ParseObject object : objects)
                                {
                                    prices.add ( object.getString ( "price" ) );
                                    product_name_array.add ( object.getString ( "name" ) );
                                    gets = object.getObjectId ( );
                                    Notification_id.add ( gets );
                                    Log.i ( "mess", gets );
                                }
    
    
                                listView.setAdapter ( csadapter );
    
                            } else {
                                e.printStackTrace ( );
                            }
                        }
                    } );
    
                    Log.i ( "ring", sat);
                }
    
    
                Log.i ("User", "Retrieved " + userTastesGot);
                Log.i ( "name", ParseUser.getCurrentUser ().getUsername () );
            }
        }
    } );
    
    }
    class adapter extends BaseAdapter {
    
        @Override
        public int getCount() {
            return product_name_array.size ();
        }
    
        @Override
        public Object getItem(int position) {
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(final int position, View view, ViewGroup parent) {
            view = getLayoutInflater ( ).inflate ( R.layout.custom1_layout, null );
            shop_product_price = (EditText) view.findViewById ( R.id.shop_product_price );
            shop_product_offer = (EditText) view.findViewById ( R.id.shop_product_offer );
            quantity = (EditText) view.findViewById ( R.id.quantity );
            product_name = (TextView) view.findViewById ( R.id.name );
            product_price = (TextView) view.findViewById ( R.id.prices );
            submit = (Button) view.findViewById ( R.id.button3 );
            cancel = (Button) view.findViewById ( R.id.cancel1 );
            submit.setTag ( position );
    
    
            product_name.setText ( product_name_array.get ( position ) );
           product_price.setText ( prices.get ( position ) );
            submit.setOnClickListener (  new View.OnClickListener ( ) {
                @Override
                public void onClick(View v) {
    
                    user_notifications.put ( "Shop_price",shop_product_price.getText ().toString () );
                    user_notifications.put ( "shop_offer",shop_product_offer.getText ().toString () );
                    user_notifications.put ( "product_quantity",quantity.getText ().toString () );
                    user_notifications.put ( "ids",ParseUser.getCurrentUser ().getObjectId () );
                    user_notifications.put ( "names",product_name_array.get ( position ) );
                    user_notifications.saveInBackground ( new SaveCallback ( ) {
                        @Override
                        public void done(ParseException e) {
                            if(e==null){
                                Toast.makeText ( Notification_shop.this, "details sent ", Toast.LENGTH_SHORT ).show ( );
                            }
                        }
                    } );
    
                  /*  gets = product_name_array.get( position );
                    Log.i ( "position",gets );
                    query.whereEqualTo ( "name",gets );
                    query.findInBackground ( new FindCallback<ParseObject> ( ) {
                        @Override
                        public void done(List<ParseObject> objects, ParseException e) {
                            if(e==null ){
                                for (ParseObject obj: objects){
                                    obj.deleteInBackground ();
                                }
                            }else {
    
                            }
                        }
                    } );*/
    
                   // Toast.makeText ( Notification_shop.this, "button is clicked", Toast.LENGTH_SHORT ).show ( );
                     objectid = Notification_id.get ( position );
                     for(String t : stockArr) {
                         if (objectid.matches ( t )) {
                             Log.i ( "drive", "well done" );
                             ParseUser.getCurrentUser ().getList ( "list" ).remove ( objectid );
                             List newlist = ParseUser.getCurrentUser ().getList ( "list" );
                             ParseUser.getCurrentUser ().remove ( "list" );
                             ParseUser.getCurrentUser ().put ( "list",newlist );
                             ParseUser.getCurrentUser ().saveInBackground ();
                         }
                     }
                      Log.i ( "tea",objectid );
    
                    v.setVisibility ( View.GONE );
                    notifyDataSetChanged ();
    
                    int positionToRemove = (int)v.getTag();
                    product_name_array.remove ( positionToRemove );
                    prices.remove ( positionToRemove );
                    Log.i ( "nice",String.valueOf ( positionToRemove ) );
    
    
    
    
    
                }
            } );
            return view;
    
        }
    
    }
    

    }