Android dynamically refreshes data in the ListView

Source: Internet
Author: User

Has been plagued by the ListView after the location of the problem, it is now understood that the original can not reset the ListView adapter, and should be used notifydatasetchanged () to refresh, so that the position will not be pinned.

Here is an example of a test, click on the last record, will add a new record,

The code is as follows:

1.TestAdapter

Package com. Testadapter;import java.util.arraylist;import java.util.hashmap;import Java.util.list;import java.util.Map;import Android.app.activity;import Android.content.context;import Android.os.bundle;import Android.view.View;import Android.view.viewgroup;import Android.widget.adapterview;import Android.widget.AbsListView.OnScrollListener; Import Android.widget.adapterview.onitemclicklistener;import Android.widget.AdapterView.OnItemSelectedListener; Import Android.widget.abslistview;import Android.widget.imageview;import Android.widget.listview;import Android.widget.simpleadapter;import Android.widget.textview;import android.widget.Toast; Public classTestadapter extends Activity {/** Called when the activity is first created.*/    Private int[] images; PrivateListView ListView; PrivateMyadapter Adapter; List<Map<String,Integer>>al; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); Images=New int[]{android. R.drawable.ic_btn_speak_now, Android. R.drawable.alert_light_frame, Android. R.drawable.arrow_down_float, Android. R.drawable.arrow_up_float, Android. R.drawable.btn_star_big_off, Android. R.drawable.btn_star_big_on, Android. R.drawable.button_onoff_indicator_off, Android. R.drawable.button_onoff_indicator_on, Android. R.drawable.checkbox_off_background, Android. R.drawable.checkbox_on_background, Android. R.drawable.ic_btn_speak_now, Android.        R.drawable.ic_delete}; ListView=(ListView) Findviewbyid (R.id.listview); Al=NewArraylist<map<string,integer>>();  for(intI=0; i< A; i++) {HashMap<string,integer > Map =NewHashmap<string,integer>(); Map.put (""+I, images[i]);        Al.add (map); } Adapter=NewMyadapter ( This, AL, R.layout.list_item,Newstring[]{"ImageView","TV"},                 New int[]{r.id.imageview, r.id.tv}];        Listview.setadapter (adapter); Listview.setonitemclicklistener (NewOnitemclicklistener () { Public voidOnitemclick (adapterview<?> arg0, View arg1,intCount,LongArg3) {                if(Adapter.getcount () ==count+1) {HashMap<string,integer> map =NewHashmap<string, integer>(); Map.put (""+(Adapter.mItemList.size ()), Android.                    R.drawable.ic_dialog_email);                    Al.add (map); Adapter.mitemlist=al;                    Adapter.notifydatasetchanged (); Toast.maketext (Testadapter. This,"is refreshing", Toast.length_short). Show ();            }                            }        }); }            Private classMyadapter extends simpleadapter{intCount =0; PrivateList<map<string, integer>>mitemlist;  PublicMyadapter (context context, list<? extends Map<string, integer>>data,intResource, string[] from,int[] to) {Super (context, data, resource, from, to); Mitemlist= (list<map<string, integer>>) data; if(Data = =NULL) {Count=0; }Else{Count=data.size (); }        }         Public intGetCount () {returnmitemlist.size (); }         PublicObject GetItem (intPOS) {            returnPOS; }         Public LongGetitemid (intPOS) {            returnPOS; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Map<string,integer> map = mitemlist.Get(position); intImage = Map.Get(""+position); View View=Super.getview (position, convertview, parent); ImageView ImageView=(ImageView) View.findviewbyid (R.id.imageview); TextView TV=(TextView) View.findviewbyid (r.id.tv);            Imageview.setbackgroundresource (image); Tv.settext (""+position); returnview; }    }}

2.main.xml

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Vertical"Android:layout_width="fill_parent"Android:layout_height="fill_parent"> <listview android:id="@+id/listview"Android:layout_width="fill_parent"Android:layout_height="wrap_content"></ListView> </LinearLayout>

3.list_item.xml

<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="Vertical"Android:layout_width="fill_parent"Android:layout_height="fill_parent"> <ImageView Android:id="@+id/imageview"Android:layout_width="wrap_content"Android:layout_height="wrap_content"> </ImageView> <TextView Android:id="@+id/tv"Android:layout_width="fill_parent"Android:layout_height="wrap_content"></TextView></LinearLayout>

Android dynamically refreshes data in the ListView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.