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

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

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

问题

  1. 我正在使用 Parse Server 来开发我的应用我创建了一个名为 "Notification" ParseObject它包含以下字段
  2. 1. 价格price
  3. 2. 名称name
  4. 接下来我有一个自定义的 ListView其中包含文本视图显示了价格和名称列表项中还有一个按钮和一些编辑框EditText)。当点击按钮时我会将编辑框中的内容保存到一个新的 ParseObject 然后删除该列表项以及包含该名称的行然后更新 ListView
  5. 问题是列表视图的更改影响了所有用户我不知道如何仅更新当前用户的列表视图
  6. 以下是我的代码
  7. ```java
  8. public class Notification_shop extends AppCompatActivity {
  9. // ...(省略其他部分)
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_notification_shop);
  14. // ...(省略其他部分)
  15. query.findInBackground(new FindCallback<ParseObject>() {
  16. @Override
  17. public void done(List<ParseObject> objects, ParseException e) {
  18. if (e == null) {
  19. for (ParseObject object : objects) {
  20. prices.add(object.getString("price"));
  21. product_name_array.add(object.getString("name"));
  22. }
  23. listView.setAdapter(csadapter);
  24. } else {
  25. e.printStackTrace();
  26. }
  27. }
  28. });
  29. }
  30. // ...(省略其他部分)
  31. class adapter extends BaseAdapter {
  32. // ...(省略其他部分)
  33. @Override
  34. public View getView(final int position, View view, ViewGroup parent) {
  35. // ...(省略其他部分)
  36. submit.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View v) {
  39. // ...(省略其他部分)
  40. // 获取点击的项目名称
  41. gets = product_name_array.get(position);
  42. query.whereEqualTo("name", gets);
  43. query.findInBackground(new FindCallback<ParseObject>() {
  44. @Override
  45. public void done(List<ParseObject> objects, ParseException e) {
  46. if (e == null) {
  47. for (ParseObject obj : objects) {
  48. obj.deleteInBackground();
  49. }
  50. } else {
  51. // 处理异常情况
  52. }
  53. }
  54. });
  55. // 更新列表视图
  56. v.setVisibility(View.GONE);
  57. notifyDataSetChanged();
  58. int positionToRemove = (int) v.getTag();
  59. prices.remove(positionToRemove);
  60. Log.i("nice", String.valueOf(positionToRemove));
  61. }
  62. });
  63. return view;
  64. }
  65. }
  66. }

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

英文:

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:

  1. public class Notification_shop extends AppCompatActivity {
  2. ListView listView;
  3. String gets;
  4. ArrayList&lt;String&gt; prices,product_name_array;
  5. EditText shop_product_price, shop_product_offer,quantity ;
  6. TextView username, product_name, product_price;
  7. Button submit, cancel;
  8. ParseQuery&lt;ParseObject&gt; query;
  9. ParseObject user_notifications;
  10. adapter csadapter;
  11. int i;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate ( savedInstanceState );
  15. setContentView ( R.layout.activity_notification_shop );
  16. prices = new ArrayList&lt;&gt; ( );
  17. product_name_array = new ArrayList&lt;&gt; ( );
  18. listView = findViewById ( R.id.listview );
  19. csadapter = new adapter ( );
  20. query = new ParseQuery&lt;ParseObject&gt; ( &quot;Notification&quot; );
  21. query.findInBackground ( new FindCallback&lt;ParseObject&gt; ( ) {
  22. @Override
  23. public void done(List&lt;ParseObject&gt; objects, ParseException e) {
  24. if (e == null) {
  25. for (ParseObject object : objects) {
  26. prices.add ( object.getString ( &quot;price&quot; ) );
  27. product_name_array.add ( object.getString ( &quot;name&quot; ) );
  28. }
  29. listView.setAdapter ( csadapter );
  30. } else {
  31. e.printStackTrace ( );
  32. }
  33. }
  34. } );
  35. }
  36. class adapter extends BaseAdapter {
  37. @Override
  38. public int getCount() {
  39. return prices.size ( );
  40. }
  41. @Override
  42. public Object getItem(int position) {
  43. return null;
  44. }
  45. @Override
  46. public long getItemId(int position) {
  47. return 0;
  48. }
  49. @Override
  50. public View getView(final int position, View view, ViewGroup parent) {
  51. view = getLayoutInflater ( ).inflate ( R.layout.custom1_layout, null );
  52. shop_product_price = (EditText) view.findViewById ( R.id.shop_product_price );
  53. shop_product_offer = (EditText) view.findViewById ( R.id.shop_product_offer );
  54. quantity = (EditText) view.findViewById ( R.id.quantity );
  55. product_name = (TextView) view.findViewById ( R.id.name );
  56. product_price = (TextView) view.findViewById ( R.id.prices );
  57. submit = (Button) view.findViewById ( R.id.button3 );
  58. cancel = (Button) view.findViewById ( R.id.cancel );
  59. submit.setTag ( position );
  60. user_notifications = new ParseObject ( &quot;Users_Notifications&quot; );
  61. product_price.setText ( prices.get ( position ) );
  62. product_name.setText ( product_name_array.get ( position ) );
  63. submit.setOnClickListener ( new View.OnClickListener ( ) {
  64. @Override
  65. public void onClick(View v) {
  66. user_notifications.put ( &quot;Shop_price&quot;,shop_product_price.getText ().toString () );
  67. user_notifications.put ( &quot;shop_offer&quot;,shop_product_offer.getText ().toString () );
  68. user_notifications.put ( &quot;product_quantity&quot;,quantity.getText ().toString () );
  69. user_notifications.put ( &quot;parent&quot;,ParseUser.getCurrentUser () );
  70. user_notifications.saveInBackground ( new SaveCallback ( ) {
  71. @Override
  72. public void done(ParseException e) {
  73. if(e==null){
  74. Toast.makeText ( Notification_shop.this, &quot;details sent &quot;, Toast.LENGTH_SHORT ).show ( );
  75. }
  76. }
  77. } );
  78. gets = product_name_array.get( position );
  79. query.whereEqualTo ( &quot;name&quot;,gets );
  80. query.findInBackground ( new FindCallback&lt;ParseObject&gt; ( ) {
  81. @Override
  82. public void done(List&lt;ParseObject&gt; objects, ParseException e) {
  83. if(e==null ){
  84. for (ParseObject obj: objects){
  85. obj.deleteInBackground ();
  86. }
  87. }else {
  88. }
  89. }
  90. } );
  91. // Toast.makeText ( Notification_shop.this, &quot;button is clicked&quot;, Toast.LENGTH_SHORT ).show ( );
  92. v.setVisibility ( View.GONE );
  93. notifyDataSetChanged ();
  94. int positionToRemove = (int)v.getTag();
  95. prices.remove ( positionToRemove );
  96. Log.i ( &quot;nice&quot;,String.valueOf ( positionToRemove ) );
  97. }
  98. } );
  99. return view;
  100. }
  101. }

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

  1. public class Notification_shop extends AppCompatActivity {
  2. ListView listView;
  3. String gets, objectid, sat;
  4. ArrayList<String> prices, product_name_array, Notification_id;
  5. String[] stockArr;
  6. EditText shop_product_price, shop_product_offer, quantity;
  7. TextView username, product_name, product_price;
  8. Button submit, cancel;
  9. ParseQuery<ParseObject> query;
  10. ParseQuery<ParseUser> parseQuery;
  11. ParseObject user_notifications;
  12. adapter csadapter;
  13. int i, j;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_notification_shop);
  18. prices = new ArrayList<>();
  19. product_name_array = new ArrayList<>();
  20. Notification_id = new ArrayList<>();
  21. listView = findViewById(R.id.listview);
  22. csadapter = new adapter();
  23. user_notifications = new ParseObject("Users_Notifications");
  24. parseQuery = ParseUser.getQuery();
  25. parseQuery.selectKeys(Arrays.asList("list"));
  26. parseQuery.whereExists("code");
  27. parseQuery.whereEqualTo("cateogory", "Book");
  28. parseQuery.getInBackground(ParseUser.getCurrentUser().getObjectId(), new GetCallback<ParseUser>() {
  29. @Override
  30. public void done(ParseUser object, ParseException e) {
  31. if (e == null) {
  32. ArrayList<String> userTastesGot = (ArrayList<String>) object.get("list");
  33. stockArr = new String[userTastesGot.size()];
  34. stockArr = userTastesGot.toArray(stockArr);
  35. for (String s : stockArr) {
  36. sat = s;
  37. query = new ParseQuery<ParseObject>("Notification");
  38. query.whereContainedIn("objectId", Arrays.asList(sat));
  39. query.findInBackground(new FindCallback<ParseObject>() {
  40. @Override
  41. public void done(List<ParseObject> objects, ParseException e) {
  42. if (e == null) {
  43. for (ParseObject object : objects) {
  44. prices.add(object.getString("price"));
  45. product_name_array.add(object.getString("name"));
  46. gets = object.getObjectId();
  47. Notification_id.add(gets);
  48. Log.i("mess", gets);
  49. }
  50. listView.setAdapter(csadapter);
  51. } else {
  52. e.printStackTrace();
  53. }
  54. }
  55. });
  56. Log.i("ring", sat);
  57. }
  58. Log.i("User", "Retrieved " + userTastesGot);
  59. Log.i("name", ParseUser.getCurrentUser().getUsername());
  60. }
  61. }
  62. });
  63. }
  64. class adapter extends BaseAdapter {
  65. @Override
  66. public int getCount() {
  67. return product_name_array.size();
  68. }
  69. @Override
  70. public Object getItem(int position) {
  71. return null;
  72. }
  73. @Override
  74. public long getItemId(int position) {
  75. return 0;
  76. }
  77. @Override
  78. public View getView(final int position, View view, ViewGroup parent) {
  79. view = getLayoutInflater().inflate(R.layout.custom1_layout, null);
  80. shop_product_price = (EditText) view.findViewById(R.id.shop_product_price);
  81. shop_product_offer = (EditText) view.findViewById(R.id.shop_product_offer);
  82. quantity = (EditText) view.findViewById(R.id.quantity);
  83. product_name = (TextView) view.findViewById(R.id.name);
  84. product_price = (TextView) view.findViewById(R.id.prices);
  85. submit = (Button) view.findViewById(R.id.button3);
  86. cancel = (Button) view.findViewById(R.id.cancel1);
  87. submit.setTag(position);
  88. product_name.setText(product_name_array.get(position));
  89. product_price.setText(prices.get(position));
  90. submit.setOnClickListener(new View.OnClickListener() {
  91. @Override
  92. public void onClick(View v) {
  93. user_notifications.put("Shop_price", shop_product_price.getText().toString());
  94. user_notifications.put("shop_offer", shop_product_offer.getText().toString());
  95. user_notifications.put("product_quantity", quantity.getText().toString());
  96. user_notifications.put("ids", ParseUser.getCurrentUser().getObjectId());
  97. user_notifications.put("names", product_name_array.get(position));
  98. user_notifications.saveInBackground(new SaveCallback() {
  99. @Override
  100. public void done(ParseException e) {
  101. if (e == null) {
  102. Toast.makeText(Notification_shop.this, "details sent", Toast.LENGTH_SHORT).show();
  103. }
  104. }
  105. });
  106. // Rest of the code...
  107. }
  108. });
  109. return view;
  110. }
  111. }
  112. }
英文:
  1. public class Notification_shop extends AppCompatActivity {
  2. ListView listView;
  3. String gets,objectid,sat;
  4. ArrayList&lt;String&gt; prices,product_name_array,Notification_id;
  5. String[] stockArr;
  6. EditText shop_product_price, shop_product_offer,quantity ;
  7. TextView username, product_name, product_price;
  8. Button submit, cancel;
  9. ParseQuery&lt;ParseObject&gt; query;
  10. ParseQuery&lt;ParseUser&gt; parseQuery;
  11. ParseObject user_notifications;
  12. adapter csadapter;
  13. int i,j;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate ( savedInstanceState );
  17. setContentView ( R.layout.activity_notification_shop );
  18. prices = new ArrayList&lt;&gt; ( );
  19. product_name_array = new ArrayList&lt;&gt; ( );
  20. Notification_id = new ArrayList&lt;&gt; ( );
  21. listView = findViewById ( R.id.listview );
  22. csadapter = new adapter ( );
  23. user_notifications = new ParseObject ( &quot;Users_Notifications&quot; );
  24. parseQuery = ParseUser.getQuery ();
  25. parseQuery.selectKeys ( Arrays.asList ( &quot;list&quot; ) );
  26. parseQuery.whereExists ( &quot;code&quot; );
  27. parseQuery.whereEqualTo ( &quot;cateogory&quot;, &quot;Book&quot; );
  28. parseQuery.getInBackground ( ParseUser.getCurrentUser ( ).getObjectId ( ), new GetCallback&lt;ParseUser&gt; ( ) {
  29. @Override
  30. public void done(ParseUser object, ParseException e) {
  31. if(e==null){
  32. ArrayList&lt;String&gt; userTastesGot = (ArrayList&lt;String&gt;) object.get(&quot;list&quot;);
  33. stockArr = new String[userTastesGot.size()];
  34. stockArr = userTastesGot.toArray(stockArr);
  35. for( String s : stockArr) {
  36. sat = s;
  37. query = new ParseQuery&lt;ParseObject&gt; ( &quot;Notification&quot; );
  38. query.whereContainedIn ( &quot;objectId&quot;, Arrays.asList ( sat ) );
  39. query.findInBackground ( new FindCallback&lt;ParseObject&gt; ( ) {
  40. @Override
  41. public void done(List&lt;ParseObject&gt; objects, ParseException e) {
  42. if (e == null) {
  43. for (ParseObject object : objects)
  44. {
  45. prices.add ( object.getString ( &quot;price&quot; ) );
  46. product_name_array.add ( object.getString ( &quot;name&quot; ) );
  47. gets = object.getObjectId ( );
  48. Notification_id.add ( gets );
  49. Log.i ( &quot;mess&quot;, gets );
  50. }
  51. listView.setAdapter ( csadapter );
  52. } else {
  53. e.printStackTrace ( );
  54. }
  55. }
  56. } );
  57. Log.i ( &quot;ring&quot;, sat);
  58. }
  59. Log.i (&quot;User&quot;, &quot;Retrieved &quot; + userTastesGot);
  60. Log.i ( &quot;name&quot;, ParseUser.getCurrentUser ().getUsername () );
  61. }
  62. }
  63. } );
  64. }
  65. class adapter extends BaseAdapter {
  66. @Override
  67. public int getCount() {
  68. return product_name_array.size ();
  69. }
  70. @Override
  71. public Object getItem(int position) {
  72. return null;
  73. }
  74. @Override
  75. public long getItemId(int position) {
  76. return 0;
  77. }
  78. @Override
  79. public View getView(final int position, View view, ViewGroup parent) {
  80. view = getLayoutInflater ( ).inflate ( R.layout.custom1_layout, null );
  81. shop_product_price = (EditText) view.findViewById ( R.id.shop_product_price );
  82. shop_product_offer = (EditText) view.findViewById ( R.id.shop_product_offer );
  83. quantity = (EditText) view.findViewById ( R.id.quantity );
  84. product_name = (TextView) view.findViewById ( R.id.name );
  85. product_price = (TextView) view.findViewById ( R.id.prices );
  86. submit = (Button) view.findViewById ( R.id.button3 );
  87. cancel = (Button) view.findViewById ( R.id.cancel1 );
  88. submit.setTag ( position );
  89. product_name.setText ( product_name_array.get ( position ) );
  90. product_price.setText ( prices.get ( position ) );
  91. submit.setOnClickListener ( new View.OnClickListener ( ) {
  92. @Override
  93. public void onClick(View v) {
  94. user_notifications.put ( &quot;Shop_price&quot;,shop_product_price.getText ().toString () );
  95. user_notifications.put ( &quot;shop_offer&quot;,shop_product_offer.getText ().toString () );
  96. user_notifications.put ( &quot;product_quantity&quot;,quantity.getText ().toString () );
  97. user_notifications.put ( &quot;ids&quot;,ParseUser.getCurrentUser ().getObjectId () );
  98. user_notifications.put ( &quot;names&quot;,product_name_array.get ( position ) );
  99. user_notifications.saveInBackground ( new SaveCallback ( ) {
  100. @Override
  101. public void done(ParseException e) {
  102. if(e==null){
  103. Toast.makeText ( Notification_shop.this, &quot;details sent &quot;, Toast.LENGTH_SHORT ).show ( );
  104. }
  105. }
  106. } );
  107. /* gets = product_name_array.get( position );
  108. Log.i ( &quot;position&quot;,gets );
  109. query.whereEqualTo ( &quot;name&quot;,gets );
  110. query.findInBackground ( new FindCallback&lt;ParseObject&gt; ( ) {
  111. @Override
  112. public void done(List&lt;ParseObject&gt; objects, ParseException e) {
  113. if(e==null ){
  114. for (ParseObject obj: objects){
  115. obj.deleteInBackground ();
  116. }
  117. }else {
  118. }
  119. }
  120. } );*/
  121. // Toast.makeText ( Notification_shop.this, &quot;button is clicked&quot;, Toast.LENGTH_SHORT ).show ( );
  122. objectid = Notification_id.get ( position );
  123. for(String t : stockArr) {
  124. if (objectid.matches ( t )) {
  125. Log.i ( &quot;drive&quot;, &quot;well done&quot; );
  126. ParseUser.getCurrentUser ().getList ( &quot;list&quot; ).remove ( objectid );
  127. List newlist = ParseUser.getCurrentUser ().getList ( &quot;list&quot; );
  128. ParseUser.getCurrentUser ().remove ( &quot;list&quot; );
  129. ParseUser.getCurrentUser ().put ( &quot;list&quot;,newlist );
  130. ParseUser.getCurrentUser ().saveInBackground ();
  131. }
  132. }
  133. Log.i ( &quot;tea&quot;,objectid );
  134. v.setVisibility ( View.GONE );
  135. notifyDataSetChanged ();
  136. int positionToRemove = (int)v.getTag();
  137. product_name_array.remove ( positionToRemove );
  138. prices.remove ( positionToRemove );
  139. Log.i ( &quot;nice&quot;,String.valueOf ( positionToRemove ) );
  140. }
  141. } );
  142. return view;
  143. }
  144. }

}

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:

确定