I have a problem with my Android application which I'm developing in C#. I have a ListActivity and an ArrayAdapter and inside ListActivity i have custom views with a TextView and a Button.
This button should start new activities, get the results from the activities (mostly text inputs), and do something in the background, and then update the TextView's text.
I'm using ViewHolder pattern in the ArrayAdapter, and Command Pattern for the background operations.
My problem is that I can't find a way how to update the TextViews inside ArrayAdapter, since I have the results of the activities in the ListActivity. I tried to create an event in ListActivity and subscribed to it inside ArrayAdapter but I think it is not an optimal solution to this problem.
Could you suggest something better implementation to this problem?
Thanks in advance!
Related
i'm quite new to app developing and I am wondering about some good design patterns when it comes to adapters for ListViews.
I don't have any specific code (at least not simple and clear code) to accompany the question, but anyway, here is the situation.
I have an Activity in which I want to show any List of Books.
I made a CustomAdapter for this, based on layout called "row.xml." This xml includes a TextView and an ImageView.
I create my CustomAdapter in this Activity as follows:
customAdapter = new ListAdapterFullList(this, R.layout.row, bookList.getList());
Now i want to reuse this Activity to show a List of Books, but the layout needs to be different this time! It should be "row_alt.xml". Instead of a TextView and an ImageView, this list now needs to display a TextView and a RatingBar.
The problem that i am facing here is that obviously I want to reuse the Activity (since what i need to display is still a List of Books and all the other behaviour is the same), but I don't know how to handle the adapter. I can't just use this:
customAdapter = new ListAdapterFullList(this, R.layout.row_alt, bookList.getList());
because this ListAdapterFullList is not implemented to handle a RatingBar instead of an ImageView.
How can i bypass this problem? Do I need to create a different Adapter to show this layout? If so, then I will need to modify my existing Activity also, although I do not know how.
Btw, I read a lot about problems where different layouts need to be used for each row in a ListView, but this is not what I am struggling with. I want to reuse an adapter with a completely different layout for the Adapter. The rows themselves are always the same.
Thanks for the help, and apologies for the possible confusing question. You can always ask for more info!
If row.xml and row_alt.xml only have two different widget, I suggest that you create two different Adapter. If really there is a common behavior between this two Adapters, create a parent class that will implements the common behavior and make your two adapter extends this parent class. Then each Adapter implements its own [getView()](https://developer.android.com/reference/android/widget/Adapter.html#getView(int, android.view.View, android.view.ViewGroup)) method.
Then for further optimizations use the ViewHolder Pattern.
I also suggest that you take a look at RecyclerView which is the new version of ListView and don't need the ViewHolder Pattern
I searched allover but couldn't find any clear answer. I have a ListView declared in A customView and I would like to open another ListView when clicking on an Item.
I managed to do that by changing the adapter each time I click on an item on the Main ListView and show a pseudo new ListView. However this is not a stable solution.
I would like to know how can I instantiate a new listView without using intents or without having to change the adapter each time I want a new list output. I would want to create an entirely new ListView without direct connection to the initial one.
Can you try using ListFragment with FrameLayout in layout file? and just switch fragment when click on list item
I found the solution. I created in the CustomView several LinearLayouts for each listview one root layout. I have also created an individual class for each listView and used the same CustomAdapter and ViewHolder throughout. What I additionally used were interfaces that I declared in each List View class, with a method that tracks the row number. Based on this I implemented all the interfaces in the CustomView and listened to the clicked rows. At last, I then created animation methods and played with the visibilities.
By the way, instead of setting the List views to INVISIBLE i set them to GONE, hence there are quite manny the GPU doesn't need to recalculate the layout bounds.
Hope that helps cheers.
The method that i Place in the customView init() looks like this:
The interface listener must be implemented on "this" context of the CustomView, otherwise NUllPointerException hence the ListView doesn't know where to listen too.
The app I'm developing is using ActionShercklockBar,
On the main activity (SherlockActivity) I have a bar with a search field,
A ListView (which is to have two entries per row)
and a spinner
the problem is that as I'm starting with android, all the tutorials I've found require that my activity extends from ListActivity
but mine is extending SherlockActivity so I'm a bit confused as to how I can implement the two lines entry
Could anyone point me to a resource on the web or provide some input on this?
Any help is appreciated
You don't have to extend from ListActivity to use a ListView. Just place a ListView in your layout and then set it's ListAdapter in your activity's onCreate method. Here's an example that shows this style of ListView: http://windrealm.org/tutorials/android/android-listview.php
For the 2 line entry per row, you'll want to customize your List, so that you set the layout of each item displayed. Check out the OrderAdapter and the getView method implementation in this example: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
The answer for your question is to use the SherlockListActivity class and in adapter that you provided for your listView, in getView() method you have to create a custom view with two rows. There are a lots of tutorials on the web on how to create a custom item in your list view, so please have a look at that.
But if you will be still confused, write me and I will edit my answer with the real sample code.
I have the same problem as you, I have ActionShercklockBar and ListViews in the main activity.
What I did is to extend from SherlockListActivity in the main activity with a ListAdapter and it works well
I have several ListActivities, in which I need to set up, for each row, cusom typeface I am loading from assets. In TextView this is not a problem, but I am facing problem of accessing my list items. ListActivity seems to not have method *.setTypeface(), so I have come to the dead end. Anyone had faced same problem before?
How are you populating your ListView? Most likely you are using one of the generic ListAdapter implementations which seem to only provide bare-bones functionality and not much else.
My suggestion would be to design your own View to use in your ListView along with your own ArrayAdapter implementation that configures type face etc. from within it's getView(...) method. You'll find quite a few tutorials on the subject if you google "custom android ArrayAdapter".
You do not set the type face on the listview. You need to set the typeface on a textview. So use a custom array adapter for your listview, inflate your own custom view which would contain a textview, and then set the typeface on that text view.
To go along with what Nick said, google "custom android arrayadapter"
I have an app using an ScrollView and it loads a lot of images.
I decided to change to ListView. I saw that to use ListView, my class must extend ListActivity.
My current class does a lot of things, like inserting in database, updating, etc.
So, should I create a new classe just for the ListView? Can I just say that my current class now extends ListActivity?
In case I need to create a separate class just for the ListView, how do I use it in my current class?
Any help is appreciated!
Best regards
Your class doesn't necessary have to extend ListActivity you could use a simple Activity with a ListView element in it's layout. To use your existing activity just replace the current ScrollView element with a ListView element. Then retrieve this element in onCreate() and set its adapter that will map the images to the ListView rows. You will have to make your own custom adapter to show the images.
ListActivity is just a sub-class of Activity.
Its the same but it allows you to have utility methods such as setListAdapter() and also manages stuff like showing a placeholder when there is nothing in the list by looking for #android:id/empty in your inflated layout.
Using ListActivity it helps to manage the List that with the id of #android:id/list. It works just as well managing your own list without the ListActivity,it wont break your current code if used correctly just have to make an adapter for the list..