How to load a ListView with a multiple array - android

I'm trying to load my ListView from the following array
static final String[][] Bookings = new String[][] {{"WC11", "John Smith"},{"WX11", "Terry Jones"}};
I'm try to use the following code but Eclipse wont accept it.
ListView bkgList = (ListView) findViewById(R.id.bkg_list);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, Bookings));
It says 'the constructor ArrayAdapter(ListVActivity, int, String[][]) is undefined'. How can I load my array into my ListView?

ArrayAdapter is for a 1-d Array and the constructor expects something like String[] bookings.
You can do something like this. Keep in mind you would need to override toString() of Booking because that is what is displayed in the TextView.
class Booking {
String type;
String name;
public Booking(String type, String name) {
this.type = type;
this.name = name;
}
}
Booking[] Bookings = new Booking[2];
Bookings[0] = new Booking("WC11", "John Smith");
Bookings[1] = new Booking("WX11", "Terry Jones");
ListView bkgList = (ListView) findViewById(R.id.bkg_list);
setListAdapter(new ArrayAdapter<Booking>(this, R.layout.list_item, Bookings));
If you want to keep the 2-d array you can extend BaseAdapter to make a custom BookingsAdapter.

If you want more than a single TextView per row of your ListView you may want to consider subclassing BaseAdapter rather than using ArrayAdapter.
For an example of how to do this, see here

Related

Sorting a custom ListView Adapter with multiple ArrayLists

I've a custom ListView with 5 ArrayLists in the adaptor.
private ArrayList<String> name = new ArrayList<>();
private ArrayList<String> message = new ArrayList<>();
private ArrayList<Long> time = new ArrayList<>();
private ArrayList<String> imageId = new ArrayList<>();
private ArrayList<String> extras = new ArrayList<>();
ArrayList time contains remaining time in milliseconds. I want to display the list from lower to higher time left. If i just perform Collection.sort on a single ArrayList, others will remain unchanged. How can i sort all the ArrayLists accordingly with time.
I am passing all the ArrayLists to my custom adaptor at once.
Any suggestions?
I think better to create a new class like this
class Class {
String name;
String message;
Long time;
String imageId;
String extras;
}
Then creating single ArrayList then you can sort it by any fields by creating Comparator you want

How to make custom list with three columns with text in android

This is my code,in this code i put data in the string array and through looping i put all the data into the list called AcerList and give that list to adapter. The by getting the index from previous class i am checking the condition according to which i am giving an adapter if it's legal.
ListView lstView;
String AcerProductsArray[] = { "Acer Aspire V5-571 Ultra Book i3","AceAspireS3391i3","Acer Aspire S3-391 i3"};
ArrayList<String> AcerList = new ArrayList<String>();
ArrayAdapter<String> AcerAdapter;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.selectedcategory);
lstView = (ListView) findViewById(R.id.list);
try{
for(int i=0;i<AcerProductsArray.length;i++)
{
AcerList.add(AcerProductsArray[i]);
}catch(){}
AcerAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, AcerList);
Bundle extras = getIntent().getExtras();
int index = 0;
index = extras.getInt("index");
if(index == 0){
lstView.setAdapter(AcerAdapter);
AcerAdapter.notifyDataSetChanged();
}
This is my output.But i also want to set the price and details using customized lists,In xml file i have only one list and three textViews. i havent use any database all the programe in hard coded.Please help me in solving this..
A string array is not going to work for you. Define a class e.g Acer and define all the required attributes for the class. Then pass an array of the Acer objects to your adapter rather then Strings. e.g
The Acer class:
public class Acer{
String name;
String price;
String details;
public Acer(String _name, String _price, String _details){
this.name =_name;
this.price = _price;
this.details = _details;
}
}
Then pass an array of Acer objects to your adapter and you will be able to acess all the properties for an object inside the adapter. e.g if you want the price for the selected item, you would do this inside the adapter :
array_of_acers.get(position).price;
Also you would be needing a custom array adapter for this. A nice and simple example for a custom listview with custom adapter is given here

Simple Adapter to show part of List (made with Hashmap) by keyword

