Android autocomplete multiple textview lines in one result - android

is there anyway to have two TextView in one AutoCompleteTextView result?
e.g.
-------------------------------
result 1 title
result 1 alt text
-------------------------------
result 2 title
result 2 alt text
-------------------------------
And so on...
The ArrayAdapter only allows one binding for a view so how could I bind more views in the adapter?
Like so:
(...= new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_1item, R.id.list_item, String[]);
I've created a new xml layout for the layout parameter in the ArrayAdapter except when I bind the TextView to the string array, it only sets the text to the TextView with the given id.
I've also searched through the other posts about this one and it did not suit my needs.
If anyone could help that would be absolutely great :)
Thank you.

You can 'extend' the ArrayAdapter class with your own implementation. After extending the ArrayAdapter, use a LayoutInflater to load an XML layout. You can then override the getView method in order to populate the layout's UI fields.
Here is an example of how you could do it
I would advise to inflate the layout in the ArrayAdapter's constructor though, it avoids some of the unnecessary if statements you see in the author's code samples.

Related

Make one item bold on ListView with ArrayAdapter/SimpleAdapter

I'm trying to create a simple ListView. the items shown on the ListView are just strings from an array i have. Only that i want just one of the strings to be shown in bold. At first when i used a long TextView that contained all of the strings with line breaks tags inside a ScrollView, i could simply use
<b> </b>
around the string i wanted to be shown bold on the gui, this way:
TextView tv.setText(Html.fromHtml(str));
But using a big TextView inside a ScrollView caused other problems, and seemed a bad option when you have ListView exactly for this matter. So now I use a ListView, that gets all the strings from an ArrayList and displays them. Only that however I try to turn this around, I can't seem to change the style of one item into Bold. I also tried creating a TextView for each of the strings, and than add the TextViews to the List, but I don't know if ArrayAdapter or SimpleAdapter can do this. SimpleAdapter only seems to be able to get the TextView as a resource ID, which is the same for all items in the list, which prevents me from achieving what i want.
Long story short: after researching this issue here I know there are ways of extending BaseAdapter for a custom Adapter that allows any custom layout for the list, but is there really no other way? Do i really need to implement a BaseAdapter and #override getItem only to make one item bold? This seems odd to me. Does anyone know of a way I can just add TextViews to the ListView? using an ArrayAdapter displays the TextViews toString :/
If you know another simple way to show just one bold item, I'll really appriciate any help. Thanks!
Alright, I have finally decided to solve this by making a custom adapter which extends ArrayAdapter. There are many examples out there, but some of them provide a lot of extras and over complicate this issue in my opinion, and I just wanted to solve this decently. I found this example extremely useful and friendly, in my research here i saw a lot of developers asking similar questions, so I really hope this helps you too.
An adapter that extends ArrayAdapter isn't too complicated, but moreover allows you to have it exactly your way with the layout in a fairly simple way. After the adapter is done, you can just add html tags to your specific strings in your source array, and when you bind them on the Adapter, use Html.fromHtml . This is how I solved it. If anybody knows if there's a way to make bold or colored text in different specific items on a ListView without the need to make this adjustment yourself (extending ArrayAdapter and overriding getItem()) please do share it with us.
I hope this helps others.
Overwrite the getView(...) of your ArrayAdapter:
public View getView(int position, View convertView, ViewGroup parent) {
View returnedView = super.getView(position, convertView, parent);
if (position == specialPosition) {
TextView text = (TextView) returnedView.findViewById(R.id.text);
text.setTypeface(null, Typeface.BOLD);
}
return returnedView;
}
Inside the getView function, make a reference to the TextView like:
text = (TextView) convertView.findViewById(R.id.textBox);
and then:
text.setTypeface(null, Typeface.BOLD);
and you're done.

Object as a source for the listview

