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.
Related
I have a List<Service> received from getParcelableArrayListExtra() Now I need to populate the received objects into a android ListView
I have try to populate the ListView in the following way. The ListView is populated to show the Object reference ids. Instead of showing contants.
How do i show the contents in this list.
The implementation is as follow :
List<Service> serviceCart = getIntent().getParcelableArrayListExtra("serviceCart");
ListView listView = (ListView) findViewById(R.id.lv_choosed_services);
ArrayAdapter<Service> serviceArrayAdapter = new ArrayAdapter<Service>(this,android.R.layout.simple_list_item_1,serviceCart);
listView.setAdapter(serviceArrayAdapter);
Open ArrayAdapter and go to read from line no 405 to 410
final T item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence) item);
} else {
text.setText(item.toString());
}
What you are doing:
If your item is String Adpater.getView(params) will use it, otherwise it will use item.toString(). In your Service.java you don't have a toString() so application uses default default toString()
What you should do:
Its best to create custom ArrayAdapter and populate your view
Override toString() in your Service.java and use only those values you want to populate in your adapter like service_name and/or service_code and default implementation will do the rest.
Note:
Please read Naming Conventions, its not recommended to name your objects like service_name it should be serviceName.
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 make a clickable and scrollable list of images. When image is clicked it should appear appropriate string in TextView.
I have list of Object which have images id and string id in itself:
public class MyClass implements Serializable {
private int imageId;
private int stringId;
//(...)
}
public ArrayList<MyClass> listOfObjects = new ArrayList<MyClass>();
How it is best to do? I do not know too much to go about it.
Thanks in advance.
PS: Sorry for my bad english.
You have to implement a custom list view. The list view shall consist of a custom layout, in which every line consists of a button. Then, you can assign each imageId to the buttons. Then, it can display the related text using the same index in the list.
I asked a question before about splitting string but maybe it wasn't clear enough.
I made a simple activity which has an example to what my problem is.
I have a message and it's a long one coming from a server.
I need to split this message and put it inside a listview, I'll show you my code.
public class Page1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity6);
String message = "0---12,,,2013-02-12 08:04,,,this is a test,,,0---11,,,2013-02-12 08:05,,,and this is why it is damaged,,,0---10,,,2013-02-12 08:06,,,what comes from select data randomly";
String[] variables = message.split(",");
ListView listView1 = (ListView) findViewById(R.id.listView12);
String[] items = { variables.toString() };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
}
}
Now let's say that the split is commas ", " so it will be
0---12 ------->ID1
2013-02-12 08:04 ------------>date1
this is a test ----------->subject1
0---11 ------->ID2
2013-02-12 -8:05 ------------>date2
and this is why it is damaged ----------->subject2
And so on, now what I can't do is that I want to put these strings in a loop and write them to a listview such that the subject1 should be in item1 and date1 should be in subitem1 like this
Subject1
Date1
------
Subject2
Date2
------
This is how the listview should look like
Can anyone help me with this please?
You would need to create a custom ArrayAdapter to populate a ListView from your objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs, in it you will override the getView method and use the position parameter you receive for the corrent row View to indicate the row index.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter
Please note that ArrayAdaper is designed for items containing only one single TextView. From the docs:
A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView
Consider subclassing ArrayAdapter (docs) and override its getView method.
I am trying to implement Search feature for my android app.So, I am using AutoCompleteTextView. But, I am using ArrayList of custom Object.
This is my class.
public class Vehicle
{
private String model,manufacturer;
}
Now, I have ArrayList of these Vehicle objects.
My question is, How to use these arraylist of objects to set as adapter for AutoCompleteTextView to search for model ??
How to use these arraylist of objects to set as adapter for
AutoCompleteTextView to search for model ??
You don't say what do you show from the Vehicle class. The simplest way to setup the AutoCompletTextView is to use the simple ArrayAdapter(which will only show one value from Vehicle, the model) and also modify the Vehicle class like this:
public class Vehicle {
private String model, manufacturer;
#Override
public String toString() {
return model;
}
}
Then simply give the list of custom Vehicle objects to the ArrayAdapter and set the adapter on the AutoCompleTextView.
For anything more complex then this you'll need to provide more details.