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 implements
		OnGroupClickListener {

	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[2][0] = "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 stub
		Log.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[0][0] = "111111";
		children[0][1] = "222222";
		children[1][0] = "333333";
		children[1][1] = "444444";
		children[2][0] = "555555";
		children[2][1] = "666666";
	}

	private int[] groups = { R.drawable.zgxq, R.drawable.wzq, R.drawable.ddz };
	public String[][] children = new String[3][2];

	// **************************************
	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 ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT, 30);
		ImageView view = new ImageView(GOS);
		view.setLayoutParams(lp);
		// Center the text vertically
		if (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 position
		view.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[groupPosition];
	}

	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[gos][cos]);
		return view;
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public Object getChild(int groupPosition, int childPosition) {
		return children[groupPosition][childPosition];
	}

	public int getChildrenCount(int groupPosition) {
		return children[groupPosition].length;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}

你可能感兴趣的:(android,OS)