On 4 Lut, 21:07, saex <[email protected]> wrote:
> public class CustomExpandableListAdapter extends BaseExpandableListAdapter


package org.abc.def;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class ExpandableListActivity extends Activity implements
Callback {
        private final static String TAG = "ExpandableListActivity";

        private Handler mHandler;
        private Adapter mAdapter;
        private Random mRandom;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.elv);

                ExpandableListView elv = (ExpandableListView)
findViewById(R.id.elv);
                mHandler = new Handler(this);
                mAdapter = new Adapter(this, elv);
                elv.setAdapter(mAdapter);
                elv.expandGroup(0);
                elv.expandGroup(1);
                mRandom = new Random();
        }

        @Override
        protected void onResume() {
                super.onResume();
                mHandler.sendEmptyMessage(0);
        }

        @Override
        protected void onPause() {
                super.onPause();
                mHandler.removeMessages(0);
        }

        @Override
        public boolean handleMessage(Message msg) {
                mHandler.sendEmptyMessageDelayed(0, 500);
                int idx = mRandom.nextInt(mAdapter.getGroupCount());
                mAdapter.update(idx);
                return true;
        }

        static class Adapter extends BaseExpandableListAdapter {
                int[] mValues = new int[5];
                private ExpandableListView mElv;
                private LayoutInflater mInflater;

                public Adapter(Context context, ExpandableListView elv) {
                        mElv = elv;
                mInflater =
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                }

                public void update(int idx) {
                        mValues[idx]++;

                        int first = mElv.getFirstVisiblePosition();
                        int last = mElv.getLastVisiblePosition();
                        // TODO: maybe its better to use: cnt = 
mElv.getChildCount();
                        int cnt = last - first + 1;
                        Log.d(TAG, "update cnt: " + cnt + ", getChildCount(): " 
+
mElv.getChildCount());
                        String tag = Integer.toString(idx);
                        for (int i = 0; i < cnt; i++) {
                                View v = mElv.getChildAt(i);
                                if (tag.equals(v.getTag())) {
                                        getChildView(idx, 0, false, v, mElv);
                                        break;
                                }
                        }
                }

                @Override
                public int getGroupCount() {
                        return mValues.length;
                }

                @Override
                public int getChildrenCount(int groupPosition) {
                        return 1;
                }

                @Override
                public Object getGroup(int groupPosition) {
                        return mValues[groupPosition];
                }

                @Override
                public Object getChild(int groupPosition, int childPosition) {
                        return mValues[groupPosition];
                }

                @Override
                public long getGroupId(int groupPosition) {
                        return groupPosition;
                }

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

                @Override
                public boolean hasStableIds() {
                        return true;
                }

                @Override
                public View getGroupView(int groupPosition, boolean isExpanded, 
View
convertView, ViewGroup parent) {
                        View v = convertView != null? convertView :
mInflater.inflate(android.R.layout.simple_expandable_list_item_1,
parent, false);
                        TextView tv = (TextView) 
v.findViewById(android.R.id.text1);
                        tv.setText("Group #" + groupPosition);
                        v.setTag("");
                        return v;
                }

                @Override
                public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
                        View v = convertView != null? convertView :
mInflater.inflate(android.R.layout.simple_expandable_list_item_1,
parent, false);;
                        TextView tv = (TextView) 
v.findViewById(android.R.id.text1);
                        tv.setText(Integer.toString(mValues[groupPosition]));
                        v.setTag(Integer.toString(groupPosition));
                        return v;
                }

                @Override
                public boolean isChildSelectable(int groupPosition, int
childPosition) {
                        return false;
                }
        }
}


see update(int) method in my adapter, its called every half of second
and updates directly ExpandableListView

pskink

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to