When display data from HasMap with key and value in Spinner it appear in one line. So how can I display data from HashMap in multiple line?
This is my try.
mydb = new DBHelper(this);
Studentcourses=(Spinner)findViewById(R.id.spinner);
HashMap<Integer,String> courses=mydb.getAllStudent_Course(std_id);
ArrayAdapter<HashMap<Integer, String>> adapter = new ArrayAdapter<HashMap<Integer,String>>(this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add(courses);
Studentcourses.setAdapter(adapter);
ArrayAdapter by its own can't write two lines, besides you use android.R.layout.simple_spinner_item
so you need to make own custom adapter like there
http://android-er.blogspot.com/2013/06/custom-arrayadapter-for-spinner-with.html
(just change the imageView to textview in layout)
Related
I can create a very simple Spinner with code like this
final List<String> categories = new ArrayList<>();
categories.add("First");
categories.add("Second");
categories.add("Third");
final Spinner spinner = new Spinner(context);
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(context, R.layout.spinner, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
however, this requires R.layout.spinner in my app. How can I avoid this XML with own, pure Java code, e.g. by implementing an own ArrayAdapter-like class?
Extend you custom adapter from ArrayAdapter and try to override getView() with your own code. You should be able to create the adapter with a null resource id. You will have to build you own views since you will no longer be using a resource.
Is it possible to show a thumbnail with a Array adapter in a list? The thumbnail is always the same.
final ArrayAdapter myAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1 );
ListView myList = (ListView) findViewById(R.id.list);
myList.setAdapter(myAdapter);
I populate my list with volley.
ArrayAdapter is usually used for populating a ListView widget from array or list of primitive values like String. When you are creating an ArrayAdapter with ArrayAdapter myAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1 ); as the second parameter you have R.layout.simple_list_item_1 which is a layout with only one TextView, so if you need a ImageView alongside a TextView, you will need to create new view that will contain TextView and ImageView widgets and create custom class that will extend Adapter where you will use the newly created view and populate it's views with your content.
I have spinner with array list thats work fine, but i want to sort out the datas from a to z (example: apple,ball,cat,dog...)order. I submit my code below
ArrayList<String> SourceArray = new ArrayList<String>();
Spinner Sourcespinner;// = new Spinner(this);
Sourcespinner = (Spinner)findViewById(R.id.Spinner1);
ArrayAdapter<String> SourceArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,SourceArray);
SourceArrayAdapter.add("Chennai");
SourceArrayAdapter.add("Mumbai");
SourceArrayAdapter.add("Kolkatta");
SourceArrayAdapter.add("Delhi");
Sourcespinner.setAdapter(SourceArrayAdapter);`
I don't know how to do sorting for this
you can use this to sort your data
Collections.sort(SourceArray);
Try to add data to the ArrayList and just use the Collections class to sort for you:
Collections.sort(SourceArray);
If you need to add your own objects they need to implement the Comparable interface and implement the method compareTo(). When changing the ArrayList's data make sure to notify the adapter that new data might have been added by using this code:
SourceArrayAdapter.notifyDataSetChanged();
If I have a hashmap containing the following:
Hashmap contains (String, String)
How can I instantiate a custom adapter? The custom adapter should extend baseadapter.
I need to combine both key and value so it looks something like "KEY+VALUE", "KEY+VALUE"... and assign this to an array VALUES. The array VALUES is used later on when I insantiate my custom adpter.
Instantiation should look something like this:
MyCustomAdapter adapter = new MyCustomAdapter(this, android.R.layout.simple_list_item_1, VALUES);
setListAdapter(adapter)
I am lost here so code would be a big help.
THANKS
graham
The following list is using as its datasource a string array called items.
public ArrayList<String> myList = new ArrayList<String>(Arrays.asList(items));
however items is a string array which I would like to stop using and instead start using concatenated key+value pairs from my hashmap
so instead of the user being presented a list of items he will be presented a list of key+value pairs taken from the hashmap hm
I don't think you need to use a custom adapter. Your layout is quite simple, you need only a textView, so you can use ArrayAdapter.
For you example you can do:
HashMap<Integer,String>hm=new HashMap<Integer,String>();
Vector<String>elements=new Vector<String>();
for(int i=0; i<=10;i){
hm.put(i,("num"+i));
}
for (Entry<Integer, String> e : hm.entrySet()) {
String newString=e.toString();
elements.add(newString);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, elements);
list.setAdapter(adapter);
I'm trying to bind data do a listview on android, but I'm not able to.
I saw some code on the internet and it worked, but I just don't know why, and I don't want to create a new ListView on the fly, I want to use the one that is listed on the main.xml
Why I can do this:
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,PEOPLE));
setContentView(lv);
But I can't do this:
ListView listPessoas = (ListView) findViewById(R.id.listPessoas);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.listPessoas, PESSOAS);
listPessoas.setAdapter(adapter);
What is R.id.pessoas??
This works fine for me:
String[] x = new String[]{"AAA","BBB","CCC"};
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> test = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,x);
lv.setAdapter(test);
Your error here is when you are creating the new ArrayAdapter. You are passing R.id.listPessoas as the row view to use for each row. This is the id of the ListView. The adapter is looking for a layout id containing a text view to be used for each row of the list. Change the R.id.listPessoas to android.R.layout.simple_list_item_1 and your code should work. The simple_list_item_1 layout is just a TextView that the data will be bound to.
What you have written is not correct, the second parameter in the array adapter constructor should be the simple layout for each list item not again your list view.
If your have a custom complex layout for your list item you need to write a custom adapter as well.
If you are getting error.
getApplicationContext(); will work instead of 'this'.