How to change values in second layout in same Activity? - android

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

Related

Android: How do I set TextView for each item in a ListView?

I need someone to point me in the right direction on this....
In my Android application I have a ListView with custom views for each row containing various textviews each. I retrieve weather information for multiple locations and then want to write the results for each location to its own listview item. The question is, how do I refer to each textview on each listview item?
I can't do it by name using findViewById, can I? If there are three items in the list, will the temperature textview for each item be names the same, just differ by an array index? If so, how do I specify TextViewName(0), TextViewName(1) and TextViewName (2) for the three list items?
Yes, I'm still very new to Android....but loving it!
You need to do this when each item is created (or recycled) in the getView method that you should override in the adapter.
Watch the google io talk from 2011 on list views and it should be quite clear how they work.
http://www.youtube.com/watch?v=wDBM6wVEO70
Use a type of adapter:
http://www.java-samples.com/showtutorial.php?tutorialid=1516

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.

Android autocomplete multiple textview lines in one result

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.

Creating Multi-Column Layout with ArrayAdapter

I've been trying to fix this problem for a while and have exhausted all the different Google searches that I could think of. I'm a beginner with Android, but understand most of the fundamentals (I think). Anyways, I'm trying to display a list of data in a multi-column layout spreadsheet style. I'm using a ListView for that because I need users to be able to click on a row to get a more detailed look at the data since only so much can fit in a row. I have it working perfectly using a SimpleAdapter, but since the amount of data can sometimes be large, up to 500 entries, I wanted to change over to a Lazy Loader system so users don't have to stare at a black screen for 20 seconds while everything loads. I think I can manage with the Lazy Loader part, but I can't figure out how to switch over from my multi-columned format SimpleAdapter to an ArrayAdapter that all the Lazy Loader examples use.
Here's an example of my SimpleAdapter code:
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
fillMaps is the List of HashMaps that are to be inserted. 'grid_item' is the layout that the entries are inserted into. 'from' is an array of column names (alternatively the keys in the HashMap) to be used. 'to' is an array of TextView items that will be filled in 'grid_item'. Any idea how to convert this to an ArrayAdapter? Or how to Lazy Load with a SimpleAdapter?
Thanks in advance!
To solve this issue for others, here is what I did to get it working perfectly:
Created my own class called Entry which simply contains the strings for the rows
Created a custom adapter that extends ArrayAdapter. For the constructor I pass into it an ArrayList of type Entry.
Create a ViewHolder for better UI efficiency
Inflate the row layout xml which is then added to my listview
To get the lazy loading working:
Added a method in my custom adapter that will append a new ArrayList of Entry objects to the ArrayList that I already have in my adapter
Created an onScrollListener and from some debugging test found that if the top visible item plus the remaining entries equalled the total entries, then I was scrolled all the way to the bottom
Once the bottom was detected, I would call my fetch method to retrieve another 30 entries, add these entries using the add method I created, and then use the notifyDataSetChanged() method for the ArrayAdapter to display the newly loaded items
Just write your own adapter. Create a private a class in your Activity that houses your ListView that extends BaseAdapter. Fill out the abstract methods and display your data however you want to. As useful as SimpleAdapter obviously is, you'll get a lot more traction out of your own adapter, and they're really not difficult to write.

Assigning SQL result to a Listview

I am trying to build a listview which will have the format of
{PlayerAname} vs {PlayerBname}
{PlayerAScore} - {PlayerBScore}
So this is a more complex (atleast to me) listview as it will have 4 properties I would need to set in it...
I've looked for some tutorials or examples but haven't found what I was looking for. I need to know how to set the properties and how to build the layout of a single listview item
thanks in advance.
You want to use a SimpleCursorAdapter. The docs for ListActivity show how to bind an SQLite result set to a list view using a SimpleCursorAdapter, including how to direct each column into a particular field of the row. The example uses the stock android.R.layout.two_line_list_item row layout, but you can use your own to lay out the data in a row as you see fit.

Categories

Resources