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

huangapple go评论76阅读模式
英文:

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

问题

我正在使用 Parse Server 来开发我的应用我创建了一个名为 "Notification" 的 ParseObject它包含以下字段
1. 价格price
2. 名称name

接下来我有一个自定义的 ListView其中包含文本视图显示了价格和名称列表项中还有一个按钮和一些编辑框EditText)。当点击按钮时我会将编辑框中的内容保存到一个新的 ParseObject 中然后删除该列表项以及包含该名称的行然后更新 ListView

问题是列表视图的更改影响了所有用户我不知道如何仅更新当前用户的列表视图

以下是我的代码

```java
public class Notification_shop extends AppCompatActivity {
    // ...(省略其他部分)
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification_shop);
        // ...(省略其他部分)

        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 View getView(final int position, View view, ViewGroup parent) {
            // ...(省略其他部分)
            
            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // ...(省略其他部分)
                    
                    // 获取点击的项目名称
                    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 {
                                // 处理异常情况
                            }
                        }
                    });

                    // 更新列表视图
                    v.setVisibility(View.GONE);
                    notifyDataSetChanged();

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

我尝试在各个地方寻找解决方案,但未能找到合适的答案。我已经困在这个问题上四天了,仍然无法解决。非常感谢您提前的帮助。

英文:

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&lt;String&gt; prices,product_name_array;
EditText shop_product_price, shop_product_offer,quantity ;
TextView username, product_name, product_price;
Button submit, cancel;
ParseQuery&lt;ParseObject&gt; 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&lt;&gt; (  );
product_name_array = new ArrayList&lt;&gt; (  );
listView = findViewById ( R.id.listview );
csadapter = new adapter ( );
query = new ParseQuery&lt;ParseObject&gt; ( &quot;Notification&quot; );
query.findInBackground ( new FindCallback&lt;ParseObject&gt; ( ) {
@Override
public void done(List&lt;ParseObject&gt; objects, ParseException e) {
if (e == null) {
for (ParseObject object : objects) {
prices.add ( object.getString ( &quot;price&quot; ) );
product_name_array.add ( object.getString ( &quot;name&quot; ) );
}
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 ( &quot;Users_Notifications&quot; );
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 ( &quot;Shop_price&quot;,shop_product_price.getText ().toString () );
user_notifications.put ( &quot;shop_offer&quot;,shop_product_offer.getText ().toString () );
user_notifications.put ( &quot;product_quantity&quot;,quantity.getText ().toString () );
user_notifications.put ( &quot;parent&quot;,ParseUser.getCurrentUser () );
user_notifications.saveInBackground ( new SaveCallback ( ) {
@Override
public void done(ParseException e) {
if(e==null){
Toast.makeText ( Notification_shop.this, &quot;details sent &quot;, Toast.LENGTH_SHORT ).show ( );
}
}
} );
gets = product_name_array.get( position );
query.whereEqualTo ( &quot;name&quot;,gets );
query.findInBackground ( new FindCallback&lt;ParseObject&gt; ( ) {
@Override
public void done(List&lt;ParseObject&gt; objects, ParseException e) {
if(e==null ){
for (ParseObject obj: objects){
obj.deleteInBackground ();
}
}else {
}
}
} );
// Toast.makeText ( Notification_shop.this, &quot;button is clicked&quot;, Toast.LENGTH_SHORT ).show ( );
v.setVisibility ( View.GONE );
notifyDataSetChanged ();
int positionToRemove = (int)v.getTag();
prices.remove ( positionToRemove );
Log.i ( &quot;nice&quot;,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.

答案1

得分: 0

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();
                            }
                        }
                    });

                    // Rest of the code...

                }
            });

            return view;
        }
    }
}
英文:
public class Notification_shop extends AppCompatActivity {
ListView listView;
String gets,objectid,sat;
ArrayList&lt;String&gt; 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&lt;ParseObject&gt; query;
ParseQuery&lt;ParseUser&gt; 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&lt;&gt; (  );
product_name_array = new ArrayList&lt;&gt; (  );
Notification_id = new ArrayList&lt;&gt; (  );
listView = findViewById ( R.id.listview );
csadapter = new adapter ( );
user_notifications = new ParseObject ( &quot;Users_Notifications&quot; );
parseQuery = ParseUser.getQuery ();
parseQuery.selectKeys ( Arrays.asList ( &quot;list&quot; ) );
parseQuery.whereExists ( &quot;code&quot; );
parseQuery.whereEqualTo ( &quot;cateogory&quot;, &quot;Book&quot; );
parseQuery.getInBackground ( ParseUser.getCurrentUser ( ).getObjectId ( ), new GetCallback&lt;ParseUser&gt; ( ) {
@Override
public void done(ParseUser object, ParseException e) {
if(e==null){
ArrayList&lt;String&gt; userTastesGot = (ArrayList&lt;String&gt;) object.get(&quot;list&quot;);
stockArr = new String[userTastesGot.size()];
stockArr = userTastesGot.toArray(stockArr);
for( String s : stockArr) {
sat = s;
query = new ParseQuery&lt;ParseObject&gt; ( &quot;Notification&quot; );
query.whereContainedIn ( &quot;objectId&quot;, Arrays.asList ( sat ) );
query.findInBackground ( new FindCallback&lt;ParseObject&gt; ( ) {
@Override
public void done(List&lt;ParseObject&gt; objects, ParseException e) {
if (e == null) {
for (ParseObject object : objects)
{
prices.add ( object.getString ( &quot;price&quot; ) );
product_name_array.add ( object.getString ( &quot;name&quot; ) );
gets = object.getObjectId ( );
Notification_id.add ( gets );
Log.i ( &quot;mess&quot;, gets );
}
listView.setAdapter ( csadapter );
} else {
e.printStackTrace ( );
}
}
} );
Log.i ( &quot;ring&quot;, sat);
}
Log.i (&quot;User&quot;, &quot;Retrieved &quot; + userTastesGot);
Log.i ( &quot;name&quot;, 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 ( &quot;Shop_price&quot;,shop_product_price.getText ().toString () );
user_notifications.put ( &quot;shop_offer&quot;,shop_product_offer.getText ().toString () );
user_notifications.put ( &quot;product_quantity&quot;,quantity.getText ().toString () );
user_notifications.put ( &quot;ids&quot;,ParseUser.getCurrentUser ().getObjectId () );
user_notifications.put ( &quot;names&quot;,product_name_array.get ( position ) );
user_notifications.saveInBackground ( new SaveCallback ( ) {
@Override
public void done(ParseException e) {
if(e==null){
Toast.makeText ( Notification_shop.this, &quot;details sent &quot;, Toast.LENGTH_SHORT ).show ( );
}
}
} );
/*  gets = product_name_array.get( position );
Log.i ( &quot;position&quot;,gets );
query.whereEqualTo ( &quot;name&quot;,gets );
query.findInBackground ( new FindCallback&lt;ParseObject&gt; ( ) {
@Override
public void done(List&lt;ParseObject&gt; objects, ParseException e) {
if(e==null ){
for (ParseObject obj: objects){
obj.deleteInBackground ();
}
}else {
}
}
} );*/
// Toast.makeText ( Notification_shop.this, &quot;button is clicked&quot;, Toast.LENGTH_SHORT ).show ( );
objectid = Notification_id.get ( position );
for(String t : stockArr) {
if (objectid.matches ( t )) {
Log.i ( &quot;drive&quot;, &quot;well done&quot; );
ParseUser.getCurrentUser ().getList ( &quot;list&quot; ).remove ( objectid );
List newlist = ParseUser.getCurrentUser ().getList ( &quot;list&quot; );
ParseUser.getCurrentUser ().remove ( &quot;list&quot; );
ParseUser.getCurrentUser ().put ( &quot;list&quot;,newlist );
ParseUser.getCurrentUser ().saveInBackground ();
}
}
Log.i ( &quot;tea&quot;,objectid );
v.setVisibility ( View.GONE );
notifyDataSetChanged ();
int positionToRemove = (int)v.getTag();
product_name_array.remove ( positionToRemove );
prices.remove ( positionToRemove );
Log.i ( &quot;nice&quot;,String.valueOf ( positionToRemove ) );
}
} );
return view;
}
}

}

huangapple
  • 本文由 发表于 2020年9月8日 11:45:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63786827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定