ListView notifyDataSetChanged() - android

I have ListView s that keeps messeages of comments. First is message others are comments.
If user send comments It should appear behind of message.It is second row in listview.
When user make a comment I use notifyDataSetChanged().Inserted comments appear in listview but in the last row. I have sorting algorithm that keeps ArrayList what i want in sorting..
Although ArrayList sorted, Inserted comment appear in last row. It should not be.
Why notifyDataSetChanged() don't keep Listview according to ArrayList.It just update it new element to last row

notifyDataSetChanged() will notify data set change to listview, i think you need to modify arraylist and then call notifyDataSetChanged()

Related

Remove one row from gridlayout

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.

List focus on a particular item in that list dynamically

In my project i have a ListView for handling the list of a product. In that i have a list of 100 Items in that List view. Each and Every time i have been seeing the first row of the ListView..
If i need to see the data in the ListView Means, i need to scroll down, Can i focus directly to a particular item by any function by passing the position(ID) of the ListView..
For Example this is my ListView that contains 11 items in it..
I'm seeing every time the ListView as the following,..
And my try is, If i give the ListView position as ID=5 means, can i get the Focus of the ListView as follow, in the opening itself...
Can we get this by ListFragment method or anything else?
Suggest me for the Best..
try this, use setselection
listview.setSelection(position);
Don't know about ListFragment but in listview, you can use listview.setSelection(position);
This method will help you to achieve this
for example: listview.setSelection(5);

get text from specific position in listview android

I have a ListView with custom_row , every row has a textView1 and a textView2 , the list has now 2 records , and i have a button that is not on the list.
When i click the button i want to get the text from textView2 of the 2 records.
Is it possible?
I would take a shortcut, you ListView is being populated by an Adapter that uses a dataset. This dataset can be almost any datastructure such as Array, ArrayList, etc.
The layout you define, such as custom_row in you case only defines the structure of your view i.e. "where" items will show within on item on the list.
On the other hand, it is still your responsibility to tell the ListView "what" to show within the textView1 and textView2. You do this using the Adapter which connects the ListView to the dataset. More often than not, the ListView is a one-to-one mapping of the dataset i.e. the first item on the list is the first item in you dataset (I don't know what you are using for only two items, might be an array).
The ListView calls getCount() on the Adapter to find out how many total views there will be. It then call getView() for each view to be shown on the screen. It is in this method that you define what will actually show in a single view on the list (your custom_row).
Now you would know which entry of the dataset is supposed to populate which view in the ListView so you can just read it off there. For example, if your getView() does:
textView2.setText(getItem(position).getSomeTextField());
And the original dataset is an ArrayList named listDataSet
You could just do listDataSet.get(2).getSomeTextField()
NOTE: You will have to manage the scope of the dataset so that it's visible from wherever you are calling.
Get back your ListView (maybe already stored in an object thanks to findViewById, or by calling getListView() on your ListActivity).
Then call getItemAtPosition() on your list view, with the position you want.

ListView always on top

I use below code to update listview.
setListAdapter(MyAdapter);
MyAdapter.notifyDataSetChanged();
getListView().setEmptyView(empty);
But each time update, the list always show top one.
I want to let it only update data, but not focus on top.
How to do it?
Use a ArrayAdapter backed by an ArrayList. To change the data, just update the data in the list and call
MyAdapter.notifyDataSetChanged().
Although you can call notifyDataSetChanged() on SimpleAdapter too.
You need to add listview.setSelection(your_collection.size());. This will make the last item in the ListView as selected when you refresh the ListView. Also call this after refereshing the ListView.

Unable to refresh the list view in realtime after deleting an item from the list

I am having a list view with text and a button. The contents of the listview(the text) are fetched from the SQLite database. I have implemented the code such that, if anyone clicks the button, the corresponding flag of that list item changes in the database table. My intention would be, as soon as the flag is changed in the database table, the corresponding list item should disappear from the list.
Currently, I'm able to change the flag value for the list item (successfully updating in the corresponding column of the table), but the list item remains in the list view until I re-open the app. The list item then disappears as desired.
Is there a way such that as soon as I click the button, and as the flag value is changed at the background, the list refreshes and show me only other items of the list.?
Your help will be much appreciated. Thanks in advance.
You need to do a requery() after changing anything in data source.
-- Edit --
Just found that requery is deprecating, but anyway, the idea is the same. Query for another new Cursor, and set it in CursorAdapter (if you are using it).
Generally, for a ListView, you have to prepare an adapter for the ListView to provide the content of every item in the ListView.
The basic idea is that in order to refresh the listview, you have to call adapter.notifyDataSetChanged() to request the refreshment.
Query with a new Cursor and set the results in the CursorAdapter
invalidate() the existing list view
Thanks all for your responses. I have tried to do it in android way, but seems to be a bit complex. Hence inorder to refresh the list view after updating the list I have re-called the activity using intents so that I'll get a fresh list view with added or deleted list entries.
Hope I'll find other android way of solutions soon.
getLoaderManager().restartLoader(0, null, this);

Categories

Resources