I have a listview with 4 identical rows. Inside those rows, I have a RelativeLayout which contains a TextView (id : R.id.notif). In my Activity, I use my own ArrayAdapter.
I would like to be able to modify the text of the third row. I tried this but it isn't working.
((TextView)listview.getAdapter().getView(2, null, listview).findViewById(R.id.notif)).setText("50");
Thank you.
Do not use adapter.getView() for that! This method is used internally for the adapter to create the view that gets displayed in the list! The correct way to do this is to modify the underlying data and to refresh the list with adapter.notifyDataSetChanged(). Do not try to access views in the list directly, you don't know if they are visible at the moment or scrolled outside the view.
Related
I'm using a GridLayout with 5 coloums and multiple (not a fix value) rows. It is possible to add more rows with values to the grid by a add button (the values are hardcoded in hashmap for now).
In each row there is a delete button in the first cell.
Now I want to implement, that if you hit the delete button, the complete row should be deleted but also other rows should stay.
Any proposals how to do this?
You could remove the specific dataset from your HashMap (or use another map in the adapter for the dymanic content) and then call:
grid.setRowCount(grid.getRowCount() - 1);
grid.notifyDataSetChanged();
That should remove the specific row.
It's difficult to explain without having your implementation, but I'll try. So suppose we have our Activity/Fragment with a RecyclerView. We have our Adapter who holds a List/Map/Anything with your elements in it. What we need is the delete button and it's click listener i guess. When you click the button you call a method in your adapter from the Activity/Fragment, which iterates your List/Map/Anything removing the items. The method then calls notifyDataSetChanged() on the Adapter again, and the grid should update correctly.
I'm curious if I can create a footer for my custom listview in my arrayAdapter class?
What I'm hoping to get out of this footer are a subtotal, tax, and total textview that will automatically sum and do "math" on my listview's "Order" when new items are applied to it.
Would doing it in the arrayAdapter be the best approach? Or should I do it inside the fragment?
I was trying to do it inside the fragment but was running into difficulty with getting data from the row's of the listview.
Just to clarify; the listview is inside its own fragment, it will be pretty alone aside from the textviews I mentioned above.
If in the fragment, which part of the lifecycle should I put it in? I haven't set up onClickListeners just yet, but I feel putting everything in onCreateView() would be incorrect since I will be adding things dynamically.
Edit: The problem I was having while doing it in my fragment was that the getListviewChildren was returning empty; I'm assuming cause I was using it in my onCreateView?
Instead of implementing it yourself in the adapter you can use ListView's own implementaion for a footer view. just call addFooterView on the list and pass it the view you want as a footer.
ListView listView = .. //create your list, or inflate it from xml...
View footerView = ... //create your view, or inflate it from xml...
listView.addFooterView(footerView);
listView.setAdapter(myAdapter);
2 things you should know though: It is best to call this method before using setAdapter on the list, because on all but the latest version of android calling it after will not have any affect. Another thing is that doing so is implemented by wrapping your adapter inside another adapter, so after using this you cannot use getAdapter() to get your original adapter.
For more info go to: http://developer.android.com/reference/android/widget/ListView.html#addFooterView%28android.view.View%29
I reuse the same ListView for navigating through data, while updating the ListView data based on the user clicking a row. Up until now I had been using the same row View throughout the activity, but after the user clicks a row, I would like to change to a different row layout for all of the ListView's rows.
To be more concrete, say the ListView contains rows using layout1, and the user clicks on a row. The ListView's array adapter subsequently gets a new set of data and all the rows are now using layout2.
Rather than create a new ListView after the click or mess with setTag ugliness, I'd just like to clear all of the the cached row views that are sent via getView's convertView. I still want to subsequently recycle the layout2 views.
Can I do this, or do I have to "bite the bullet?"
It looks like ListView.invalidateViews() will do this:
public void invalidateViews ()
Causes all the views to be rebuilt and redrawn.
I want to change the background color of newly added listview rows. My listview is based on a custom adapter and the layout is inflated using the layout xml. I have tried many solutions but its somehow not working. I am not able to get the row elements in the listview.
Thanks.
You can study more here
http://developer.android.com/reference/android/widget/BaseAdapter.html
In addition, you can search with key word "Custom adapter"
here is some;
custom adapter for gridview in android
If the goal of the implementation is to show a newly added items in the adapter in a different color then you could implement the following logic.
Custom adapter, which you already have.
Another Arraylist to keep track of newly added items.
Add any new items to this arraylist.
in the getView() method, check if this item from the adapter is present in this auxiliary array, if present, change the background color of the view that you would return.
After sometime, maybe a timer, you may want to remove those elements from the auxiliary item.
Memory consumption maynot be too high because you are only storing reference to the objects that are present in the adapter already and not making a new copy.
I believe if you are saving the data you show in the ListView in array or database, you have to save the size of the array in SharedPreferences after setting the listadapter. Now assume you have 5 elements in your ListView and you save that, when you add again you have 8 elements, 8-5=3 now you know that the last 3 elements are new and you override the SharedPreferences with 8 (and so on..), in your custom listadapter change the background of the last 3 elements since you have all the values and the position of the row in the function:
public View getView(int i, View convertView, ViewGroup parent)
Which is the Integer i.
I have a ListActivity which gets its data from a database query. I also have a custom adapter which extends simple cursor adapter.
To show the customised content, I have overridden the newView and bindView methods.
Each element of the view has:
TextView containing a title
ImageView containing number of stars - the image shown is changed based on a value obtained from the database
A button - the button text changes on clicks (Favourite/Make Favourite), and triggers a database update event.
My problem is this - when I scroll the ListView, the changes I made seem to disappear .. for instance, the first item is marked favourite, and the list is scrolled ... when I come back to the first item, the text reverts back to its previous value, although internally the db has been updated.
I read that the notifyDatasetChanged() is not right for this case as the adapter should be notified of the database changes. I am trying to use the reQuery() method, but do not know where to place it.
Should I place the reQuery() in the onClick method of the button? If not, where should it be placed?
What so ever Adapter is you are using must have getView Method.. Whenever a list is scrolled this getView Method got called for all the listItem in the view. Make Sure in the getView Method. That the view is getting generated dynamically.
It should check for clicked or changed value.
to update the list without scrolling your have to call listView.getAdapter().notifyDataSetChanged()
Check the following link. here i mentioned a complete code to use listview properly. Using this we can achieve any listview behavior. We can embed animation also.
Change ListView background - strange behaviour
Hope this help :)
If you have extended BaseAdapter ,
after data is changed call , listView.setAdapter() and in getView() , update the view's data too.
Use SetListAdapter with ArrayAdapter to display the list. Use ConvertView to avoid inflating View.
if (convertView==null)
{
//inflate your list
}