Android ListView Refresh Single Row
After I have gotten the data for a single row of a ListView
, I want to update that single row.
Currently I am using notifyDataSetChanged();
but that makes the View
react very slowly. Are there any other solutions?
Asked by: Rafael382 | Posted: 25-01-2022
Answer 1
As Romain Guy explained a while back during the Google I/O session, the most efficient way to only update one view in a list view is something like the following (this one update the whole view data):
ListView list = getListView();
int start = list.getFirstVisiblePosition();
for(int i=start, j=list.getLastVisiblePosition();i<=j;i++)
if(target==list.getItemAtPosition(i)){
View view = list.getChildAt(i-start);
list.getAdapter().getView(i, view, list);
break;
}
Assuming target
is one item of the adapter.
This code retrieve the ListView
, then browse the currently shown views, compare the target
item you are looking for with each displayed view items, and if your target is among those, get the enclosing view and execute the adapter getView
on that view to refresh the display.
As a side note invalidate
doesn't work like some people expect and will not refresh the view like getView does, notifyDataSetChanged
will rebuild the whole list and end up calling getview
for every displayed items and invalidateViews
will also affect a bunch.
One last thing, one can also get extra performance if he only needs to change a child of a row view and not the whole row like getView
does. In that case, the following code can replace list.getAdapter().getView(i, view, list);
(example to change a TextView
text):
((TextView)view.findViewById(R.id.myid)).setText("some new text");
In code we trust.
Answered by: Sarah872 | Posted: 26-02-2022Answer 2
One option is to manipulate the ListView
directly. First check if the index of the updated row is between getFirstVisiblePosition()
and getLastVisiblePosition()
, these two give you the first and last positions in the adapter that are visible on the screen. Then you can get the row View
with getChildAt(int index)
and change it.
Answer 3
This simpler method works well for me, and you only need to know the position index to get ahold of the view:
// mListView is an instance variable
private void updateItemAtPosition(int position) {
int visiblePosition = mListView.getFirstVisiblePosition();
View view = mListView.getChildAt(position - visiblePosition);
mListView.getAdapter().getView(position, view, mListView);
}
Answered by: Kelsey356 | Posted: 26-02-2022
Answer 4
The following code worked for me. Note when calling GetChild() you have to offset by the first item in the list since its relative to that.
int iFirst = getFirstVisiblePosition();
int iLast = getLastVisiblePosition();
if ( indexToChange >= numberOfRowsInSection() ) {
Log.i( "MyApp", "Invalid index. Row Count = " + numberOfRowsInSection() );
}
else {
if ( ( index >= iFirst ) && ( index <= iLast ) ) {
// get the view at the position being updated - need to adjust index based on first in the list
View vw = getChildAt( sysvar_index - iFirst );
if ( null != vw ) {
// get the text view for the view
TextView tv = (TextView) vw.findViewById(com.android.myapp.R.id.scrollingListRowTextView );
if ( tv != null ) {
// update the text, invalidation seems to be automatic
tv.setText( "Item = " + myAppGetItem( index ) + ". Index = " + index + ". First = " + iFirst + ". Last = " + iLast );
}
}
}
}
Answered by: Alfred159 | Posted: 26-02-2022
Answer 5
There is another much more efficient thing you can do, if it fits your use-case that is.
If you are changing the state and can somehow call the proper (by knowing the position) mListView.getAdapter().getView()
it will the most efficient of all.
I can demonstrate a really easy way to do it, by creating an anonymous inner class in my ListAdapter.getView()
class. In this example I have a TextView
showing a text "new" and that view is set to GONE
when the list item is clicked:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// assign the view we are converting to a local variable
View view = convertView;
Object quotation = getItem(position);
// first check to see if the view is null. if so, we have to inflate it.
if (view == null)
view = mInflater.inflate(R.layout.list_item_quotation, parent, false);
final TextView newTextView = (TextView) view.findViewById(R.id.newTextView);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCallbacks != null)
mCallbacks.onItemSelected(quotation.id);
if (!quotation.isRead()) {
servicesSingleton.setQuotationStatusReadRequest(quotation.id);
quotation.setStatusRead();
newTextView.setVisibility(View.GONE);
}
}
});
if(quotation.isRead())
newTextView.setVisibility(View.GONE);
else
newTextView.setVisibility(View.VISIBLE);
return view;
}
The framework automatically uses the correct position
and you do have to worry about fetching it again before calling getView
.
Answer 6
For me below line worked:
Arraylist.set(position,new ModelClass(value));
Adapter.notifyDataSetChanged();
Answered by: Ned872 | Posted: 26-02-2022Answer 7
Just for the record, did anyone consider the 'View view' on the override method ?
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Update the selected item
((TextView)view.findViewById(R.id.cardText2)).setText("done!");
}
Answered by: Maya432 | Posted: 26-02-2022
Similar questions
android - Performance issue - ListView, TextView with a Spanned input within each child
Currently I have a ListView with a TextView within each child. That TextView is taking in a Spanned string to populate it. I am using the ViewHolder pattern and I am not parsing the Spanned string in getView(). When I switch from a Spanned string back to just a plain string the performance of the ListView's scrolling increases dramatically. I would like to keep using Spanned because it will format my text correctly on the ...
performance - How to update Android ListView with dynamic data in real time?
I have a background thread loading data which I want to display in an Android ListView. The data changes very often (i.e. 1-2 times per second). Sometimes the number of rows in the dataset changes too (but certainly not as often as the data in the cells changes).
There are two ways to update the data in the cells, as far as I can tell:
Have the background thread notify the UI thread that new dat...
performance - Best way of showing Stream Data in Android Listview
We are trying to show a table data on a listview. The data consists of 8 columns and around 50 rows.
In one second period approximatelly 8 update data comes.
(ie:update row2-column5 to something.)
Every time a new data comes, we are updating the dedicated cells and we call datasetchanged() of the adapter
and this causes some performance and scrolling problems.
My question is:
What is the ...
slow performance of android listview due to add contact image in my listview
I have make on demo that list all contact from contact Uri in that I have made custom list view after adding of contact image my listview scrolling is very slow, following is my code.
public Bitmap getProfilepicture(Activity activity, String address)
{
Bitmap bitmap;
Uri personUri = Uri
.withAppendedPath(Phones.CONTENT_FILTER_URL, address);
Cursor ph...
Android: How to Improve listview search performance
My App has a List-view to display values from an SQLite DB. There is an edit-text on the of the listview for searching for values in the List-view. Here is some of my code:
public void onTextChanged(CharSequence s, int start, int before,int count) {
String sCondition = "C_Data like '%" + ed.getText() + "%'";
Cursor data = database.query("tbl_Contents", fields, sCondition, null, null...
android - ListView fling performance worse on Galaxy 10.1" Tab (3.1) vs 7" Tab (2.2)
I have a simple ListView which contains a number of TextViews.
When I do a (similar) fling through this list, on a Galaxy Tab 10.1" and a Galaxy Tab 7", I notice the following differences:
the fling duration is much shorter on the 10.1" (about 2.6 sec vs 1.7 sec)
the fling distance is much more on the 10.1" (the list scrolled more)
the number of calls to OnScrollListener.onScroll i...
performance - Android and ListView with a very poor and slow scroll
I have a customized listview. In this list, I have something like 15 items. When I am trying to scroll, the scroll is very slow and not smooth.
Here is my listView:
<ListView
android:id="@+id/AnnouncementsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="25dp"
android:cacheColorHint="#00000000"
android:fastScrollEnabled="true"
an...
Android - performance issue with ListView update when in Fragment
I have my layout like following:
| Header |
___________________________________________
| | |
| | |
| List of user | Fragment with Details |
| | [Contains ListView] |
| | |
| | ...
java - How do I improve the performance of a listview?
When I use images from the internet in a Listview, my Listview scrolls very slowly.
But in some applications such as Twitter, Google+ and other applications (despite showing images from the internet) there is no problem with scrolling and it is very fast and smooth.
How do I improve the performance of a Listview with images?
android - 3 holders inside listview and Performance
I Have a 3 holders. 1 holder for 1 item.
Method getView looks:
public View getView(int position, View convertView, ViewGroup parent) {
mCursor.moveToPosition(position);
int type = checkDialogType(mCursor);
Holder holder = null;
if (convertView != null){
holder = (Holder)convertView.getTag(type);
if (holder == null){
holder = createHolderByType(type, parent);
...
performance - Android resource hungry operations
I am using JSON to receive data from the web service, creating text to JSON object is resource hungry operation.
I have to load images from the web, I am lazy loading images in the async process how can I compress and make the cache ??
After the crash, how can I stop re-launching activity?
How can I know how much memory remaining and Android OS will kill my...
Performance TestCase Interface in Android
Has anybody implemented the PerformanceTestCase provided by android and got some results...If you have done it please provide some source code,it will be really helpful...
Here is the android link:
http://developer.android.com/reference/android/test/Performance...
android OpenGL ES simple Tile generator performance problem
following this question : Best approach for oldschool 2D zelda-like game
Thank to previous replies, and with a major inspiration from http://insanitydesign.com/wp/projects/nehe-android-ports/ , i started to build a ...
Streaming video playback performance issues with background tasks on Android
I have developed a little application that can play video from a rstp streaming sever (darwin in this case, but this is not relevant) by means of the VideoView widget (I have tried Mplayer+SurfaceView approach too). I use Wifi connection for this.
The video plays just smooth when video is the only task. The small application should carry out other tasks at the same time such as continously discover...
Replace standard Android JSON parser for better performance?
I know that Android has a JSON parser baked in but I was wondering if it was worth using something that offered better performance (like Jackson - see http://jackson.codehaus.org/) ? Anybody tried that ?
dalvik - Changing coding style due to Android GC performance, how far is too far?
I keep hearing that Android applications should try to limit the number of objects created in order to reduce the workload on the garbage collector. It makes sense that you may not want to created massive numbers of objects to track on a limited memory footprint, for example on a traditional server application created 100,000 objects within a few seconds would not be unheard of.
The problem is how far should I tak...
android - For-Loop Performance Oddity
I just noticed something concerning for-loop performance that seems to fly in the face of the recommendations given by the Google Android team. Look at the following code:
package com.jackcholt;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(save...
performance - How to cache and store objects and set an expire policy in android?
I have an app fetch data from internet, for better performance and bandwidth, I need to implement a cache layer.
There are two different data coming from the internet, one is changing every one hour and another one does not change basically. So for the first type of data, I need to implement an expire policy to make it self deleted after it was created for 1 hour, and when user request that data, I will check the s...
How does Android emulator performance compare to real device performance?
I'm looking into writing an Android game, tough I don't curerntly own an Android device. For those of you who own a device, how does the performance on the emulator relate to real device performance? I'm especially interested in graphics related tasks.
This obviously depends on both the machine running the emulator, and the specific device in question, but I'm talking rough numbers here.
This question is a ...
performance - Is it better to use GL_FIXED or GL_FLOAT on Android
I would have assumed that GL_FIXED was faster, but the iPhone docs actually say to use GL_FLOAT because GL_FIXED has to be converted to GL_FLOAT. Is it the same on Android? I suppose it varies by phone, but what about recent popular ones (Nexus One, Droid/Milestone, etc.)?
Bonus points: This appears to be completely undocumented (e.g. search google for GL_FI...
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android