简体   繁体   English

如何从解析中删除一行并更新当前用户的自定义列表视图?

[英]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我创建了一个 Parseobject "Notification" 字段: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.该列表视图中还有按钮和一些 EDITTEXT,单击该按钮时,我将编辑文本保存在新的 Parseobject 中,并删除列表视图的该位置和包含该名称的行。 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.现在已经 4 天了,我仍然被困在这个问题上。 i really need help.我真的需要帮助。 thanks in advance.提前致谢。

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;

    }

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM