I have a ListView showing the names of different profiles, when the user holds the click on an item the activity shows an alertDialog, if the user press the confirm button I want to remove the element from my listView, from my ArrayAdapter and from my ArrayList.
I know that arg2 in the onItemLongClick method represent the index of the selected item but I want to be able to access it inside the onClick method of the positive button.
Any advice?
My ArrayList is called "ListaUtentiStringa", the ArrayAdapter is "profilesAdapter" and the listView is called listview.
Sorry for my bad english.
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle("Vuoi davvero cancellare il profilo?");
builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// How to remove the selected item?
}
});
builder.setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = builder.create();
alert.show();
profilesAdapter.notifyDataSetChanged();
return true;
}
});
Do this:
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle("Vuoi davvero cancellare il profilo?");
builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// How to remove the selected item?
adapter.remove(adapter.getItem(position));
}
});
Try this..
Use ListaUtentiStringa ArrayList and profilesAdapter adapter as Global variable.
builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// How to remove the selected item?
ListaUtentiStringa.remove(arg2);
profilesAdapter.notifyDataSetChanged();
dialog.dismiss();
}
});
EDIT
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
Related
I have created a database which is displayed in a ListView using an SimpleAdapter. Now, I want to show a layout which contain (edit & delete ), so when user longClick on an item of the list it will get a dialog to choose if its want to delete or to edit on this item. Now, I'm focusing on delete statement and
I have tried with this code, but it does not work for me, the program always force close when I use this code.
Thanks
#Override
protected void onCreate(Bundle savedInstanceStates){
super.onCreate(savedInstanceStates);
setContentView(R.layout.activity_user_lstview);
userDataListView = (ListView) findViewById(R.id.ListViewUser);
List<Map<String, String>> data = new ArrayList<Map<String, String>>(
String[] from = {"tvId", "tvUsr", "tvPass", "tvDept","tvAuth"};
int[] to = {R.id.tvId, R.id.tvUsr, R.id.tvPass, R.id.tvDept, R.id.tvAuth};
adapter = new SimpleAdapter(UserListView.this, data, R.layout.userlisttemplate, from, to);
userDataListView.setAdapter(adapter);
userDataListView.setLongClickable(true);
userDataListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),"Clicked item : " +adapter.getItem(position), Toast.LENGTH_LONG).show();
}
});
userDataListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
final int which_item = position;
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(UserListView.this);
dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
dialogBuilder.setTitle("Confirmation");
dialogBuilder.setMessage("Choose an option edit or delete data ?");
dialogBuilder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int which) {
// Edit
dialogInterface.dismiss();
}
});
dialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int which) {
// Delete
data.remove(which_item);
adapter.notifyDataSetChanged();
Toast.makeText(UserListView.this, "Data has been removed", Toast.LENGTH_SHORT).show();
dialogInterface.dismiss();
}
});
dialogBuilder.create().show();
return true;
}
});
}
I have a custom listview , what im trying to do is when a user select a specific value in lisview it will goto editText inside a dialog in the same activity.. but it wont get the value.. here's my code
public void savedNotes(){
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, final int position,
long arg3) {
LayoutInflater li = LayoutInflater.from(context);
View promt = li.inflate(R.layout.prompt_saved_notes,null);
AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(context);
alerDialogBuilder.setView(promt);
final EditText textfield1 = (EditText) promt.findViewById(R.id.edt_textfield);
alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
String itemString = list.getItemAtPosition(position).toString();
textfield1.setText(itemString);
}
}).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alerDialogBuilder.create();
alertDialog.show();
}
});
}
Try this..
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, final int position,
long arg3) {
LayoutInflater li = LayoutInflater.from(context);
View promt = li.inflate(R.layout.prompt_saved_notes,null);
AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(context);
alerDialogBuilder.setView(promt);
final EditText textfield1 = (EditText) promt.findViewById(R.id.edt_textfield);
String itemString = list.getItemAtPosition(position).toString();
textfield1.setText(itemString);
alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
}
}).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alerDialogBuilder.create();
alertDialog.show();
}
});
Hope this will help you.
You can't get the value from a custom listview with
String itemString = list.getItemAtPosition(position).toString();
Instead you might have used an array or an arraylist to store the data that you are setting inside the listview. Inside the onItemCLick() type
//For arraylist
String itemString = your_arrayList.get(position);
//For array
String itemString = your_array[position];
By your code, this "itemString" will appear in edittext only when you click/tap the "Saved" button in the dialog.
You are setting value on EditText after clicking the positive("saved") button of dialog, but when your dialog is already visible EditText is not having any value.
First you need to remove the code from here:-
alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
//Remove these two line code from here
String itemString = list.getItemAtPosition(position).toString();
textfield1.setText(itemString);
}
}
Now add these two line just after finding the reference of EditText in alert dialog like :-
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View arg1, final int position,
long arg3) {
LayoutInflater li = LayoutInflater.from(context);
View promt = li.inflate(R.layout.prompt_saved_notes,null);
AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(context);
alerDialogBuilder.setView(promt);
final EditText textfield1 = (EditText) promt.findViewById(R.id.edt_textfield);
String itemString = list.getItemAtPosition(position).toString();
textfield1.setText(itemString);
alerDialogBuilder.setCancelable(false).setPositiveButton("saved",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
}
}).setNegativeButton("cancel",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
dialog.cancel();
}
});
AlertDialog alertDialog = alerDialogBuilder.create();
alertDialog.show();
}
});
}
I have OnItemClickListener in my activity for list view in which i start another activity,and i also have OnItemLongClickListener in which basically i want to delete the long clicked row.When i click on list view OnItemClickListener works fine,but when when i long click on list still both OnItemClickListener and OnItemLongClickListener.And i don't want OnItemClickListener to work when i click long.
This is how my code looks like.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, CompleteInformationActivity.class);
intent.putExtra("position", "" + selected.get(position));
startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Long click happened.", Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Do you want to delete?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Item is Deleted.", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "Item is not Deleted.", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
return false;
}
});
I think you should return true in your onItemLongClick method to consume the click. Check Android documentation http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html
I am using onlongitemclick and can produce a dialog that comes up to confirm a delete, but I cannot get the listitem position or text.
Edit: I cannot get the selectedValue string value inside of the public void onClick(DialogInterface dialog, int which) function.
lv is my listview object
lv.setOnItemLongClickListener(new OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
ListView list1 = (ListView) findViewById(android.R.id.list);
final String selectedValue = (String) list1.getItemAtPosition(arg2);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(RecipeList.this);
alertDialog.setTitle("Delete");
alertDialog.setMessage(selectedValue);
alertDialog.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
obj Recipe = new obj(selectedValue, RecipeList.this);
Recipe.remove(<I need the listview item to create the object and then delete some listing in the DB, seletecValue should do this, but it does not>)
Intent intent2 = new Intent(RecipeList.this, RecipeList.class); //go to recipe list
startActivity(intent2);
} });
alertDialog.setPositiveButton("Keep", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// alertDialog.dismiss();
} });
alertDialog.show();
return true;
}
});
arg1 is your view, you should be able to get the text from the view.
arg2 is the position.
See: http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html
I have a listView and a onClickListener that onClick do som things and it works. Now I want to display an AlertDialog onLongClick but nothing happens. I've added android:longClickable="true" to the ListView in the manifest too but still, nothing happens.
listView.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
final CharSequence[] items = {"Revansch!", "Lägg till som vän","Ta bort spelet"};
AlertDialog.Builder builder = new AlertDialog.Builder(ChallengeList.this);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
});
Anyone know why it's not working?
Thanks in advance!
Try this,
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, final long id) {
final CharSequence[] items = {"Revansch!", "Lägg till som vän","Ta bort spelet"};
AlertDialog.Builder builder = new AlertDialog.Builder(ChallengeList.this);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
});
I've tried this. Its working.
myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
return onLongListItemClick(v,pos,id);
}
protected boolean onLongListItemClick(View v, final int pos, long id) {
// write code for AlertDialog
return true;
}
});
You need to override onItemLongClick method also.