so I'm stuck on something probably pretty basic. I've got data stored in an SQLite database table and want to display that in an expandable list view. The data basically forms as headings and subheadings. There's many sub-headings to a single heading.
Retrieving the data is pretty straightforward. The part I'm stuck at is taking that data and giving it to the expandable list view.
I want the expandable list view to display the headings as the item that can be expanded, with the subheadings as the child items. How can I do this?
Thanks in advance.
You must provide an adapter to ExpandableListView. There is a base class adapter that you can extend it for your needs.
For creating the data lists. Assume Column1 is Heading, Column2 is SubHeading, try this in activity/fragment:
ArrayList<String> headings = new ArrayList<>();
HashMap<String, ArrayList<String>> subheadings = new HashMap<>();
String heading, subheading;
do{
heading = cursor.getString(1);
subheading = cursor.getString(2);
ArrayList<String> tmpChild;
if(headings.contains(heading)){
tmpChild = subheadings.get(heading);
}else{
headings.add(heading);
tmpChild = new ArrayList<>();
}
tmpChild.add(subheading);
subheadings.put(heading, tmpChild);
}while (cursor.moveToNext());
For the adapter, try my answer here: Tree with checkBox
Hope that helps!
Related
I'm designing a project about cataloging something. In this project user must be able to create his own table as he wish. Therefore I do not have any static class and instance of it.
I'm creating a diaglog pane and I can create textfields for user inputs according to column names of database table dynamically but how can i add those user's inputs into the tableView ?
As I can add any String input into the ListView can I add user String inputs into tableView columns?
ListView<String> listView = new ListView();
public ObservableList<String> listCatalogNames = FXCollections.observableArrayList();
listCatalogNames.add("Books");
More details with an example;
There is listview that contains all catalog names and according to lisview selection tableview will be created dynamically center of borderpane.
User have books(name, author, page) and movies(name, year, director, genree) catalogs.
Lets say user selected movies and tableView appeared with 4 columns and clicked add button. Diaglog pane created with 4 textfield. I built everything until that point but I cannot add user's input into the tableView because i dont have any static class for Movies or Books etc.
Is there any way to create dynamic class ?
Please give me an idea and help me about that situation.
here is the github link of our project
Just use String[] storing the Strings for every column of a row (or a similar data structure) as item type for the TableView. It should be simple enough to create a String[] from the inputs and add it to this TableView:
static TableView<String[]> createTable(String... columnNames) {
TableView<String[]> table = new TableView<>();
for (int i = 0; i < columnNames.length; i++) {
final int index = i;
TableColumn<String[], String> column = new TableColumn<>(columnNames[i]);
column.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue()[index]));
table.getColumns().add(column);
}
return table;
}
Adding a String[] userInputs to a TableView<String[]> table would be done like this:
table.getItems().add(userInputs);
A similar issue (creating a TableView based on the metadata of a ResultSet) can be found here: How to fill up a TableView with database data
Easiest solution that comes to my mind is to make use of polymorphism. You can create a super class of both Book and Movie, let's call it Item. Then you can declare your table to contain Item and cast to one of the concrete classes when you need to.
So I have arraylist modelData that populates a recyclerview using sqlite database in some activity.!
Now in my MainActivity I want an string arraylist of names from the modelData !
that's what did so far..
// inside the onCreate of MainActivity
//code ..
db = new DBHandler(this, null, null, 1);
modelData = new ArrayList<AzkarModel>();
modelData = db.getDataFromDB();
for (AzkarModel o : modelData) {
ArrayList<String> names = new ArrayList<>();
names.add(o.getName());
}
for (int i = 0; i< modelData.size();i++){
names.add(modelData.get(i).getName());
Log.i("The List Log", names);
}
Two problems
1) [FIXED] The names arraylist is showing the same element twice at first and end
I/ The List Log: [Mike, John, Sam, Nora, Mike]
2) The arraylist names doesn't get updated..! when I add/edit/delete from the recycler and go back to the MainActivity I don't see the new changes unless I close the app then open it again..! I can't use notifyDataSetChanged since there's no adapter here.!
Once you have an array list object, populate it inside a loop
ArrayList<String> modelNames = new ArrayList<>();
for (AzkarModel model : modelData) {
modelNames.add(model.getName());
}
Now you have an array list with model names inside. Concerning the second question, please use an recyclerview adapter here you can find a nice tutorial.
I managed to retrive data from the database and store it in ArrayList:
ArrayList<Category> selectCategory = select.all().from(Category.class).execute();
for (Category category: selectCategory) {
builder.append(category.name).append("\n");
}
Now I want to add that data to child element of ExpandableListView. The following code is hardcoded, and I don't want it that way. I want it to get data from database. How do I do that?
List<String> class1= new ArrayList<String>();
class1.add("English");
class1.add("Maths");
class1.add("Geo");
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
I used this tutorial to create my own ExpandableListAdapter.
Basically you need a list of all parent items and an additional Hashmap with the parent as key and the child as List<>.
List<Parent> parentList = new ArrayList<>();
HashMap<Parent, List<Child>> childHashMap = new HashMap<Parent, List<Child>>
So I am working on a news application which has a Arraylist TopNewsList with all the news from all categories. Now I have a category specific list of news, so I want to use the same arraylist in the adapter for different Listviews, I want the adapter to filter the news based on category id I provide (I have it in the arraylist), the Arraylist is actually - ArrayList<HashMap<String, String>> with id, news title, imageurl etc etc Note that I cannot use a copy , because there are fields like read/unread which should reflect across categories.
This is my ArrayList FYI
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CATEGORYID, id);
map.put(TAG_TITLE, title);
map.put(TAG_DESCRIPTON, description);
map.put(TAG_CREATED, created);
map.put(TAG_IMAGE, image);
map.put(TAG_IMAGE2, image2);
// adding HashList to ArrayList
TopNewsList.add(map);
From design point of view you should make class/type News with this fields.
So you will get
class News
{
String id;
String title;
// and so on
}
Then you will have ArrayList<News> allNews list of newses.
If you want to filter and get only some newses from the list just make another list
List<News> newsOfSportsCategory = ArrayList<News>();
for(int i=0;i< allNews.size();i++)
{
if(allnews.get(i).id.equals("idOfSportsCat")
{
newsOfSportsCategory.add(allnews.get(i));
}
}
You will get list with sport categories, and this is not copy because you are only holding references to objects that are also on allNews list.
Hi, I have an SQLite database and I'm using a simple cursor to read the database and then show it to a TextView. However, I would like to show the data in a ListView.
So I have the string but I don't know how to show it in ListView. Is there a simple method or will I have to create an adapter of sorts?
I was also wondering how you can set parts of the list view to the header and others to the subheading. I want it so that each row is a different list item if that's possible.
Here is a simple example.Lets say you have two array lists named "Items" and "ItemsId" in your main activity.
Now, in your database handler class fetch your table and store the values to the Items and ItmesId array lists using below code.
MainActivity.Items.clear();
MainActivity.ItemsId.clear();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
// Adding values to list
MainActivity.Items.add(cursor.getString(0));
MainActivity.ItemsId.add(cursor.getString(0));
} while (cursor.moveToNext());
}
Now populate your list view with Items array list in Main activity.
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, Items);
listview.setAdapter(adapter);
Use the SimpleCursorAdapter.
Here is a trivial example:
yourListView.setAdapter(new SimpleCursorAdapter(
/* The context where the ListView associated with this SimpleListItemFactory is running */,
/* resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to" */,
/* The database cursor. Can be null if the cursor is not available yet. */,
/* A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet. */,
/* The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet. */,
/* Flags used to determine the behavior of the adapter, as per CursorAdapter(Context, Cursor, int). */));