I have but of a problem. I've made a list as following:
List<Map<String, String>> ShopsList = new ArrayList<Map<String,String>>();
private void initList() {
// We add the cities
ShopsList.add(createShop("Antwerpen", "Broer Bretel"));
ShopsList.add(createShop("Antwerpen", "Caffènation"));
ShopsList.add(createShop("Antwerpen", "Caffènation - Take Out Nation"));
ShopsList.add(createShop("Antwerpen", "Coffeelabs"));
ShopsList.add(createShop("Antwerpen", "De Dikke Kat"));
ShopsList.add(createShop("Antwerpen", "Mlle Loustache"));
ShopsList.add(createShop("Berchem", "Broer Bretel"));
ShopsList.add(createShop("Berchem", "Caffènation"));
ShopsList.add(createShop("Berchem", "Caffènation - Take Out Nation"));
and
private HashMap<String, String> createShop(String key, String name) {
HashMap<String, String> shop = new HashMap<String, String>();
shop.put(key, name);
return shop;
}
So now I use SimpleAdapter to diplay this list in a Listview. But what I want is to be able to only show the data from the list with a specific keyword. So I do
ListView lv = (ListView) findViewById(R.id.listView);
SimpleAdapter simpleAdpt = new SimpleAdapter(this, ShopsList, android.R.layout.simple_list_item_1,
new String[] {"Antwerpen"}, new int[] {android.R.id.text1});
lv.setAdapter(simpleAdpt);
When I do this, he only shows me the data with the right keyword, but adds the other entry's as empty. So when I ask for the second keyword, he first adds 6 empty places before displaying the right entry's.
How should I do this? I think I should add the locations of the entry's with the wanted keyword, but how do I retrieve these locations in an easy way?
Thanks!
edit:
as it was pointed out, I mistaken SimpleAdapter with ArrayAdapter. Apologies and if you want to change your implementation to an ArrayAdapter (which is much simpler IMHO), the code is below.
original:
When filling up the android.R.id.text1 with the values, the ArrayAdapter just calls .toString() in each element from the List.
There're several ways to accomplish what you want.
One of them would be to make a List<String> and just make the Strings as you want to be seen on the screen.
A valid more "O.O." approach is to create your own class.
for example:
public class Shop{
private String city;
private String name;
public Shop(String city, String name){
this.city = city;
this.name = name;
}
#Override
public String toString() {
return city + " - " + name
}
}
and then on your adapter you'll use a List<Shop> instead of a map. and by overriding the toString() method you can manipulate the text as you wish.
just to mention, another way of doing it could be a extending the ListAdapter class

Android ListView: don't show all strings

I show a listview from the following string:
String[] values = new String[] { test1, test2, test3 };
The variables:
private String test1 = "test";
private String test2 = "test";
private String test3 = "test3";
Now I don't want to show the strings that contain "test" in my listview.
Like this:
if (String == "test") {
*don't show in ListView*;}
And I want it to test all Strings at once if they contain "test"
How is it possible?
EDIT: Here the adapter code:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
Yup...
if (values[i].equals("test")) // i being the iterator of your for-loop
OR
if (values[i].contains("test"))
contains is a slow(er) string function though.
You might want to use an ArrayList though... that way you can just add all those objects to the array list, then iterate through it... and remove them as you go...
// Do something to add all items to your array list
...
// Iterate through the list, removing what doesn't need to be there
for (int i = 0; i < arrayList.size(); i++){
if (arrayList.get(i).contains("test"))
arrayList.remove(i);
}
...Then set 'arrayList' (or whatever you've called it) as the string list for your adapter. If you use the same constructor it'll look like this...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, arrayList.toArray());
all the time, if you want compare 2 strings use the equals methode
read this about The equals() method
You should be using a filter on your ListView. Look at these examples:
(Simple iterating) http://androidsearchfilterlistview.blogspot.com/2011/06/android-custom-list-view-filter.html
(getFilter()) Filtering ListView with custom (object) adapter
Like others have posted, you compare String objects using .equals(), but if you are trying to only display certain items in your ListView you should use the getFilter() method like described in the link I posted.
edit: I found you a nice SO example.

String[] to ListView in Android

I want to make an list with two TextViews in each line.
I have String[] like this:
public class arrays {
public static String[] PodcastTitle;
public static String[] PodcastURL;
public static String[] PodcastContent;
public static String[] PodcastDate;}
I can create a list using this code, but it only allows to put PodcastTitle to list.
ListView lv1;
lv1 = (ListView)findViewById(R.id.listView1);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , arrays.PodcastTitle));
So my question is how can I make a list that displays PodcastTitle and PodcastDate?
You can use android.R.layout.simple_list_item_2 as already described.
But you can also use android.R.layout.simple_list_item_1. To do so you have to make a class like this (override toString):
public class Podcast {
private String title;
private String date;
// getter & setter
#Override
public String toString() {
return title + "\n" + date;
}
}
Make a list of your data instead of your arrays
List<Podcast> myData = new ArrayList<Podcast>();
And finally the adapter
ArrayAdapter<Podcast> adapter = new ArrayAdapter<Podcast>(this,
android.R.layout.simple_list_item_1, myData);
setListAdapter(adapter);
Instead of android.R.layout.simple_list_item_1 you need android.R.layout.simple_list_item_2, which contains a TwoLineListItem.
And instead of ArrayAdapter in that case you need to use SimpleCursorAdapter. But ideally your data should be structured differently, like a List<Pair<String, String>>

Categories

Resources