How to remove item from adapter - android

I have a container, adapter, and couple items. I would like to save items into database one by one.
However, I have issue on getting next item from adapter and resize adapter.
So far I have the code below:
adapter.add(item[0]);
adapter.add(item[1]);
adapter.add(item[2]);
item = adaper.getItem(0);
item.setDismissListener(new Item.OnDimissedListener(){
#Override
public void save(){
1. save item to database, it works here
2. get next item, I do
item = adapter.removeFirst(); // this one returns null pointer exception.
}
}
container.setAdapter(adapter); // set adapter here
Do you guys have any idea?

I had a similar project where I stored strings in a SQLite database and users could add/delete/modify items. What I did was to temporarily store the list of items in an ArrayList<String> and then update the ListView adapter with adapter.notifyDataSetChanged(); when that ArrayList changes.
In your case, I'd try something like this:
// Create an ArrayList <String>
ArrayList<String> itemsArray = new ArrayList<String>();
itemsArray.add( YOUR_ITEMS_GO_HERE );
...
// add data in ArrayAdapter
adapter = new ArrayAdapter<String>(this, R.layout.list, itemsArray);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
// Perform your Save actions
// Delete the first item in the ArrayList
list.remove(0); // removes the first item
// Update the ListView
adapter.notifyDataSetChanged();
If you are not using Strings, replace <String> with your <Object Type>

Related

Refresh listview data from custom ArrayAdapter which is populated by SQLite Database

I have a custom ArrayAdapter which is set to a listview with data from database:
CustomAdapter adapter = new CustomAdapter(myview.getContext(), -1, database.getAll());
listview.setAdapter(adapter);
Now, I want when the user deletes a database item the listview should refresh.
Until now, I can successfully delete the item from the database, but it remains in the listview. In the ArrayAdapter class I call refresh() method from a new class same to adapter's parent class
public void refresh(){
ParentClass class_par = new ParentClass();
class_par.refreshfromParent();
}
And in the ParentClass I have a new method: refreshfromParent()
public void refreshfromAlarm(){
DatabaseHandler database = new DatabaseHandler(myview.getContext());
listview.setAdapter(new CustomAdapter(myview.getContext(), -1, database.getAll())); // here I change listview adapter, and populate it with new database data, but I get NullPointerException
}
So, how can I refresh listview items? Please, DO NOT mention notifyDataSetChanged(), it DOES NOT work.
here you making en error
CustomAdapter adapter = new CustomAdapter(myview.getContext(), -1, database.getAll());
listview.setAdapter(adapter);
//so first you should make an arrayList and define it globally like
// ArrayList <yourType>list = new ArrayList<>();
//then initialize your adapter like this
list.addAll(databse.getAll());
CustomAdapter adapter = new CustomAdapter(myview.getContext(), -1, list);
listview.setAdapter(adapter);
// now when you want to delete an item
// delete it from your list like list.remove(location)
// and also delete same item from your datebase
// after that just call this
notifyDataSetChanged();
I realized that notifyDataSetChanged is a big trouble, so I decided to close and reload my fragment which contains my listview. Thus, the result looks the same with DataSetChanged and there is no difference in the users' eyes. I suggest everyone who reaches in this question do the same.

updating Listview when sqlite updated

Hi guys is it possible that everytime I add an item in the database it will notify and update the listview? For example I have two fragments in one activity then When I send a data in the database from the Fragment A the Listview in Fragment B will update also without using interface.
There is a function of ArrayAdapter called - notifyDataSetChanged()
//getting the list view
ListView userListView = (ListView) findViewById(R.id.users_ListView);
//creating an array to represnt what ever you want
ArrayList<String> users = new ArrayList<>();
for (int i = 0; i < 10 ; i++) {
//populate the array
String s = "user " + i;
users.add(s);
}
//setting up adapter to the array (this is 1 row with 3 arguments)
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,
users);
//connecting the list view to get the data to show from the adapter
userListView.setAdapter(adapter);
//changing the arrayList by adding or subtracting
String s = "another user";
users.add(s);
//update the adapter and list view with this command
adapter.notifyDataSetChanged();
Share your code if you are having any trouble.
good luck =)

How I can populate a ListView with a Cursor?

Is it possible to load all items in the adapter at once?
I am using a CursorLoader, a ListView and a Cursor adapter, the items in the the adapter are loaded with scrolling.
I know I have only few simple items (about 10) so I do not have performance problems
Thanks
Read this Populating-a-ListView-with-a-CursorAdapter or this Android Cursor Example
If you want to use an ArrayList (getted from Populating a ListView using an ArrayList):
lv = (ListView) findViewById(R.id.your_list_view_id);
// Instanciating an array list (you don't need to do this,
// you already have yours).
List<String> your_array_list = new ArrayList<String>();
your_array_list.add("foo");
your_array_list.add("bar");
// This is the array adapter, it takes the context of the activity as a
// first parameter, the type of list view as a second parameter and your
// array as a third parameter.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
your_array_list );
lv.setAdapter(arrayAdapter);

Programatically replace String Array in a ListView Array Adapter

I have a ListView that i have populated with a string array: {"1","2","3"} using an Array Adapter.
(ListView) ListOptions = (ListView)findViewById(R.id.ListOptions);
String[] exampleString = new String[]{"1","2","3"};
ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,exampleString);
ListOptions.setAdapter(adapter);
Later on, i want to be able to change exampleString to exampleString2:
{"4","5","6"}
and have the ListView update with exampleString2 displayed as the list options.
is there something i can do along the lines of:
adapter.ChangeStringArray(exampleString2);
ListView.setAdapter(adapter);
I want to avoid having to create a new adapter each time I change the String array that is populating the list items.
ArrayAdapter has methods for managing the backing data array.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, new ArrayList<String>());
adapter.addAll(exampleString);
adapter.clear();
adapter.addAll(exampleString2);
adapter.notifyDataSetChanged();

ANDROID: Listview wont update after deleting database data

New to Android and I'm having an issue with updating my a ListView after deleting entries from a database.
In my activity I have a ListView with multiple choice checkboxes in each row. After the user presses a delete button, deleteName() gets called. deleteName() will then delete any Names that were checked in the ListView. The Names get deleted from the database, but the ListView never update.
I tried using adapter.notifyDataSetChanged() but nothing happens. Why wont my ListView update?
public void deleteName() {
NamesDataSource datasource = new NamesDataSource(this);
datasource.open();
ListView listView = (ListView) findViewById(android.R.id.namelist);
SparseBooleanArray checked = listView.getCheckedItemPositions();
List<Name> values = datasource.getAllNames();
ArrayAdapter<Name> adapter = new ArrayAdapter<Name>(this, android.R.layout.simple_list_item_multiple_choice, values);
for (int i = 0; i < checked.size(); i++) {
int position = checked.keyAt(i);
if (checked.valueAt(i))
datasource.deleteName(adapter.getItem(position));
}
adapter.notifyDataSetChanged();
setListAdapter(adapter);
datasource.close();
}
Your code is not optimal as you are creating a new adapter with each call to the delete method. You should maintain an activity wide reference to the adapter (possibly in your 'OnCreate')
The reason your listview is not updating is because you are filling its adapter with values but not removing the deleted ones from it.
e.g you are
datasource.deleteName(adapter.getItem(position));
but there is no corresponding
adapter.remove(name);
Try this
listView.inValidate():
That is happening because you are adding values to adapter before clearing database
add values to adapter after clearing/deleting values in database and then call adapter.notifyDataSetChanged();
Let me know if this helps you

Categories

Resources