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 =)
Related
I'm having a list view populated from sqlite with different data. I use a Simple Adapter to populate the list. Here is my code how I populate the list:
ArrayList<HashMap<String, String>> userList = controller.getOld();
// If users exists in SQLite DB
if (userList.size() != 0) {
// Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter(OldTips.this, userList, R.layout.old_tips, new String[] {
"userId", "tara","ora_meci" ,"echipa_acasa","echipa_deplasare","cota","explicatii","rezultat","pauza","final"}, new int[] { R.id.userId, R.id.tara , R.id.ora_meci, R.id.echipa_acasa, R.id.echipa_deplasare, R.id.cota,R.id.explicatii,R.id.rezultat,R.id.pauza,R.id.finall});
final ListView myList = (ListView) findViewById(android.R.id.list);
m
myList.setAdapter(adapter);
}
Is there a way, on this particular way of populate a list view, to add section headers and each section header to be a date which I get from the sqlite? I walk through other solutions but didn't find a way to achieve it...
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();
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>
I have a class that takes 4 parameters for a List. The class is called Item. I created a List variables that is an ArrayList . I then create a new ListAdapter. I set this adapter to the main ListView. I then add 10 inputs into the the ArrayList. This all works but when I try to add the ArrayList Items to the ListAdapter is gives me an error saying to add a Cast to the method.
Here is the code that I am talking about
final List <Item> tempList = new ArrayList <Item>();
mainListViewListAdapter = new ListAdapter(MainActivity.this, R.layout.main_list_item_layout, tempList);
// set mainListAdapter
mainListView.setAdapter(mainListViewListAdapter);
for (int x = 0; x < 10;x++){
tempList.add (new Item ("Example1","Example2","Example3","Example4"));
}
***HERE IS WHERE I GET THE ERROR AT addAll();***
mainListViewListAdapter.addAll(tempList);
mainListViewListAdapter is set to ListAdapter, and mainListView is set to ListView.
Why would I add a cast to mainListViewListAdapter? I have run this code on another project and I didn't have to use a cast.
Need destroy adapter - after using and configure each time use - new StringArray of Values for Adapter and new Adapter.
Use about this constructionale:
String[] values1 = new String[] { "Sunny", "Sealand", "Lengord" };
makeListItem(values1);
public void makeListItem(String[] addValues){
MySimpleArrayAdapter lvAdapter1 = new MySimpleArrayAdapter(this, addValues);
ListView1.setAdapter(lvAdapter1); lvAdapter1 = null; }
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