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...
Related
I would like to change it by getting the data from the database .
public void initList(){
product = new String[]{"apple","apricot","banana","orange","nuts","pears","pineapple","watermelon"};
listProducts = new ArrayList<>(Arrays.asList(product));
adapter = new ArrayAdapter<String>(this, R.layout.list_item,R.id.txtitem, listProducts);
listView.setAdapter(adapter);
}
For lists that come from the database, you should use a CursorAdapter
public void initList() {
String[] from = new String[] { productColName };
int[] to = new int[] { R.id.txtitem };
Cursor cursor = db.query(.....);
CursorAdapter adapter = new SimpleCursorAdapter(context, R.layout.list_item, cursor, from, to, 0);
listView.setAdapter(adapter);
}
This is an easy way to handle database data for a list item with all TextViews. If you have more complex data, such as for images etc. you should create a CursorAdapter subclass and override newView() and bindView().
You can use SQLite db, which is commonly used in android apps. You can insert and retrieve the data using SQLite queries.
For your example: you can create a table consisting the names of the fruits.
You can query your database to get all the fruits and populate the list (by adding the retrieved results in to the list) and use it as you are using it now to inflate the listView.
You can use the following tutorial to know about how to create a table, insert/update an entry and retrieve data from the table.
SQLite setup and sample queries
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 =)
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'm a Fresher to android.
I have a Listview and inside that Listview row I need 2 to 3 spinners.
Eg:
Inside that listview row I need to display the country list (spinner1),state list (spinner2) and city list (spinner3). Each of the three lists should be displayed in separate spinner.
Actually I have created a listview but I don't have any idea about inserting spinner inside the ListView row.
This is my listview code:
public void downloadBtnSelected(View anchor) {
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<6;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("Menu", Menu[i]);
hm.put("Images", Integer.toString(Images[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "Images","Menu" };
// Ids of views in listview_layout
int[] to = { R.id.imageView1,R.id.textView1};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) findViewById(R.id.listView1);
// Setting the adapter to the listView
listView.setAdapter(adapter);
Can anyone please orient me with a best way?