I'm attempting to make an alert dialog remove an item in a ListView but I can't find a solution to how I do it.
Here is my code:
contactsListView.setLongClickable(true);
contactsListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(ContactsActivity.this);
builder.setTitle("Contact Removal");
builder.setMessage("Are you sure you want to remove this contact?");
builder.setCancelable(false);
builder.setPositiveButton("Yes, I'm sure!", new HandleAlertDialogListener());
builder.setNegativeButton("No, I've changed my mind", new HandleAlertDialogListener());
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
});
Here is my HandleAlertDialogListener()
private class HandleAlertDialogListener implements DialogInterface.OnClickListener {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}
the problem is that I can't refer to the position of the item I want removed.
Another question is what are the which values for the dialog's buttons?
Any help would be appreciated.
In your onItemLongClick() make your position parameter final , then create your alert dialog like this :
new AlertDialog.Builder(this).setTitle("Delete").setMessage("Review").setPositiveButton(R.string.positive_delete, new DialogInterface.OnClickListener() {
#Override
public void onClick(final DialogInterface dialog, final int id) {
// use position here.
}
}).setNegativeButton(R.string.negative_reask, new DialogInterface.OnClickListener() {
#Override
public void onClick(final DialogInterface dialog, final int id) {
dialog.dismiss();
}
}).create().show();
Hope this helps!
I guess you have an Adapter to show items in a ListView?
If so, you should delete an item like I said:
myDataList.remove(position);
myAdapter.notifyDataSetChanged();
Where position is an attribute in this method:
onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Related
I am new to android. I want to have a dialog box with positive and negative buttons when i click on a gridview item. Right now when i click on an item, it populates another activity with the information of the item that i clicked on.
For example, using putExtra and getExtras , populating the activity works great. Now all i want is that instead of the action being performed on gridView click, i want the action being performed on the ok button of the Alertdialog box.
How should i set the gridview.setOnCLickListener so that it pops up an alertDialog Box when an item is clicked??
gridView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
gridView.setClickable(true);
item= (Hotel) parent.getItemAtPosition(position);
String names = item.getName();
Intent i=new
Intent(HotelList.this,UserViewActivity.class);
i.putExtra("names",names);
startActivity(i);
}
});
Try out the following code:
#Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
item= (Hotel) parent.getItemAtPosition(position);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setCancelable(false);
dialog.setTitle("Your Title");
dialog.setMessage("Your Message" );
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
String names = item.getName();
Intent i= new Intent(HotelList.this,UserViewActivity.class);
i.putExtra("names",names);
startActivity(i);
alert.cancel();
}
})
.setNegativeButton("Cancel ", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
alert.cancel();
}
});
final AlertDialog alert = dialog.create();
alert.show();
}
});
Hope this helps.
First, remove gridView.setClickable(true). It's simply not necessary. Second, You can create the Dialog in your code:
AlertDialog ad = new AlertDialog.Builder(this).setContentView(R.layout.activity_dialog).show();
I want to delete items on a listView when longpressed. In this code you can add (with edittext) a list item with Button, as you can see below.
I have been unable to write the code for deleting them with a long press. What should I do?
Ideally, a long press will bring up a menu and user can touch and delete the section they want.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
addButton = (Button) findViewById(R.id.addButton);
listView = (ListView) findViewById(R.id.listView);
listItems = new ArrayList<String>();
listItems.add("First Item - added on Activity Create");
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
listItems.add(editText.getText().toString());
adapter.notifyDataSetChanged();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_LONG)
.show();
}
});
}
Try this one :
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long arg3) {
adapter.remove(data[position]);//position of the item you click
adapter.notifyDataSetChanged();
return false;
}
});
You can do it with animation:
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long arg3) {
adapter.remove(data[position]);//position of the item you click
adapter.notifyItemRemoved(position);
return false;
}
});
Use this for listview item delete on long click and it should be display alert dialog when you delete any item from list view
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
removeItemFromList(position);
return true;
}
private void removeItemFromList(int position) {
final int deletePosition = position;
AlertDialog.Builder alert = new AlertDialog.Builder(
this);
alert.setTitle("Delete");
alert.setMessage("Do you want delete this item?");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TOD O Auto-generated method stub
// main code on after clicking yes
songsList.remove(deletePosition);
songAdapter.notifyDataSetChanged();
songAdapter.notifyDataSetInvalidated();
}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alert.show();
}
I have created the following AlertDialog. In it, the user selects an item from a range of options stored in an XML file and handled using an Adapter, and then clicks either the positive button or the negative button. Here is the code:
public void OpenDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
dialog.setTitle("Promotion Options");
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(activity.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(com.zlaporta.chessgame.R.layout.promotion, null);
dialog.setView(v);
dialog.setPositiveButton("Choose", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == 1) {
System.out.println("ok");
}
}
});
dialog.setNegativeButton("Undo Move", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
Spinner spinner = (Spinner) v.findViewById(com.zlaporta.chessgame.R.id.promotionSpin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(activity, com.zlaporta.chessgame.R.array.option,
android.R.layout.simple_spinner_item);
//could be other options here instead of simple_spinner_dropdown_item. Just type simple and see what comes up next time
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Now, I'd like information about the user's selection in the spinner to be passed back to the enclosing Activity after the positive button is clicked. What's the best way to do this?
As you seem to have defined the AlertDialog in a separate class, you cannot directly access methods defined inside the Activity from where it is invoked.
One possible way is to define a public method inside the Activity as follows:
public void doSomething(Object spinnerDataObject){
....
}
and access it this way:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// call the Activity's method here and send the selected item.
((MainActivity)activity).doSomething(parent.getItemAtPosition(position));
dialog.dismiss();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
This will send the selected Spinner data object to the Activity. Whatever you want to do with that object can be done inside doSomething().
here is the code. When I long click on an item and dont drag my finger away from it, the menu still pops up, but it also activates my onClick listener. I have no idea why. I have tried looking for solutions online, and I only found something that told me to check the return statement. I am returning True, so I do not know what else to do.
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(AddClaim.this, "Clicked "+list.get(position), Toast.LENGTH_SHORT).show();
//adapter expenses
setContentView(R.layout.add_expense);
ListView expView = (ListView) findViewById(R.id.ExpenseListView);
Collection<Expense> expenses = list.get(position).getExpenses();
final ArrayList<Expense> expense = new ArrayList<Expense>(expenses);
final ArrayAdapter<Expense> expAdap = new ArrayAdapter<Expense>(AddClaim.this, android.R.layout.simple_list_item_1, expense);
expView.setAdapter(expAdap);
}
});
//LONG CLICK FUNCTIONS
listView.setOnItemLongClickListener(new OnItemLongClickListener(){
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
final int finalPosition = position;
PopupMenu popup = new PopupMenu(AddClaim.this, view);
popup.getMenuInflater().inflate(R.menu.add_claim, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
//DELETE button check.
if (item.getTitle().equals("Delete")){
AlertDialog.Builder adb = new AlertDialog.Builder(AddClaim.this);
adb.setMessage("Delete "+ list.get(finalPosition).toString()+"?");
adb.setCancelable(true);
adb.setPositiveButton("Delete",new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Claim claim = list.get(finalPosition);
ClaimListController.getClaimList().deleteClaim(claim);
}
});
adb.setNegativeButton("Cancel",new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
adb.show();
}//end of delete button check
//START of ADD EXPENSE check
if (item.getTitle().equals("Add Expense")){
Intent intent = new Intent(AddClaim.this, ExpenseAdd.class);
intent.putExtra("somename", finalPosition);
startActivity(intent);
}
//end of add expense check
return true;
}
});
popup.show();
return false;
}
});
}
Look carefully at your code,and count your braces but read my comments
listView.setOnItemLongClickListener(new OnItemLongClickListener(){ //function starts here
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) { // longclick starts here
final int finalPosition = position;
PopupMenu popup = new PopupMenu(AddClaim.this, view); // your menu code starts here
popup.getMenuInflater().inflate(R.menu.add_claim, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
//DELETE button check.
if (item.getTitle().equals("Delete")){
AlertDialog.Builder adb = new AlertDialog.Builder(AddClaim.this);
adb.setMessage("Delete "+ list.get(finalPosition).toString()+"?");
adb.setCancelable(true);
adb.setPositiveButton("Delete",new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Claim claim = list.get(finalPosition);
ClaimListController.getClaimList().deleteClaim(claim);
}
});
adb.setNegativeButton("Cancel",new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
adb.show();
}//end of delete button check
//START of ADD EXPENSE check
if (item.getTitle().equals("Add Expense")){
Intent intent = new Intent(AddClaim.this, ExpenseAdd.class);
intent.putExtra("somename", finalPosition);
startActivity(intent);
}
//end of add expense check
return true; // you only return true if onmenu item is clicked.. which is too late
}
});
popup.show(); // and your menu code ends here, because this is where it is shown..
return false; // you are returning false.. this is {star line}
} //longclick ends here
}); //function ends here
}
{start line} should return true
I want to delete an item from the ListView using an OnItemLongClickListener.
This is my code to make an AlertDialog appear when I OnLongClick an item.
I also need to now what code to use when I delete an Item
public class DeleteItem extends Activity {
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = (ListView) findViewById(R.id.listView);
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
AlertDialog.Builder alert = new AlertDialog.Builder(DeleteItem.this);
alert.setMessage("Are you sure you want to delete this?");
alert.setCancelable(false);
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Here I need the delete code
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return false;
}
});
}
}
ArrayAdapter has the method remove(int index):
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(final AdapterView<?> arg0, View arg1,
final int position, long arg3) {
AlertDialog.Builder alert = new AlertDialog.Builder(DeleteItem.this);
alert.setMessage("Are you sure you want to delete this?");
alert.setCancelable(false);
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ArrayAdapter yourArrayAdapter = (ArrayAdapter) arg0.getAdapter();
yourArrayAdapter.remove(position);
yourArrayAdapter.notifyDataSetChanged();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return false;
}
});
Adjust the cast with the generic type of your adapter. It works only if you provide a Collection of elements to the adapter. If you provided an Array it will throw an exception