In my application, I want to implement a ListView, of each itemview is
a LinearLayout with two focusable buttons in it horziontally, and each
row item data loads from server in page. But it seems that when
adapter.notifyDataSetChanged is invoked, if the focus is on the right
button of the itemview, it will jump to the left button. Any way I can
make the focus stable ?

Following is my sample code to reproduce the problem. Any one can help
give some suggestion? Thanks.

package com.listviewfocus;

import java.util.Vector;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

public class SimpleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ListView list = new ListView(this);
        final MyAdapter adapter = new MyAdapter();

        list.setItemsCanFocus(true);
        list.setAdapter(adapter);

        setContentView(list);

        final Handler handler = new Handler();

        new Thread(new Runnable(){
            public void run() {
                while(true) {
                    handler.post(new Runnable() {
                        public void run() {
                            adapter.addItem(System.currentTimeMillis()
+"");
                        }
                    });

                    try {
                        Thread.currentThread().sleep(5000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    class MyAdapter extends BaseAdapter {

        private Vector<String> items = new Vector<String>();

        public MyAdapter() {

        }

        public void addItem(String s) {
            items.add(s);
            this.notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup
parent) {

            LinearLayout view;

            if (convertView instanceof LinearLayout) {
                view = (LinearLayout) convertView;
            } else {
                view = new LinearLayout(parent.getContext());
                Button b1 = new Button(parent.getContext());
                Button b2 = new Button(parent.getContext());
                view.addView(b1);
                view.addView(b2);
            }

            ((Button)(((ViewGroup)
view).getChildAt(0))).setText(position+":b1");
            ((Button)(((ViewGroup)
view).getChildAt(1))).setText(position+":b2");

            return view;
        }

    }

}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to