Notifydatasetchanged () refreshes the ListView (adapter with Jsonarray bindings)

Source: Internet
Author: User

1.fragment Code:
Package com.ts.fragment;




Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Random;
Import Org.json.JSONArray;
Import org.json.JSONException;
Import Org.json.JSONObject;
Import COM.EXAMPLE.MYWINXINTEST.R;
Import Com.ts.fragment.AutoListView;
Import Com.ts.fragment.AutoListView.OnLoadListener;
Import Com.ts.fragment.AutoListView.OnRefreshListener;
Import Com.ts.myst.JSONArrayAdapter;
Import Com.ts.util.HttpUtil;
Import Android.annotation.SuppressLint;
Import Android.graphics.Paint;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import android.support.v4.app.Fragment;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemSelectedListener;
Import Android.widget.ListView;
Import Android.widget.Spinner;
Import Android.widget.TextView;




public class Searchfragment extends Fragment implements Onrefreshlistener,
Onloadlistener
{
Spinner Areaspinner;
TextView Searchtextview;
ListView ItemListView;
map<string, string> countmap = new hashmap<string, string> ();
String url1 = httputil.base_url+ "Asset_findassetarea.action";
String url2 = httputil.base_url+ "Asset_findassetsb.action";
String url3 = httputil.base_url+ "Asset_findassetsbcount.action";
Private Autolistview Lstv;
Private Jsonarrayadapter Jsonarrayadapter;
Private Jsonarray Itemjsonarray = new Jsonarray ();
Private String Areaid;
Private Handler Handler = new Handler () {
@SuppressLint ("Newapi")
public void Handlemessage (Message msg) {
String result = (string) msg.obj;
Jsonarray resultjsonarray = null;
try {
Resultjsonarray = new Jsonarray (result);
} catch (Jsonexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
Switch (msg.what) {
Case Autolistview.refresh:
Lstv.onrefreshcomplete ();


try {
for (int i = 0; i < itemjsonarray.length (); i++) {
Itemjsonarray.remove (i);
}
for (int i = 0; i < resultjsonarray.length (); i++) {
Itemjsonarray.put (Resultjsonarray.get (i));
}

} catch (Jsonexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Break




Case Autolistview.load:
Lstv.onloadcomplete ();
for (int i = 0; i < resultjsonarray.length (); i++) {
try {
Itemjsonarray.put (Resultjsonarray.get (i));
} catch (Jsonexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Break
}
Lstv.setresultsize (Resultjsonarray.length ());
Jsonarrayadapter.notifydatasetchanged ();
};
};
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate)
{
View v = inflater.inflate (r.layout.search_fragment, NULL);
Lstv = (Autolistview) V.findviewbyid (R.ID.LSTV);
try{
Itemjsonarray = new Jsonarray ("[]");
Jsonarrayadapter = new Jsonarrayadapter (getactivity (), Itemjsonarray, "Assetcode_assetname_assetsyr", False, "3");
//Itemlistview.setadapter (ADAPTER2);
Lstv.setadapter (Jsonarrayadapter);
Lstv.setonrefreshlistener (this);
Lstv.setonloadlistener (this);
InitData (Areaid);

@Override
public void onnothingselected (ADAPTERVIEW&LT;?

> Parent) {
TODO auto-generated Method Stub

}
});
}
catch (Exception e) {
E.printstacktrace ();
}
return v;
}
private void InitData (String areaid) {
LoadData (Autolistview.refresh,areaid);
}
private void LoadData (final int What, final String areaid) {

New Thread (New Runnable () {

@Override
public void Run () {
TODO auto-generated Method Stub
try {
Thread.Sleep (700);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
Message msg = Handler.obtainmessage ();
Msg.what = what;
try {
Msg.obj = <span style= "font-family:arial, Helvetica, Sans-serif;" >getdata (areaid) <span style= "font-family:arial, Helvetica, Sans-serif;" >;
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Handler.sendmessage (msg);
}
}). Start ();
}
@Override
public void OnLoad () {
TODO auto-generated Method Stub
LoadData (Autolistview.load,areaid);
}
@Override
public void Onrefresh () {
TODO auto-generated Method Stub
LoadData (Autolistview.refresh,areaid);
}
Test data
public string GetData (string areaid) {
String result = null;
try {
result = Httputil.postrequest (Url2, countmap);//Get Data from server
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}




return result;
}
}

Notifydatasetchanged () refreshes the ListView (adapter with Jsonarray bindings)

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.