Listview sorting by priority wise - android

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();

Related

Android ListView adapter with two ArrayLists

In our chat app we want to use cool new Library SQLBrite to update chat on database changes. Since our chat has endless scrolling, and chat room can have very big list of messages, we want to split ArrayList supplied to Chat ListView adapter into two lists. Check graphic for the idea.
We want to set point in database above which, old messages will be queried by normal SQLite queries. And below that point we want set SQLBrite, that will bring us fresh messages added to database.
Each part should populate its corresponding ArrayList. And two arrayLists should be combined in one adapter.
My question is it possible to do? If yes how we can combine and handle two dynamic ArrayLists in single adapter?
Edit 1
1. I need to keep chat scroll position during from resetting, and no flickers during ArrayLists update.
1.With the help of generics you can handle two arraylist with single ArrayList.
For example in adapter :
setListData(ArrayList<T> pListData)
{
mListData=pListData;
}
In View
getView(int position, View convertView, ViewGroup parent){
T commonModel= getItem(position);
if(T instanceof ArrayListOneModel){
ArrayListOneModel model1=(ArrayListOneModel)T;
do stuf for first arraylit...
}
}
If you are using same model you can set a type (enum ) for both arraylist
& during showing time you can check that.
3.Otherwise you can first add old data in arraylist & then using collection
addAll() add 2nd latest message list in it. then
adapter.notifyDataSetChanged()
will set first old message then will set latest message in your list
More Clarification:
In second approach if you have different models for both arraylist then contain an enum in both model as a setter getter.
public enum eType{
FIRST_LIST_TYPE,SECOND_LIST_TYPE
}
During Fetching data from different DB's set Type in model.
e.g
public class model{
private enum eType;
// other setter getter value from your DB
/**
* Setter getter:
*/
public void seteType(enum eType)
{
this.eType = eType;
}
public enum geteType()
{
return eType;
}
}
During fetching data set Type e.g.
Model model = new Model();
model.seteType(eType.FIRST_LIST_TYPE) ;
//same for 2nd db.
& simply check type inside getView() according to your requirement.
yes that is possible inside BaseAdapter getCount method write following code
#Override
public int getItemCount() {
return list1.size()+list2.size();
}
and inside getView method you can do something like below
public View getView(int position, View convertView, ViewGroup viewGroup) {
if(position < list1.size) {
Object object = list1.get(position);
//write code to inflate view here related to list 1
}
else {
Object object = list2.get(position - list1.size());
//write code to inflate raw here related to list 2
}
}
You can pass only one list in adopter, which means you have to merge both arrays.
In order to merge both array. they have to be of same type, i.e. Array of same custom object.
If arrays are updating dynamically, then merge arrays again, as their data changes, and call notifyDataSetChanged() each time, to reflect changes in listview
Yes you can do it. but both arraylist should have common data format.
for eg ..
In adapter you can make method like
public void addMessages( <your_array_list> data ) {
list.addAll(data); //where list is your data container
}
now you may have two arraylist
like
ArrayList<your_type> oldMsg;
ArrayList<your_type> newMsg;
..
..
...
.
.
so you can call adapter method which we have created
yourAdapter.addMessages(oldMsg);
yourAdapter.addMessages(newMsg);

ListActivity Values are Objects, Want to Display Strings

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 :)

implementing custom array adapter for multi dimensional array

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;

Can I add data from different tables of the same database into each row of a ListView in Android?

Can I add data from different tables of the same database into each row of a ListView in Android?
I have a vendor app and I want to add data from one standard items list table and one daily table to the list view.
If both tables have the same row format (or at least you're selecting the same type of rows from both tables), you can combine them into a single query with UNION. See the sqlite select query documentation for more info on UNIONs.
I think you mean that the query joins two different tables; is that correct?
If you're using a SimpleCursorAdapter, then you can use a CursorToStringConverter to provide the labels for the ListView. Here's an example:
// choices to be displayed in the AutoCompleteTextView.
adapter.setCursorToStringConverter(new CursorToStringConverter() {
public String convertToString(android.database.Cursor cursor) {
final int columnIndex1 = cursor.getColumnIndexOrThrow("col1");
final String str1 = cursor.getString(columnIndex1);
final int columnIndex2 = cursor.getColumnIndexOrThrow("col2");
final String str2 = cursor.getString(columnIndex2);
return str1 + str2;
}
});
If you want to data from each of the tables to be presented in separate Views (instead of within a single TextView), then you can use a SimpleCursorAdapter.ViewBinder to update the Views. (Here's an example of a ViewBinder. I wrote this to work with a Spinner, but it works the same way with a ListView.)

How to pass more than one array to a listview?

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.

Categories

Resources