I want to show only one parameter from object(s) found using AutoCompleteTextView. I have list of custom items and I'm using this list in ArrayAdapter which is used in my AutoCompleteTextView. But as I find item by typing something to AutoCompleteTextView, only object as whole is shown (Object type and some identifier), but I want to show just Objects attribute "name" which is a String.
The way i'd go about this is making a separate arraylist with all the names inside of it. Display that and get the user to chose from there, once they have use the index to find the object in the other list.
Initiate new string array
String[] data = new String[1]); // terms is a List<String>
for(int i=0;i<=1;i++){ //only the 1st position of ur data getting inserted
data[0]=s.get(i).toString();
}
ArrayAdapter<?> adapter = new ArrayAdapter<Object>(activity, android.R.layout.simple_dropdown_item_1line, data);
keywordField.setAdapter(adapter); // keywordField is a AutoCompleteTextView
Related
I have a list of custom objects, that I created from downloaded JSON array and I would like to populate the Spinner with just one field from my list, but I still want to keep the whole onItemSelected.
Currently I am doing it like that
ArrayAdapter<Country> myAdapter = new ArrayAdapter<Country>(Prototype_activity.this, android.R.layout.simple_spinner_item, ValueHolder.countryList)
spinner.setAdapter(myAdapter);
spinner.setVisibility(View.VISIBLE);
So how do I say to adapter to use only one field from each item?
...and I would like to populate the Spinner with just one field from my
list, but I still want to keep the whole onItemSelected.
If you are going to use an ArrayAdapter then implement the toString() method for the Country class to return a String representation of the desired field.
How can I create an empty spinner with no elements in it and add them later?
ArrayAdapter<String> aSpin = ArrayAdapter.createFromResource(this, null , null);
Doesnt work.
I'm curious, in which scenario would you want an empty spinner?
anyway, a spinner with a null object will give you an annoying exception. If you really want a spinner "empty", i would recommend you to create a normal spinner with a fake object. That object would be a string like " ". So, it won't be empty but it will look like if it is.
-- Edit --
In order to use a spinner with objects, you will need an adapter. So, first, if you want an empty spinner, just find its view from your layout and that's all. You will have an empty spinner:
mSpinner = (Spinner)findViewById(R.mLayout.mSpinnerID);
Then, when you need to add your items to the spinner, create an array with those items, then add them to the adapter, and add the adapter to your spinner. For example, let's say that you will have strings:
ArrayList<String> mList = new ArrayList<String>();
mList.add("item 1");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mList);
mList.setAdapter(adapter);
An Adapter is something different than a Spinner. If you want to create an empty Spinner, just define one in your layout file or create one programmatically. It will not contain any items if you don't set an Adapter on the Spinner (or specify static entries in the xml declaration).
You might want to take a look at the Spinner tutorial.
I have a doubt. I have 3 array list dynamic values. I need to display these dynamic values in a listview. can someone please tell me how can i achieve this.
I have name[] array, status[] array and image[] array. I need to dynamically display the values in listview in a android sample
This is what i have:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,NameList);
In this i am able to display i am able to display all the names in NameList[].
An array list only accepts an array of values, so as you see, you can only pass in the names. You have two options,
The simple option is to create a compound object, say Person, that has a name, status, and image. Then you create a Person[], and create the array list on that, and pass it into the array adapter. You must implement Person.toString() to print out the Person object as you'd like.
If you need to be able to lay out the Person fields in a more flexible way then you could simply by implementing toString() on Person, you need to create a custom list adapter. You can take a look at this post to get you started,
Categorise the listview
I want to create a AutoCompleteTextView in android. The problem is that I want to show whole list of data when user selects AutoCompleteTextView and start filtering data as user types the letters.Please help me in doing this.
Well here is an way how you can do that,
declare an String array -
String[] array = new String[]{"first","second","third","fourth"};
Now, initialize the Adapter with the source.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,array);
Finally fetch the AutoCompleteTextView id from your xml and set the adapter.
AutoCompleteTextView mView = (AutoCompleteTextView)
findViewById(R.id.myAutoTextView);
mView.setAdapter(adapter);
I want to display List of Contact Names with the respective phone numbers
like
Vikas Patidar <9999999999>
Rahul Patidar <9999999999>
using AutoCompleteTextView when a user type text in the mobile number field.
In default style I can only display the list of names.
Can anyone please tell me how can I implement this so that when a user select any item in list and I can display number of that in Mobile number field.
You need use adapter for this task. For example, with SimpleAdapter:
SimpleAdapter adapter = new SimpleAdapter(context, list, R.id.row_layout, new String[] { "Name", "Phone" }, new int[] { R.id.name, R.id.phone });
autoCompleteField.setAdapter(adapter);
For this you need make a layout XML file and list must be of type List<? extends Map<String, ?>. Strings in 4th parameter are keys for maps in list. Ints in 5th parameter are identifiers of components in layout file.
Or you can extend any adapter and use it. See this link for reference.