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>>
Related
I'm using a listview to load all database items inside it, and it works. The problem is everytime i recall that activity my listview is populated my duplicate items.
Here the code:
public class UserActivity extends ActionBarActivity{
public static ArrayList<String> ArrayofName = new ArrayList<String>();
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_activity);
DataHandler db = new DataHandler(this);
List<Data> items = db.getTitle();
listView = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, ArrayofName);
listView.setAdapter(adapter);
}
e.g. if i insert item "Facebook" it will be inserted inside my database, when i call UserActivity it will load database content(in this case Facebook item) and show all its content inside my listView. The problem is when i do this my listView show me 2 Facebook item. If i have 2 items, when i recall UserActivity it will show me 4 items.
What's wrong?
Thanks
The problem is with
public static ArrayList<String> ArrayofName = new ArrayList<String>();
It should not be static else this will be crated once and it will not initialize on the next activity recreate.Remove the static keyword and write like this-
public ArrayList<String> ArrayofName = new ArrayList<String>();
OR
Ensure the ArrayList is having no elements when you satrts populating from the database.
There are two solutions:
1.) Remove the static keyword from
public static ArrayList<String> ArrayofName = new ArrayList<String>();
If you can't remove the keyword for any reason, use solution 2.).
2.) Call the method clear() of your ArrayList before adding new entries.
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
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
I have a class that extends ListActivity with a SimpleAdapter as the list adapter. My code looks like this:
public class ListOfFirms extends ListActivity {
Intent extras;
int time;
String km;
ArrayList<String> firms = new ArrayList<String>();
SimpleAdapter adapter;
static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firms);
extras = getIntent();
time = extras.getIntExtra("time", 0);
km = extras.getStringExtra("km");
adapter = new SimpleAdapter(
this, list, R.layout.taxi_custom,
new String[] {"name","price"},
new int[] {R.id.taxi_name,R.id.taxi_price});
initializeFirm();
setListAdapter(adapter);
}
My question is how I can add a button to each element in the list, the button should be floating to right. My list contains object of the class Firm, how can I know which object that I grab out from the list, when a user presses this button?
here is example of custom listview which may help you
use custom adapter....
and set
listview.setAdapter(adapter);
You will have to write a CustomAdapter which extends BaseAdapter.
If you can use OnTouchListener and OnLongClickListener instead of a button it is a little easier to implement. Also if you just want to select the item, it ies easier tu use the standard built-in Android mechanisms.
Only if you REALLY need a button on each list item you have to do it like Gaurav Agarwal suggested... - which is something you might have to do sooner or later anyway :-)
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