如何从解析中删除一行并为当前用户更新自定义列表视图?
How to delete a row from parse and update custom listview for current user?
我正在为我的应用程序使用解析服务器。我创建了一个带有字段的 Parseobject“Notification”:
1.price 2.姓名
然后我有一个自定义列表视图,其中包含这些价格和名称的文本视图。该列表视图中还有按钮和一些 EDITTEXT,单击该按钮后,我将编辑文本保存在新的 Parseobject 中,并删除列表视图的该位置和包含该名称的行。之后我正在更新列表视图。
问题是所有用户的列表视图都发生了变化,我不知道如何为当前用户更新它。
这是我的代码:
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;
}
}
我试图到处寻找解决方案,但找不到合适的答案。现在已经 4 天了,我仍然停留在那个问题上。我真的需要帮助。提前致谢。
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;
}
}
}
我正在为我的应用程序使用解析服务器。我创建了一个带有字段的 Parseobject“Notification”: 1.price 2.姓名
然后我有一个自定义列表视图,其中包含这些价格和名称的文本视图。该列表视图中还有按钮和一些 EDITTEXT,单击该按钮后,我将编辑文本保存在新的 Parseobject 中,并删除列表视图的该位置和包含该名称的行。之后我正在更新列表视图。
问题是所有用户的列表视图都发生了变化,我不知道如何为当前用户更新它。
这是我的代码:
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;
}
}
我试图到处寻找解决方案,但找不到合适的答案。现在已经 4 天了,我仍然停留在那个问题上。我真的需要帮助。提前致谢。
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;
}
}
}