class Venue {
private int id;
private String name;
}
I populate the ListView with something like
List<String> venueNames = data.getVenueNames();
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, venueNames));
So after the user selects a particular list item, I'd like to do a SQLite query with the venue id, but this information is lost when creating the venue names. How do you guys solve this?
EDIT: I should mention that it isn't guaranteed that venue name is unique.
Your problem is here:
List<String> venueNames = data.getVenueNames();
Don't do that. You are the one "losing" the information. If you don't want to lose it, don't lose it.
The simplest thing is to create an ArrayAdapter<Venue>, and have Venue's toString() return name. Then, calling getItem() on your ArrayAdapter<Venue> will return a Venue.
Related
Not sure if the title accurately describes what I'm trying to do, but here goes...
My app receives the following JSON object from the server:
[{"id":"0","code":"1234","name":"thing"}]
I want to create a ListView all of those items (the amount of items in the JSON always varies) and only display the value of the "name" field in my ListView. So my list should look like this for the user:
thing
another thing
third thing
...
Now I want to retrieve the value of "code" from the JSON object for that particular "name" when it is clicked on the ListView. So with the object above it would give me "1234" when "thing" was clicked.
Later I wish to send the "code" to another activity and do some followup queries to the database using it, but for now I can't seem to get any of this to work.
I am aware that with what I have right now, the "code" is not visible to the program because I build my ListView from Strings of "name" from the JSON object
I have the following OnClickListener
search_results.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView item is clicked!
}
});
My ListView is built like this:
JSONArray jsonArray = new JSONArray(json);
String[] stocks = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
stocks[i] = obj.getString( "name" );
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, stocks);
search_results.setAdapter(arrayAdapter);
Note: I have tried to use MatrixCursor, but after hours of fiddling around with it, I just gave up... Basically I built a MatrixCursor the same way I built my stocks[] and was hoping to return the index of the clicked ListView item and then just use that index on the MatrixCursor because I assume they would be pointing to the same thing? I'm not sure this is the right way to do this though. And in the end I couldn't figure it out anyway.
You have to create an object, POJO, reflecting your JSON object. Something like this :
class MyObject {
String id;
String code;
String name;
}
Then, use Gson to creates your list of objects from your JSON.
After that, you will be able to create an Adapter to deal with MyObject class instead of String. It would be easier to retrieve the information of the item you've clicked.
To achieve what you ask I think it would be a good idea for you to consider using RecyclerView instead of ListView.
1. Create a simple POJO class
And parse your JSON into, then store the POJO in an ArrayList.
class MyPojo{
private String id;
private String code;
private String name;
//Dont forget to add your getters and setters...
}
2. Switch your ListView for RecyclerView
I suggest you take a look at this tutorial for using Recycler View.
Recycler View Tutorial
From there on everything else would be pretty intuitive.
If you need any more help comment.
I have a ListView in my android app that does sorting. For example, when I clicked Name, it sorts by name.
Here is the code:
private class MyListAdapter extends ArrayAdapter<Person> {
private final ArrayList<Person> people;
//.......
public void sort(SortBy x) {
switch (x) {
case NAME:
this.sort(new OrderByName());
sortedBy = "NAME";
break;
Here is the Comparator:
private static class OrderByName implements Comparator<Person> {
#Override
public int compare(Person s1, Person s2) {
return s1.getName().compareTo(s2.getName());
}
}
When I click Name to sort the listview by name.. I got the list sorted but the old row of data still remains.. and associates with another row...
I am fairly new to Android... And I really need some help on debugging this issue. Thanks in Advance!
After sorting the list, just use the notifyDataSetChanged() method to your ArrayAdapter object. It will refresh the list. I'm assuming, you're not creating more than one ArrayAdapter object.
//notify that the list changed adapter.notifyDataSetChanged();
First check whether your ArrayAdapter have latest values in it.If it is updated with latest values used below :
adapter.notifyDataSetChanged(); // notify the listview to update items in the list.
Hope these helps you.
Thanks in advance for any reply. I currently working on android application that let user select several option from 10 spinner type(dropdownlist). But for every onChanged click in spinner, i have to check other spinner item for making a query in cursor. I thinking to use if-else statement to solve it as if-else statement was the easier way to approach the outcome i need but it will consume too many LOC and is not very efficient when modification of code is needed.
Sorry to demand solution from you guy but i cant figure a better method.
It is possible for you to store the last known value of the dropdown into a variable, and then run through those stored variables on click.
var storedVaribles[10];
OnChanged(int i, var changedVariable)
{
storedVariables[i] = changedVariable;
checkWithAllStoredVariables(storedVariables);
}
This is only pseudocode, but it should give you an idea of the direction to head with the solution.
erhaps this type of question would be more appropriate on CodeReview.
You have control of your spinner Adapters. Try to construct your data such that you directly obtain the information that belongs in your query. The basic idea is to have an object like this:
class SpinnerItem {
String displayTitle;
String querySyntax;
public SpinnerItem(String title, string sql) {
displayTitle = title;
querySyntax = sql;
}
}
Then make a SpinnerAdapter that keeps an array or a list of these objects. In getView(), you would bind the displayTitle to a TextView that the user sees. When you want to compose your query, you can call
SpinnerItem item = adapter.getItem(spinner.getSelectedItemPosition());
// use item.querySyntax in your query
As a very simple example, suppose I have a table with contacts, and each contact has either an email or phone number. I want the query to filter on this type based on the selected spinner item. Here's how I might build it:
List<SpinnerItem> items = new ArrayList<SpinnerItem>();
items.add(new SpinnerItem("All", "1"));
items.add(new SpinnerItem("Phone numbers only", "type = 'phone'"));
items.add(new SpinnerItem("Emails only", "type = 'email'"));
MySpinnerAdapter adapter = new MySpinnerAdapter(items);
spinner.setAdapter(adapter);
// later when building query ...
StringBuilder builder = new StringBuilder("select * from table where ");
SpinnerItem item = adapter.getItem(spinner.getSelectedItemPosition());
builder.append(item.querySyntax);
String sql = builder.toString();
I have a database with a table that stores int and string. The integer value is the primary key. I have created a function to just fetch the strings from the database and store them in a list which is then applied to the ListView using an ArrayAdapter as shown below.
List<String> list = db.getAllStringNotes();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_activated_1, list);
listview.setAdapter(adapter);
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
The trouble is deleting from this listview. Since the primary key is not present along with the string, I was deleting using the position of the item selected from the list view. But that obviously messes up things if I'm removing an entry from the middle of the list.
So was wondering if I could add the primary key i.e. an integer value to this list view along with the String but not display the integer value?
The simple thing is create two List,
1. String - Stored String notes
2. Integer - Stored all Primary Keys
So whenever user click on Listview user get its position, and based on that position get primary key value from second list and then perform your delete query.
There are many ways to do this: but as you are already using an ArrayList so i would suggest just make another arraylist while fetching from database:
So while deleting using the position :
Use the Primary Key from the PrimaryKeyArrayList
and delete values from both the ArrayList;
With this you will get exactly what you need;
Follow these Steps:
Create bean.java file that will have your db value
Create CustomAdapter.java to pupulate the items and handle the delete operation
Create delete method in your adapter and pass the selected bean object to delete from DB and ArrayList.
after deletion call notifyDatasetChanged method to reflect the change in list
EDIT:
Bean File:
public class MyDB_Bean{
public int id;
public String data;
MyDB_Bean(int id,String data){
this.id=id;
this.data=data;
}
}
Calling from Activity
ArrayList<MyDB_Bean> list = db.getAllStringNotes();
MyArrayAdapter adapter = new MyArrayAdapter<String>(this,list);
listview.setAdapter(adapter);
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
For CustomAdapter follow these tutorials
http://www.ezzylearning.com/tutorial.aspx?tid=1763429
Custom Adapter for List View
http://www.vogella.com/tutorials/AndroidListView/article.html
http://learnandroideasily.blogspot.in/2013/06/listview-with-custom-adapter.html
I have a Master Detail Flow activity, and in that activity there is a string array. I would like to format my item name string "content" in my list with HTML. However, I am not sure where to declare an HTML method to make this work, but I do know that the strings are called in a public string toString method in the Content.java file
public String toString() {
return (content);
}
or maybe I could get the list adapter to implement the formatting which is in my ListFragment.java file
setListAdapter(new ArrayAdapter<ExerciseContent.Exercise>(getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1, ExerciseContent.ITEMS));
Thanks in advance
Items item_data[]= null;
Items item = item_data[position];
ItemHolder.title.setText(Html.fromHtml("<u><b>" + item.title+ "</b></u>"));
Try, these may help you !