Getting Parse Row Wrong ObjectId - java

I have a listview that contains 4 objects in it.When I long press one of them ,a menu open with "Delete" option that I can delete the row.
However,I am getting the wrong row objetId for some reason and I can't figure out what is it I am doing wrong.
This is what I have done:
Creating context menu:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// Get the list
feedListView = (ListView)v;
// Get the list item position
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
int position = info.position;
position -= feedListView.getHeaderViewsCount();
int finalPosition = position;
if (ParseUser.getCurrentUser().getUsername().equals(data.getUserName())){
Log.e("Test","Same user + ObjectId = " + ((AdapterView.AdapterContextMenuInfo) menuInfo).position);
if (v.getId()==R.id.feedListView) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
}
}else{
Log.e("Test","Not Your Post");
Toast.makeText(this, "Its not your post", Toast.LENGTH_SHORT).show();
}
}
Choosing from context menu:
if (item.getItemId() == R.id.deleteMenu){
new AlertDialog.Builder(FeedActivity.this)
.setTitle("Are you sure ?")
.setMessage("Are you sure you want to delete this post?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Feed");
objects.get(finalPosition).deleteInBackground(new DeleteCallback() {
#Override
public void done(ParseException e) {
Log.e("Done","Item Deleted Successfully !!!");
arrayList.remove(finalPosition);
adapter.notifyDataSetChanged();
}
});
}
});
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
Thanks

So I fix it by adding these 2 lines:
The first:
MyData Editdata = (MyData) adapter.getItem(position);
To
MyData Editdata = (MyData) adapter.getItem(finalPosition);
The second line I added is(I added it because my listview display the same way):
query.orderByDescending("updatedAt");

Related

Change ListView choiceMode from singleChoice to multipleChoiceModal on button click when using ArrayAdapters

Actually, I want to achieve something like the functionality we mostly see in our cell phones for chats/msgs deletion. When a delete button is clicked on the menu, checkboxes must appear for row selection along with the label on top displaying the no. of selected rows. When "Done/Delete" button on the top right side is clicked after selecting the no. of rows, a delete confirmation dialog box must appear and selected rows must be deleted. I have highlighted the required functionality in images below. [image 1-button in menu ][1][Image 2- multi-mode appearance on clicking the delete button][2][image 3- selection and label][3][image 4- confirmation dialog box][4].This is what I coded so far but my code ain't working, need help. here's my code:
//for the start I want the list be in single mode
ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter arrList = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, namesList);
listView.setAdapter(arrList);
// for the list to be in multimode on clicking the button in the menu
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.mymenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.deletebutton) {
if (namesList.size() != 0) {
if (mListView.getChoiceMode() == ListView.CHOICE_MODE_SINGLE) {
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
directoryList = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
namesList);
delete();
}
} else {
Toast.makeText(getApplicationContext(), "names not found",
Toast.LENGTH_SHORT).show();
}
}
return super.onOptionsItemSelected(item);
}
// delete method implementation
AlertDialog.Builder a_builder = new AlertDialog.Builder(this);
a_builder.setTitle("Confirm Delete?");
a_builder.setMessage("DELETE")
.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
int i;
SparseBooleanArray checkedItemPositions = mListView.getCheckedItemPositions();
int itemCount = listView.getCount();
for (i = itemCount - 1; i >= 0; i--) {
if (checkedItemPositions.get(i)) {
arrList.remove(namesList.get(i));
}
}
checkedItemPositions.clear();
arrList.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
[1]: https://i.stack.imgur.com/ZR1Wv.png
[2]: https://i.stack.imgur.com/4gJGd.png
[3]: https://i.stack.imgur.com/uvn9p.png
[4]: https://i.stack.imgur.com/3gsML.png

Recyclerview sqllite update only works on first item on the list

I have a list of items that are displayed in a recylcerview in my adapter class i implmented a onlongclick method, when used it opens up a new alertdialog, when i press on the selected item in the alterdialog im calling updateList() function that is supposed to update the selected item the problem is this only works for the first item displayed in my list, so in exmaple if i pressed the 10th item on the list said item wont update.
Update function
public boolean updateList(long rowId, String stanje) {
ContentValues newValues = new ContentValues();
newValues.put(TABELA_STOLPEC_STANJE,stanje);
return db.update(TABELA_IME,newValues,TABELA_STOLPEC_ID+"="+rowId,null)>0;
}
OnlongClickListener this method is in my fragment class.
mmAdapter.setOnLongClick(new ToDoRecyclerViewAdapter.OnLongClickListener_() {
#Override
public void onLongClick(long i,String item) {
if(item.equals("doing")) {
boolean update_1 = db.updateList(i, item);
if(update_1) {
android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(TodoFragment.this).attach(TodoFragment.this).commit();
Toast.makeText(getContext(), "Dodano v bazo.!", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getContext(), "Prislo je do napake!", Toast.LENGTH_SHORT).show();
}
}
}
});
onlongclicklistener implementation in my adapter class
holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(final View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(v.getContext());
CharSequence meni[] = new CharSequence[] {"DOING"};
adb.setItems(meni, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if(i == 0) {
mValues.get(i).setStanje("doing");
clickLinster_.onLongClick(mValues.get(position).getId_(),mValues.get(position).getStanje());
}
}
});
AlertDialog alertDialog = adb.create();
alertDialog.setCancelable(true);
alertDialog.show();
return true;
}
});
public interface OnLongClickListener_ {
void onLongClick(long i, String item);
}
The reason is simple, you are updating wrong row with index, correct solution:
#Override
public void onBindViewHolder(#NonNull final CustomViewHolder holder, int position) {
//Rest of onBindViewHolder code
holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(final View v) {
final int pos = holder.getAdapterPosition();
AlertDialog.Builder adb = new AlertDialog.Builder(v.getContext());
CharSequence meni[] = new CharSequence[] {"DOING"};
adb.setItems(meni, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if(i == 0) {
mValues.get(pos).setStanje("doing");
clickLinster_.onLongClick(mValues.get(pos).getId_(),mValues.get(pos).getStanje());
}
}
});
AlertDialog alertDialog = adb.create();
alertDialog.setCancelable(true);
alertDialog.show();
return true;
}
});
}