I have an object submission which contains some informations which I would like to show in a list view.
For example, in my object I can do this:
submission.firstElement().getDate()
and
submission.firstElement().getTitle()
which returns me a date and a title.
My problem is: how to show those two elements (not only the first's) in a listview (two elements per list item).
I have thinked on a for loop with the submission.size() but I have no idea how to get forward with.
Thank you for your help.
You'll want to look at using a ListView and a ArrayAdapter. You could use a BaseAdapter but I have found a ArrayAdapter more than adequate for displaying a list of custom objects in a ListView.
First create a layout that will be used to show the desired data per row in the ListView. In your case I assume that it will be two TextViews, one showing the title and date.
Once done, create a ListAdapter and in it's constructor give it the layout for the row created above. Once done, use the getView() function to access the layout and populate the TextView's with the object's date and title.
Have a look at the following links:
http://developer.android.com/reference/android/widget/ArrayAdapter.html
http://developer.android.com/reference/android/widget/BaseAdapter.html
http://www.vogella.de/articles/AndroidListView/article.html
EDIT:
Ana, to aid you a bit further use a ListActivity as shown in this Google documentation: http://developer.android.com/reference/android/app/ListActivity.html. In the example given they use a SimpleListAdapter. However, for an ArrayAdapter the constructor is a little simpler:
new ArrayListAdapter(this, R.layout.row_layout, listOfObjects);
When using a ListActivity a layout does not need to be created as by default it includes one.

easier way to make listview in android?

i have to make a listview in which there are two elements to be displayed vertically.
i know that to use the default adapter given with android there can only be one array and one text resource...ie if i am using android.R.layout.simple_list_view then there is only one text resource.
To make a custom Listview i am doing the following:
making a xml layout file for each element of the listview
extending a custom adapter class which extends the baseadapter
in the getview method of the custom adapter class i am inflating the view for each element and then returning with the info i want the listview element to have from an array which i have passed as a constructor to the custom adapter class.
this seems very tedious because there are several instances where i have to make listview where sometimes there are three text elements in each listview element and sometimes 2 text elements in each listview element.
is there an easier way to do the above.
thank you in advance.
With such simple layout, I'd suggest you to just use a LinearLayout and 2-3 TextViews (or any view you need, even an horizontal LinearLayout). Nothing will beat that simplicity. There's no need for a ListView in that case.
You could consider creating a generic, reusable ListView layout file that is loaded up with all the various elements you need (which, hopefully is a concise few). You could default as many of those elements in the layout XML file with android:visible="false" and then programmatically toggle the visibility.
Why can't you just reuse the adapter? It's got plenty of loading/unloading methods associated with it.
Yes, what Aleadam is saying; if you only have a couple of things why use a ListView? TextView would seem a much quicker way to prototype data display!

How to change values in second layout in same Activity?

My scenario is that I have a activity which shows the cursor stored in SQLite DB. The main layout contains textview at top and a listview. And then I use simplecursoradapter to populate cursor into listadapter and put listadapter into listview. simplecursoradapter use another layout. The problem now is that when I use simplecursoradapter I bring three columns into listview, example: item name, date and price. That is ok if I don't change these values.
Actually I want to add some string to price and form new string such as currency sign. According to my understanding we only can setContentView for one layout not two layouts.
I also tried to populate a new layout and set value but failed
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View textEntryView = inflater.inflate(R.layout.itemlist, null);
TextView price = (TextView) textEntryView.findViewById(R.id.price);
price.setText(currency + c.getString(4).toString());
Even there is no problem in syntax and run apps. But when I run the app and check listview, the price still show price only without adding currency sign. I only can add currency sign under the main layout not the second layout used in simplecursoradapter.
In fact, currency is chosen in user preference, and I use sharedpreference to retrieve its value and add to price value in cursor. It seems that simplecursoradapter is using different layout, so cannot do that.
Does anyone has ideas about this case ?
I would be appreciated if methods and codes are provided for similar approach.
Thanks !!
I'm not sure I follow your question 100%, so this might not be exactly the answer you're looking for.
If you want to have control over how the layout of items look in a list view, you should make your own custom implementation of ArrayAdapter:
A ListAdapter that manages a ListView
backed by an array of arbitrary
objects. (...) To use something other
than TextViews for the array display,
for instance, ImageViews, or to have
some of data besides toString()
results fill the views, override
getView(int, View, ViewGroup) to
return the type of view you want.
If you just google for custom arrayadapter example, you should find enough examples showing you how to implement this. Two such examples are:
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
http://android-er.blogspot.com/2010/06/custom-arrayadapter-with-with-different.html
Good luck :)
Finally I used bindview to solve my problem. In my question I already mentioned that I use simplecursoradapter to get data from SQLiteDB not array. I know arrayadapter works but it doesn't work for SQLite efficiently. Before I extended baseadapter to design my own to add custom views on textviews. It works but too slow if there are lots of data. So simplecursoradapter is more efficient.
I extended simplecursoradapter and override bindview for my own purpose. It works for what I need.
Thanks for your help and link for arrayadapter

How to generate a ListView with headers above some sections?

I want to generate a ListView that has some dividers between some of the entries, like it can be seen in some of the property sections. See the example below. I try to generate a List that consists of some textviews followed by one of the fancy dividers explaining the next part of the list and then again some text views. How can this be done? I thought about creating different views to add to the list? Is this the way to go?
I got a solution. I don't know if it is the best one.
I use a custom adapter derived from ArrayAdapter for the list as described in this tutorial. In the adapter class I check if the position in the getView method is a normal row, then I inflate the row layout. If it is the first row from a new group I inflate a headline layout that is a normal row plus the group headline above it.
If you don't want to mix the header into one of your rows. Consider the following solution:
You can overwrite the two methods getItemViewType and getViewTypeCount.
You now have a list that can display different rows. You need to check the expected view type for the item in the getView Method and inflate different layouts depending on it.
The list will handle the recycling for you in a way that it will return only correct recycle views to your getView method, this means if the recycleView is not null it can be used to display your current cell.
You can use my SectionedAdapter, if GPLv3 is acceptable (licensed that way due to some upstream code). You can use my MergeAdapter, if you need something more flexible and with a less-limiting license (Apache 2).
I think you might be looking for android.widget.ExpandableListView
http://developer.android.com/reference/android/widget/ExpandableListView.html
I'm also interested in an answer to this. There must be a more straightforward way to do this.
In looking at the Adapter, there's a method, Adapter.getItemViewType(int position).
ListView defines a return value, ITEM_VIEW_TYPE_HEADER_OR_FOOTER which indicates if the returned item is a header or footer.
I haven't tried it, but I assume if you create your own Adapter and return an item with the type indicating it is a header or footer, that the ListView will display it appropriately.

Categories

Resources