Related code for Android development----ListView Refresh

Source: Internet
Author: User

Mainactity

public class Mainactivity extends Listactivity {
static final int menu_manual_refresh = 0;
static final int menu_disable_scroll = 1;
static final int menu_set_mode = 2;
static final int menu_demo = 3;
int index=1;

Private Pulltorefreshlistview Mpullrefreshlistview;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mpullrefreshlistview = (Pulltorefreshlistview) Findviewbyid (r.id.pull_refresh_list);//1
New Getdatatask (). Execute ("http://api.expoon.com/AppNews/getNewsList/type/1/p/" +index);
Set a listener to is invoked when the list should is refreshed. Drop-down refresh processing
Mpullrefreshlistview.setonrefreshlistener (New onrefreshlistener<listview> () {//2
@Override
public void Onrefresh (pulltorefreshbase<listview> refreshview) {
String label = Dateutils.formatdatetime (Getapplicationcontext (), System.currenttimemillis (),
Dateutils.format_show_time | Dateutils.format_show_date | Dateutils.format_abbrev_all);

Update the Lastupdatedlabel
Refreshview.getloadinglayoutproxy (). Setlastupdatedlabel (label);//3. Refresh Interface processing agent, display new time

Do work to refresh the list here.

New Getdatatask (). Execute ("http://api.expoon.com/AppNews/getNewsList/type/1/p/" +index); 4. Perform a custom asynctask to refresh the data request
}
});

Add an End-of-list listener//5. Setting up pull-up loading processing
Mpullrefreshlistview.setonlastitemvisiblelistener (New Onlastitemvisiblelistener () {

@Override
public void onlastitemvisible () {

New Getdatatask (). Execute ("http://api.expoon.com/AppNews/getNewsList/type/1/p/" +index);
Toast.maketext (Mainactivity.this, "Loading", 1). Show ();
}
});


}


Private class Getdatatask extends Asynctask<string, Integer, string> {

@Override
Protected string Doinbackground (String ... params) {
Simulates a background job.
String json = NULL;
try {

Thread.Sleep (4000);
HttpClient httpclient=new defaulthttpclient ();
HttpGet httpget=new HttpGet (params[0]);
try {
HttpResponse execute = httpclient.execute (httpget);
int statusCode = Execute.getstatusline (). Getstatuscode ();
if (statuscode==200) {
httpentity entity = execute.getentity ();
JSON = entityutils.tostring (entity);
LOG.I ("AAA", JSON);
}
} catch (Clientprotocolexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

} catch (Interruptedexception e) {}


return JSON;
}

@Override
protected void OnPostExecute (String result) {
Super.onpostexecute (result);
Gson gson=new Gson ();
Final List_news Fromjson = Gson.fromjson (Result,list_news.class);
LOG.I ("BBB", Fromjson.getdata (). toString ());
Mlistitems.addfirst ("Added after Refresh ...");
Myadapter adapter=new Myadapter (Fromjson.getdata (), Getapplicationcontext ());
ListView Actuallistview = Mpullrefreshlistview.getrefreshableview ();
Actuallistview.setadapter (adapter);

Actuallistview.setonitemclicklistener (New Onitemclicklistener () {

@Override
public void Onitemclick (adapterview<?> arg0, View arg1,
int arg2, long arg3) {
TODO auto-generated Method Stub
Intent intent=new Intent (mainactivity.this,detailsactivity.class);
Intent.putextra ("List", Fromjson.getdata (). Get (ARG2));
StartActivity (Intent);
}
});
Adapter.notifydatasetchanged ();
Index= index+1;
Call Onrefreshcomplete when the list has been refreshed.
Mpullrefreshlistview.onrefreshcomplete ();
}
}

}

Detailsactivity

public class Detailsactivity extends Activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.show_detail);
Intent Intent = Getintent ();
News news= (News) Intent.getserializableextra ("list");
ImageView imageview= (ImageView) Findviewbyid (r.id.image_show);
TextView textview= (TextView) Findviewbyid (r.id.text_show);
Textview.settext (News.getnews_title ());
Bitmaputils bitmaputils=new bitmaputils (Getapplicationcontext ());
Bitmaputils.display (ImageView, News.getpic_url ());
}

}

Baseadapter

public class Myadapter extends Baseadapter {
List<news> list;
Context context;
Public Myadapter (list<news> List, context context) {
Super ();
This.list = list;
This.context = context;
}
@Override
public int GetCount () {
TODO auto-generated Method Stub
return List.size ();
}



@Override
Public Object getItem (int position) {
TODO auto-generated Method Stub
return List.get (position);
}

@Override
public long getitemid (int position) {
TODO auto-generated Method Stub
return position;
}

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
TODO auto-generated Method Stub
Viewholder Viewholder;
if (convertview==null) {
Viewholder=new Viewholder ();
Convertview=layoutinflater.from (context). Inflate (R.layout.list_view, NULL);
viewholder.textview= (TextView) Convertview.findviewbyid (R.id.text);
viewholder.imageview= (ImageView) Convertview.findviewbyid (r.id.image);
Convertview.settag (Viewholder);
}else{
Viewholder= (Viewholder) Convertview.gettag ();
}
ViewHolder.textView.setText (List.get (position). Getnews_title ());
Bitmaputils bitmaputils=new bitmaputils (context);
Bitmaputils.display (Viewholder.imageview,list.get (position). Getpic_url ());
return convertview;
}
Class viewholder{
TextView TextView;
ImageView ImageView;
}
}

Sub-Assembly class

public class List_news {
List<news> data;

Public list<news> GetData () {
return data;
}

public void SetData (list<news> data) {
This.data = data;
}

}

Layout page Main

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
tools:context= "Com.bwie.sx.MainActivity" >
<com.handmark.pulltorefresh.library.pulltorefreshlistview
Android:id= "@+id/pull_refresh_list"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:cachecolorhint= "#00000000"
Android:divider= "#19000000"
android:dividerheight= "4DP"
Android:fadingedge= "None"
Android:fastscrollenabled= "false"
Android:footerdividersenabled= "false"
Android:headerdividersenabled= "false"
Android:smoothscrollbar= "true"/>


</RelativeLayout>

public class News implements serializable{
String News_title;
String Pic_url;
Public String Getnews_title () {
return news_title;
}
public void Setnews_title (String news_title) {
This.news_title = News_title;
}
Public String Getpic_url () {
return pic_url;
}
public void Setpic_url (String pic_url) {
This.pic_url = Pic_url;
}

}

Related code for Android development----ListView Refresh

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.