So I want to display more than one text view in a list view but I have completely no clue about how to do this. The idea I am trying to implement is actually a list of music files loaded from a multi dimensional array such that
String musicFiles[][] = {{"TRACK NAME","ARTIST NAME"}};
so that within the same row the array adapter can display the "TRACK NAME" and below that "ARTIST NAME" but obviously the array adapter can not take a multi dimensional array and i have to implement a custom adapter and extend the array adapter class. Problem is I have no idea how to do this. Help!
Multidimensional array might not be the simplest solution. I would suggest creating a container class for all required pieces of information about the music file.
Something like this:
public class MusicFile {
public final String trackName;
public final String artistName;
public MusicFile(String trackName, artistName) {
this.trackName = trackName;
this.artistName = artistName;
}
}
An then use it inside an array:
MusicFile[] music;
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 had a list view containing 5 arrays, 4 arrays are static arrays and 1 array is dynamic array coming from the server.
My question is how can i sort the entire list view using 1 array(dynamic)
My Adapter is like this
Adapter adapter=new Contacts_Adapter(class.this, Contacts_Names_Array, Contacts_Numbers_Array, Contacts_Images_Array, Contacts_Ids_Array, Chat_List_Array);
in the above code contain 5 arrays
Static Arrays = Contacts_Names_Array, Contacts_Numbers_Array, Contacts_Images_Array, Contacts_Ids_Array
Dynamic Array : Chat_List_Array
Based on the dynamic array i want sort the list view
Any suggestion.....?
If you merge all those arrays into one list of POJO objects whose members are contactName, contactNumber,..., contactId then you can use Collections.sort to sort the list.
Merging is simply creating a class like this
class Bla{
String contactName, contactNumber;
int contactId;
}
which you fill with the values from your arrays. And then put together the results in one list. If you use Adapter then you have likely done this as Adapter only corresponds to one list of objects.
To sort based on id, you define a Comparator
public class BlaIdComparator<Bla> implements Comparator<Bla> {
public int compare(Bla bla1, Bla bla2){
return bla1.contactId - bla2.contactId;
//this depends on your sorting order, ascending or descending
}
}
then use it on your list like this
Collections.sort(list, new BlaIdComparator());
then refresh your Adapter for the change to take effect
adapter.notifyDatasetChanged();
I have a ListActivity that is currently displaying a list of type Data objects. These type Data objects have custom data:
String title;
The following is in my ListActivity constructor:
List<Data> values = dataInterface.getAllData();
ArrayAdapter<Data> adapter = new ArrayAdapter<Data>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
The application displays on screen all the Data objects without error.
The problem is that the text displayed for each list item is:
Data#42731008
Data#427362c0
and so on.
I understand these are the object id's of the Data objects. Instead, I want to display the Strings found in Data.title. I can't figure out how to accomplish this.
If you don't want to use a custom Adapter, then define the public String toString() method in your Data class as follows:
public String toString {
return title;
}
It will be used by the ArrayAdapter when it represents the object in the ListView.
In your adapter :
holder.myTextview.setText(getItem(position).getTitle());
The point is in the getItem(position).getTitle(), because your custom adapter extends ArrayAdapter<Data>, getItem(position) will return an instance of Data.
After that, you just need to get the title from the Data using getTitle(); (depends on your setter - getter in you Data class).
Just comment if you dont understand something or if i miss understand you :)
I want to populate a spinner with some values from a List
The list is populated with objects
ArrayAdapter<PersonDetails> toStopAdapter = new ArrayAdapter<PersonDetails>(MoreTicketSalesActivity.this, R.layout.generic_spinneritem,
R.id.spinner_item_name, personDetails);
Spinner.setAdapter(toStopAdapter);
My PersonDetails class looks like this
private int id;
private int index;
private String name;
At the moment when i set the adapter the personDetails is full with data
But on my spinner is displayed some strange text "com.project.person ..."
What i do wrong?
Thanks
You're using a simple display for each element of the spinner. What does 'generic_spinneritem' look like?
The simple answer is to make sure your PersonDetails class overrides toString and returns the name (or whatever you want). Right now its showing a class name.
For more complicated row display, you'll need to create a custom adapter and override getView.
I am using a ListView to display items. Currently I am passing a String array of items to it. But I want to pass one more array and display its items along with the items of the first array; that is, somehow having two lines of text.
How can I do that?
You could also make a custom Adapter for your list and use custom class as type of items of that adapter. Something like this:
class CustomAdapter extends ArrayAdapter < RowData > {
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
}
}
class RowData {
String line1;
String line2
RowData(String l1, String l2){line1=l1;line2=l2;}
}
Regards!
Since both the arrays contain data to be displayed in a row of a listview, IMO it is logical and fair to believe that they are related in some manner.
You can create a class called RowData and have a String(data type of first array) and Object(or datatype of second array) as its members. You can then pass an array of Rowdata type to the list view.
For instance, if you want to pass: String[] and Date[] to the listView you will pass
RowData[] now, where Rowdata is defined as:
class RowData{
String dataItem1;
Date dataItem2;
}
Use a table or format the items of the various arrays into a new array.