英文:
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<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.
答案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<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;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论