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.
Related
ListView list = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> dataAdaptor = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, android.R.id.text1, cars);
list.setAdapter(dataAdaptor);
I write that codes for listview but I don't know how I get data for subitem. How can I do this?
A ListView item can have it's own custom layout. When you create your adapter for the ListView you can pass in the layout id to the Adapter constructor.
If you want to show some more details like image and text or two textview then You will have to extend an Adapter and implement getView() to property set the image+text.
please check this answer with examples.
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)
I have a ListView that i have populated with a string array: {"1","2","3"} using an Array Adapter.
(ListView) ListOptions = (ListView)findViewById(R.id.ListOptions);
String[] exampleString = new String[]{"1","2","3"};
ListAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,exampleString);
ListOptions.setAdapter(adapter);
Later on, i want to be able to change exampleString to exampleString2:
{"4","5","6"}
and have the ListView update with exampleString2 displayed as the list options.
is there something i can do along the lines of:
adapter.ChangeStringArray(exampleString2);
ListView.setAdapter(adapter);
I want to avoid having to create a new adapter each time I change the String array that is populating the list items.
ArrayAdapter has methods for managing the backing data array.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, new ArrayList<String>());
adapter.addAll(exampleString);
adapter.clear();
adapter.addAll(exampleString2);
adapter.notifyDataSetChanged();
I need to put small icons on the right of each list item. I know it can be done with a custom list but it will mean rewiring a lot of code. if there a way to do it without using a custom adapter?
mList = (ListView) findViewById(R.id.sidebar_list);
mList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, mStrings));
mList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
No, overriding getView inside a custom adapter is the only way to dynamically set different images to your list rows. However, if you want to set a static (same) image for all the row items, then you may define your custom resource xml for the row (with a TextView and ImageView or comppund drawable-TextView inside) and pass it to your default adapter when initializing.
You will need to create XML layout for the view (if you don't have one) and add an imageview.
you should give it and ID ('thubnail').Next you just ask the adapter for a certain view by its position
View singleView = listView.getChildAt(wantedChild);
ImageView thumbnail = (ImageView) singleView.findViewById(R.id.thumbnail);
thumbnail.setImageBitmap(bitmap);
Yes, you can use SimpleAdapter and a custom layout with an ImageView.
Trivial example:
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("text", "one");
map.put("image", String.valueOf(getResources().getIdentifier("ic_launcher", "drawable", getPackageName())));
list.add(map);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new SimpleAdapter(this, list, R.layout.list_item, new String[] {"text", "image"}, new int[] {R.id.text, R.id.image}));
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'.