Android studio Using Split method to able to delete the todo list from the data base

I'm using the onItemLongClickListener method to create a delete event when long click on the list item. I have the delete task method inside the DAO class. How can I implement it inside the LongClick method?
TaskList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(final AdapterView<?> adapterView, final View view, final int position, final long l) {
//Get the selected item position
selectedItem = adapterView.getItemAtPosition(position).toString();
//Create a dialog box to inform the delete
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Delete");
//Display the seletced item name
builder.setMessage("Do you want to remove " + selectedItem + "?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String[] list = selectedItem.split(":");
//Missing something here that I'm not sure
if (dao.deleteTask(id)) {
Toast.makeText(MainActivity.this, "Deleted" + task[position], Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.show();
return true;
}
});
public boolean deleteTask(int id) {
//Get a reference to the database
SQLiteDatabase db = this.getReadableDatabase();
//Excute a sql query and get a results in Cursor object "res"
db.execSQL("Delete from tasks where id = " + id);
return true;
}
store your id on a separate list e.g.
int idList[] = {1, 2, 3, 4, 5};
TaskList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(final AdapterView<?> adapterView, final View view, final int position, final long l) {
//Create a dialog box to inform the delete
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Delete");
//Display the seletced item name
builder.setMessage("Do you want to remove " + selectedItem + "?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (dao.deleteTask(idList[position])) {
}
}
});
builder.show();
return true;
}
});

Remove an item from a ListView using an alert dialog

I have this ListView containing items and I want to create an alert dialog that removes any of these items when I long click on an item. onLongClick on an item shows an AlertDialog and if I click yes, it removes the item.
Here is my code.
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Are You Sure You Want to Delete This Note?!")
.setTitle("Attempt to Delete A Note")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
notesList.remove(i);
arrayAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "ooooooh No!!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "Good Choice", Toast.LENGTH_SHORT).show();
}
})
.show();
return true;
}
});
I think the problem is 'i' position of alert dialog click listner and u need to user list item clicked position in order to delete item from list.
Please use below code :
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Are You Sure You Want to Delete This Note?!")
.setTitle("Attempt to Delete A Note")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
if(notesList!=null){
notesList.remove(position);
arrayAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "ooooooh No!!", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
e.printStackTrace();
}
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "Good Choice", Toast.LENGTH_SHORT).show();
}
})
.show();
return true;
}
});
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Are You Sure You Want to Delete This Note?!")
.setTitle("Attempt to Delete A Note")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
try {
dataModels.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "ooooooh No!!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "Good Choice", Toast.LENGTH_SHORT).show();
}
})
.show();
}
});
This is works for me...try this

Confirmation before deleting from an ArrayList (Android)

I've set up OnContextItemSelected to show an alert when the user tries to delete an item and asks them for confirmation. The problem being that the "confirmationReceived" variable is getting set to true the first time the code is executed with nothing else happening, then when it's executed a second time the item is deleted before the user has a chance to confirm that they want it deleted.
onCreateContextMenu
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Timetable Item");
menu.add(0, Remove_Item, Menu.NONE, R.string.Remove_Item);
}
onContextItemSelected
#Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
AlertDialog diaBox = AskRemoveConfirm();
diaBox.show();
switch (item.getItemId()) {
case (Remove_Item): {
if(confirmationReceived == true) {
AdapterView.AdapterContextMenuInfo menuInfo;
menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int index = menuInfo.position;
removeItem(index);
confirmationReceived = false;
return true;
}
}
}
return false;
}
AskRemoveConfirm()
private AlertDialog AskRemoveConfirm()
{
AlertDialog myRemovalDialogBox =new AlertDialog.Builder(this)
.setTitle("Confirmation")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
confirmationReceived = true;
dialog.dismiss();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
return myRemovalDialogBox;
}
do somthing like this
private AlertDialog AskRemoveConfirm()
{
AlertDialog myRemovalDialogBox =new AlertDialog.Builder(this)
.setTitle("Confirmation")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
confirmationReceived = true;
if(confirmationReceived == true) {
AdapterView.AdapterContextMenuInfo menuInfo;
menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int index = menuInfo.position;
removeItem(index);
confirmationReceived = false;
return true;
}
dialog.dismiss();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
return myRemovalDialogBox;
}

Categories

Resources