[Android] dynamic addition of data in ListView

Source: Internet
Author: User

Design Concept: append data records to the list placed in the adapter, and then use the notifyDataSetChanged () refresh of the adapter.

In this example.

Package com. app. test01; import java. util. arrayList; import java. util. hashMap; import java. util. list; import org. json. JSONArray; import org. json. JSONException; import org. json. JSONObject; import android. r. integer; import android. app. activity; import android. OS. bundle; import android. view. contextMenu; import android. view. layoutInflater; import android. view. menuItem; import android. view. view; import android. view. contextMenu. contextMenuInfo; import android. widget. absListView; import android. widget. absListView. onScrollListener; import android. widget. adapterView; import android. widget. adapterView. adapterContextMenuInfo; import android. widget. baseAdapter; import android. widget. listView; import android. widget. textView; import android. widget. toast; import com. app. adapter. myWeixinJSON; import com. app. adapter. myWeixinList;/*** click the data appended ListView * @ author 402-9 **/public class ListViewPage extends Activity {private ListView lv; private BaseAdapter mJson; private JSONArray mData = new JSONArray (); // JSON data source private View view_page_footer; // bottom View private int num = 1; // load data count private int count = 50; // total data // private boolean flag; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. weixin); lv = (ListView) findViewById (R. id. lv); getJSONArray (mData); mJson = new MyWeixinJSON (mData, this); view_page_footer = LayoutInflater. from (this ). inflate (R. layout. view_page_footer, null); lv. addFooterView (view_page_footer); // Add the bottom view TextView text_page = (TextView) view_page_footer.findViewById (R. id. text_page); text_page.setOnClickListener (new View. onClickListener () {// click the button to append data and notify the adapter @ Overridepublic void onClick (View v) {// TODO Auto-generated method stubTextView TV = (TextView) v; TV. setText ("loading... "); getJSONArray (mData); TV. setText ("next page"); mJson. notifyDataSetChanged () ;}}); lv. setAdapter (mJson); // bind the adapter}/** data source JSONArray */private void getJSONArray (JSONArray jArray) {try {for (int I = 1; I <= 5; I ++) {JSONObject jsonObject = new JSONObject (); jsonObject. put ("title", "name" + num ++); jsonObject. put ("time", "September 29"); jsonObject. put ("info", "I have passed your friend verification request, now we can start to talk"); jsonObject. put ("img", R. drawable. special_spring_head2); jArray. put (jsonObject); if (num = count) {lv. removeFooterView (view_page_footer); Toast. makeText (this, "No more data... ", Toast. LENGTH_LONG ). show () ;}} catch (Exception e) {// TODO: handle exception }}}

The added bottom view has only one button for clicking append:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "vertical" android: padding = "5dp"> <TextView android: id = "@ + id/text_page" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "next page" android: gravity = "center"/> </LinearLayout>



Click "Next page" to append data to the ListView.

After the append is complete, clear the bottom view.


Related Article

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.