simple_list_item_2 in Android - android

after creating some menus with simple_list_item_1 (which worked very fine) I tried to replace this by simple_list_item_2, but my system throws around with exceptions...
Now I'm wondering how to create such a two-different-sized-line-entry for my list...is there any trap for beginners? Can someone please help me fixing my (small!?) problem?
My code looks like this:
ListAdapter listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_2, fileNames);
setListAdapter(listAdapter);
My String[] fileNames stores all strings to display in ascending order (does this matter for the functionality?!)
After unsuccessfully searching in some forums I now hope that someone of you can give me an useful suggestion.
nice greetings,
poeschlorn

simple_list_item_2 is different, instead of just a TextView it contains a TwoLineListItem containing two TextViews. ArrayAdapter is not going to work here, because the constructor you're using expects only a TextView; just look at the constructors. Instead you'll either have to create a custom adapter or use one that supports it like SimpleCursorAdapter or (I think) SimpleAdapter. This guy has a somewhat hacky solution that might work for you.

Related

Implementing text color-coding in Android Listview?

I'm trying to implement color highlighting for specific words in a ListView.
I saw another post here suggesting to embed HTML within the text, as follows:
<font color='#FF0000'>Sample text</font>
However, this isn't working, and is instead showing up literally (as opposed to the tags taking effect and coloring the text).
I'm assuming there might be some flag I could toggle on that would allow embedded tags, but I have no idea where to start looking, or if this is even the right way to go about color-coding my listview text.
More information:
Listview in question is a simple 1 item per line listview.
Listview is populated with data from an array of strings.
My adapter is as follows:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
screenCode);
Any help would be very much appreciated.
Thanks in advance!
To geht full control over the generated view per item you could override the method ArrayAdapter.getView. There you could for example build a TextView which renders the html-formatted Text.
Another solution would be to define your own layout. For details see this question: ArrayAdapter in android to create simple listview

Append items to Simple Adapter dynamically

I have a very simple question. I'm working on an Android application where I make use of Simple Adapter for ListView. Now I need to append items to this ListView dynamically. I know it is possible to do so in case of Array Adapters.
It is done as follows:
adapter.notifyDataSetChanged();
Is there any equivalent for the same in case of Simple Adapters? I couldn't find much relevant links regarding this on the net.
Kindly help.
Thanks in advance!
I think not, but you always can create a new SimpleAdapter and call setAdapter again with the new adapter.
If you are going to add/remove elements you, probably, need some other adapter not a SimpleAdapter. Because they say in the docs that SimpleAdapter is 'An easy adapter to map static data to views defined in an XML file'

How to use a custom Adapter in a Fragment

I have a simple question, I know you can use:
setListAdapter(new ArrayAdapter<String> (getActivity(),
R.layout.your_layout,
R.id.your_text,
stringArray));
inside a ListFragment on your onActivityCreated() method. This works well for me so far, but only covers one item. For instance, say if in the the layout, I had two TextViews: sms_text, and sms_timestamp; both to be filled by smsTextArray [] and smsTimeStampArray [] respectively. How would that call be? I tried:
setListAdapter(new ArrayAdapter<String> (getActivity(),
R.layout.tweet_list_item,
R.id.sms_text,
smsTextArray,
R.id.sms_timestamp,
smsTimeStampArray,
));
That did not work :(
I also tried calling setListAdapter() twice in the same onActivityCreated(), but although that worked, it only displayed the second call of setListAdapter() in the list items.
I also tried creating my own Adapter but I kind of got lost in that mess. Can you guys point me in the right direction? I do not need the exact code, just a link to a library I could read, or perhaps it is a simple as just a code call. Who knows, maybe I am just using the wrong parameters, any ideas?
You can't use a simple ArrayAdapter and display more than one piece of data. An adapter maps one element in the list to one element in the listView. To have data come from two different pieces you need to either create your own adapter (simpler than it sounds). Or alternatively you can use a list of Maps in a SimpleAdapter (see this for an example).
Personally, I'd recommend going for the custom Adapter. You're going to have to code one eventually, and there's no time like the present. Plus, you're code will be more readable and obvious.

populate an expandableListView with arrayLists

Can anyone tell me any good links on how to do this?
I've looked through a good 15-20 links and none have really explained anything.
I just want to dynamically populate an ExpandableListView with ArrayLists or if theres
another way... I'd love to know.
PLEASE HELP :(!
I'm not sure an ArrayList is the right data structure to use for an ExpandableListView -- but if you mean a ArrayList of Maps that would be more appropriate. You could do that with the SimpleExpandableListAdapter class it looks like. Then use ExpandableListView.setAdapter() to couple it with your view.

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

Categories

Resources