This article mainly discusses the use of the custom adapter and Its notifyDataSetChanged () method (the listener without listView ):
First look at the effect: (during the entire operation, the current Activity is not pause or stop. The figure captured by the pea pod is really big... speechless)
1. Initialization status, 20 dataitems in total
2, 15 seconds later, slide down and add a text100 item:
3. Click "add" and slide down to add a text200 item (a text200 item is added each time the Add button is clicked ):
4. Click the delete button to slide up and down (one item in listView is reduced every time ):
The code is not complete, with brief comments, and optimization is not considered
Adapter. xml:
<ListView
Android: id = "@ + id/listview1"
Android: layout_width = "fill_parent"
Android: layout_height = "0dp"
Android: layout_weight = "1"
Android: cacheColorHint = "#00000000" type = "codeph" text = "/codeph"/>
<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal"
>
<Button
Android: id = "@ + id/button_add"
Android: layout_width = "0dp"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: text = "add">
</Button>
<Button
Android: id = "@ + id/button_delete"
Android: layout_width = "0dp"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: text = "delete">
</Button>
</LinearLayout>
<ListView/> attributes in the tag: android: layout_height = "0dp" and android: layout_weight = "1" ensure that the Android platform calculates other elements (linearLayout) during layout) to calculate the width and height of the current listView. Because the default value of android: layout_weight = "XXX" is 0, the priority of 1 is lower than that of 0 (if the entire xml is RelativeLayout layout, it's easy. You can set direct attributes)
The android: layout_weight = "1" and android: layout_width = "0dp" attributes of the two <Button/> labels make their parent elements evenly allocated space during layout, if an interval is set in <Button/>, for example, android: layout_marginLeft = "xxxdp", the parent element first deducts the interval xxxdp, and the remaining space is still evenly allocated to two button buttons.
Adapter_item.xml:
<TextView
Android: id = "@ + id/textview1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textColor = "# FF0000"
Android: textSize = "25dp"/>
<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginLeft = "150dp"
Android: focusable = "false"/>
Activity AdapterActivity:
Public class AdapterActivity extends Activity {
Private ListView listView;
Private List <HashMap <String, String> data;
Private ListViewAdapter adapter;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. adapter );
ListView = (ListView) findViewById (R. id. listview1 );
Button addButton = (Button) findViewById (R. id. button_add );
Button deleteButton = (Button) findViewById (R. id. button_delete );
ButtonListener listener = new ButtonListener ();
AddButton. setOnClickListener (listener );
DeleteButton. setOnClickListener (listener );
This. initListView ();
Timer timer = new Timer ();
Timer. schedule (new TimerTask () {// Add a data entry to the data set after 15 seconds
@ Override
Public void run (){
HashMap <String, String> map = new HashMap <String, String> ();
Map. put ("text", "text100 ");
Map. put ("buttonText", "buttonText100 ");
Data. add (map );
Log. I (Constant. TAG, "data added successfully ");
// Adapter. notifyDataSetChanged (); Non-UI thread Error
Message msg = new Message ();
Msg. what = 1;
Handler. sendMessage (msg );
}
},15000 );
}
Private Handler handler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
// Super. handleMessage (msg );
Switch (msg. what ){
Case 1:
// Notify the observer by calling this method in the UI thread (there is an observer in the source code about the adapter, which is not discussed in detail !) The adapter data has changed. Refresh the view.
Adapter. notifyDataSetChanged ();
// Adapter. notifyDataSetInvalidated (); //
// The results are the same. Except for different comments in the source code, the executed code is the same and is not further explored.
// ListView. postInvalidate (); invalid refresh
Break;
}
}
};
Private void initListView (){
Data = ViewApp. getData (); // ViewApp is a global class. When the program is running, data is loaded. This is only assigned to www.2cto.com.
Adapter = new ListViewAdapter (this, data, R. layout. adapter_item );
SimpleAdapter simpleAdapter = new SimpleAdapter (this, data,
R. layout. adapter_item, new String [] {"text", "buttonText "},
New int [] {R. id. textview1, R. id. button1 });
ListView. setAdapter (adapter );
}
Class ListViewAdapter extends BaseAdapter {
Private List <HashMap <String, String> data;
Private int resource;
Private LayoutInflater inflater;
Private HashMap <String, String> itemData;
Public ListViewAdapter (Context context,
List <HashMap <String, String> data, int resource ){
// Super (context, data, resource, from, );
This. data = data;
This. resource = resource;
Inflater = (LayoutInflater) getSystemService (Context. LAYOUT_INFLATER_SERVICE );
}
@ Override
Public int getCount (){
Return data. size ();
}
@ Override
Public Object getItem (int position ){
Return null;
}
@ Override
Public long getItemId (int position ){
Return 0;
}
// This method is called to render the data of each item and display the data by swiping up or down.
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
If (null = convertView ){
ConvertView = inflater. inflate (resource, null );
}
// ConvertView. setTag ("abc ");
ItemData = data. get (position );
TextView textView = (TextView) convertView
. FindViewById (R. id. textview1 );
TextView. setText (itemData. get ("text "));
Final Button button = (Button) convertView
. FindViewById (R. id. button1 );
Button. setText (itemData. get ("buttonText "));
Button. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
Toast. makeText (AdapterActivity. this, button. getText (),
Toast. LENGTH_SHORT). show ();
}
});
Return convertView;
}
}
Class ButtonListener implements View. OnClickListener {
@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. button_add:
HashMap <String, String> map = new HashMap <String, String> ();
Map. put ("text", "text200 ");
Map. put ("buttonText", "buttonText200 ");
Data. add (map );
Log. I (Constant. TAG, "ADD ");
Break;
Case R. id. button_delete:
Log. I (Constant. TAG, "DELETE ");
Data. remove (1 );
Break;
}
Adapter. notifyDataSetChanged (); // refresh the view after the adapter changes
}
}
}
From my remarks _ Mo