ExpandListView自动更新(以前发到eoe,现在整理下)
1.直接上界面代码:package com.example;import java.util.Timer;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.View;import android.view.WindowManager;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnGroupClickListener;public class ExpandableListView1 extends Activity implementsOnGroupClickListener {private MyAdapter adapter;private ExpandableListView expandableList;private Timer timer_m;private Handler mRedrawHandler = new Handler();/** Called when the activity is first created. */public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);adapter = new MyAdapter(this);expandableList = (ExpandableListView) findViewById(R.id.list);// expandableList.setBackgroundDrawable(R.drawable.icon);expandableList.setAdapter(adapter);expandableList.setGroupIndicator(null);expandableList.setDivider(null);expandableList.setOnGroupClickListener(this);timer_m = new Timer();timer_m.schedule(new MyTask2(), 5000, 10000);}class MyTask2 extends java.util.TimerTask {public void run() {adapter.children = "seven";Runnable updater = new Runnable() {public void run() {adapter.notifyDataSetChanged();}};mRedrawHandler.post(updater);timer_m.cancel();}}public boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {// TODO Auto-generated method stubLog.i("", "parent.getId() =" + parent.getId());switch (parent.getId()) {case R.id.list:Log.i("", "groupPosition =" + groupPosition);break;default:break;}return false;}}
2.适配器:
package com.example;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.ImageView;import android.widget.TextView;public class MyAdapter extends BaseExpandableListAdapter {private ExpandableListView1 GOS;public MyAdapter(ExpandableListView1 _GOS) {super();GOS = _GOS;children = "111111";children = "222222";children = "333333";children = "444444";children = "555555";children = "666666";}private int[] groups = { R.drawable.zgxq, R.drawable.wzq, R.drawable.ddz };public String[][] children = new String;// **************************************public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {ImageView view = getGenericView(groupPosition);return view;}public ImageView getGenericView(int pos) {// Layout parameters for the ExpandableListViewAbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 30);ImageView view = new ImageView(GOS);view.setLayoutParams(lp);// Center the text verticallyif (pos == 0) {view.setImageResource(R.drawable.zgxq);}if (pos == 1) {view.setImageResource(R.drawable.wzq);}if (pos == 2) {view.setImageResource(R.drawable.ddz);}// Set the text starting positionview.setPadding(0, 5, 0, 0);return view;}public long getGroupId(int groupPosition) {return groupPosition;}public Object getGroup(int groupPosition) {Log.i("", "getGroup =" + groupPosition);return groups;}public int getGroupCount() {return groups.length;}public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {TextView view = getChildTextView(groupPosition, childPosition);return view;}public TextView getChildTextView(int gos, int cos) {TextView view = new TextView(GOS);view.setText(children);return view;}public long getChildId(int groupPosition, int childPosition) {return childPosition;}public Object getChild(int groupPosition, int childPosition) {return children;}public int getChildrenCount(int groupPosition) {return children.length;}public boolean hasStableIds() {return true;}public boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}
页:
[1]