So, i'm storing my datas in an IO-file! My datas are displayed and i want to delete an item from the listview, i maked this code, and i'm stucking!
L.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
AlertDialog alert_reset;
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage("Supprimer cette donnée ?")
.setCancelable(false)
.setPositiveButton("Oui",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int id) {
.............
updatelv(activity);
}
})
.setNegativeButton("Non",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
alert_reset = builder.create();
alert_reset.show();
return true;
}
Have i to use List.remove(arg2)?
And for deleting the data from a file, how can i do this ?
Thank you.
To remove an item from a ListView (which is just a display of some data) you need to remove the item from the data that backs the ListAdapter.
A common example is an Adapter that contains a list. To remove an item from the list and update the ListView you would do something like this.
myList.remove(arg2); // remove the item
myAdapter.notifyDataSetChanged(); // let the adapter know to update
IMHO the easiest way is to start by deleting the entry in the file, and then restart the "buildList" process. As the old entry is no more in the file, the new list won't show it any more.
About deleting in the file, it's more a java based question than Android, and it depends also on the store format you use (xml, json, custom ?). You should consider using a database, which is more flexible and easy to update.
Related
I pass data from a ListView to another ListView with SharedPreference. I can write items and can remove an item of them.
After I delete item,I restart this page and I see item which I deleted. I cannot remove it permanently.
MainActivity
final DataProvider[] providers = gson.fromJson(jsonurun, DataProvider[].class);
final List<DataProvider> list = new ArrayList<>(Arrays.asList(providers));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
final DataProvider dataProvider = (DataProvider) adapterView.getItemAtPosition(i);
AlertDialog.Builder builder = new AlertDialog.Builder(Listele1.this);
builder.setMessage("Silinsin mi ?")
.setCancelable(false)
.setPositiveButton("EVET", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
list.remove(dataProvider);
prefAdapter.notifyDataSetChanged();
}
})
try change
prefAdapter.notifyDataSetChanged();
for notifyItemRemoved("here, position you has deleted");
notifyItemRangeChanged("here, position you has deleted" ,"your array".size());
When you are passing data form ListView 1 to ListView 2 using SharedPreference.
using below 2 lines.
final DataProvider[] providers = gson.fromJson(jsonurun, DataProvider[].class);
final List<DataProvider> list = new ArrayList<>(Arrays.asList(providers));
You only remove data from Listview 2 ie: list in your
case : list.remove(dataProvider);
And forgot to delete the data from SharedPreference at same time.That's why when restart the page your deleted item is visible again.
In short try to remove data form SharedPreference as well after performing
list.remove(dataProvider);
prefAdapter.notifyDataSetChanged();
So right now I have a custom listview adapter that adds another row when the user selects a item. The thing is, each item in the row should have a modification button where they can choose to add whatever modification it is (can choose more than one modification)
This is a food ordering app that when the item is selected, there should be another button in the list labeled "Modify", where a pop-up comes up and allows the user to choose what modification it wants by using checkbox. ("Less salt", "More sauce", etc). Each modification list is the same for each dish. When the user exits the popup and clicks on the same modify button, the checkboxes checked should stay there.
I originally created a Popup class where when the button is selected, there is an intent to jump to that Popup activity, but I couldn't find the relationship between the custom adapter and the Popup activity. I also tried using an AlertDialog to replace the Popup window, but could not find a way to save all the checked items and show which ones were selected before.
Here's my code
modifyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Modification");
final CharSequence[] modify_items = orderClass.getModifyList()
.toArray(new CharSequence[orderClass.getModifyList().size()]);
builder.setMultiChoiceItems(modify_items, null, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
if(isChecked){
selectedList.add(indexSelected);
selectedItems.set(position, selectedList);
}
else if(selectedList.contains(indexSelected)){
selectedList.remove(Integer.valueOf(indexSelected));
selectedItems.set(position, selectedList);
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
Log.d("dialog", "Showing dialog");
}
});
return view;
}
You need to put data in a holder structure(its kinda a class that holds variables)
then you can show data by index and theres is a relation between the list view index shown and the data index so when is a list view cell clicked you get the position then find it in the structure and post it to another activity or anything else to modify it remember you have to declare a flag to launch the activity else the app will crash i think its called activtyoutofbound index or something like this
I am making a ToDo List and have troubles with deleting an item from ListView.
If the User has done one thing on his list, he can click on that item and it will be either striked through, or the strike trhough will be undone:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view;
int i_strikethrough = tv.getPaintFlags();
if(i_strikethrough == 1297){
tv.setPaintFlags(tv.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
} else if (i_strikethrough == 1281){
tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
}
});
If the user makes a long click, a message will pop up and he can choose to delete this item:
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Delete");
alertDialogBuilder.setMessage("Are you sure you want to delete?");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
adapterInhalt.remove(adapterInhalt.getItem(position));
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
dialogInterface.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
return true;
}
});
Now, my problem is the following:
Imagine the second Item is striked through but the third is not.
If i delete the second Item, then the third Item is at second place and is striked through. But it should not be striked through.
I hope that you understand my problem and that you can help me solving this issue.
I hope this picture will help you understanding the issue:
A helping Picture
You aren't implementing a list view properly. List views recycle views. This means they reuse the same views and put different positions in your list into them. This provides very efficient UI code. It also means that if you make any changes to the view outside of getView of your adapter that those changes will be applied to the wrong item when you remove or scroll.
The write way to make a listview is that if you want to update the UI of any position, you change the model of that position. THen you tell the adapter that it needs to update by calling notifyDataSetChanged(). The getView function will then get called to redraw each visible element and should apply the strike through.
You should define a class for your items, that has a boolean field for strike status. for example:
public class MyItem{
String name;
boolean isStriked;
}
then you can check if an item is striked through on adapter's getView() method. you can increase the cohesion in your code this way. BTW I recommend using RecyclerView as it has predefined methods and animations for item deletion
This question already has an answer here:
Unable to modify ArrayAdapter in ListView: UnsupportedOperationException
(1 answer)
Closed 9 years ago.
I want to remove data from a ListView. For that on a long press event I've used the code below:
lstGame.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
AlertDialog.Builder builder = new AlertDialog.Builder(FavouriteActivity.this);
builder.setMessage("Remove from Favourite?").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Const.favourite(FavouriteActivity.this, (args[arg2]));
Toast.makeText(FavouriteActivity.this, "Selected Item Removed from Favourite.", Toast.LENGTH_LONG).show();
// Here I get the UnsupportedException---->
// adapter.remove(args[arg2]);
lstGame.setAdapter(adapter);
lstGame.invalidate();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
Dialog alert = builder.create();
alert.show();
return false;
}
});
Why do I get that exception?
If the adapter reference points to a default ArrayAdapter instance then you most likely instantiate the ArrayAdapter using an array of objects as its source of data. If this is the case then, under the hood, the ArrayAdapter will transform that array into a special ArrayList(not the normal java one). This special ArrayList doesn't implement the methods that change its size(so using methods like add or remove(which modify that list) on the ArrayAdapter will throw the UnsupportedOperationException), it will only allow you to modify the values in it.
If you want to use that remove method then put the data from the array that you currently use in the ArrayAdapter in an ArrayList and then pass that list to the ArrayAdapter constructor.
UPDATE
The original question i asked was about my long id value but because you guys were right in the way u said i had the correct id i removed my error. Thanks for the help. read my answer for more detail.
1) My app uses the local android SQLiteDatabase and has three tables. I have no problems for two of the tables but turns out my third one is presenting some issues because of my column declarations are public static final string COLUMN_NAME = "name"; ,etc.
My Activities are not extending the ListActivity so that I can have custom lists and listviews for each activity.
I am getting my listview by listview = (ListView) findViewById(R.id.myList); and adding a listener to the listview by listview.setOnItemClickListener(ListListener); Then here is my method for the list listener:
OnItemClickListener ListListener = new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View v, int position,
final long id)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(ExerciseList.this)
.setIcon(R.drawable.edit)
.setTitle("Update Selected Exercise")
.setMessage("Would you like to update the current Exercise? Click continue to proceed.")
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
final Intent i = new Intent(getBaseContext(), AddExercise.class);
i.putExtra(ExerciseDbAdapter.KEY_ROW_ID, id);
startActivityForResult(i, EDIT_EXERCISE);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
}
};
This above method is just a working on list item click listener!
Intent.putExtra("Key",value) is right way to put the data in intent so
i.putExtra("INSERT THE KEY HERE",ExerciseDbAdapter.KEY_ROW_ID, id);
Okay guys so i found the issue with my application and you were all right. I was getting the correct row id from the application.
I however was passing another data member through my intent causing the setRowIdFromIntent() method to change the id from null to 0. or from not null to 0.
Basically no matter what the value i was passing it was being set to 0 from my setRowIdFromIntent() method because of the data member i passed through. Therefore the above code is almost irrelevant to my problem.
So if you want a working on click list listener the one above will definitely help you pass the correct id to your new activity. Sorry again for this confusion I had on my side. Thanks again for all other postings!