GridView - setting selection to last item also loads first item

by Mark Murphy » Mon, 19 Jul 2010 22:54:36 GMT

Sponsored Links

setTag() is 1.0+. Indexed setTag() is 1.6+.

Mark Murphy (a Commons Guy)  |  | 

_The Busy Coder's Guide to Android Development_ Version 3.1 Available!


Other Threads

1. Custom ListView with ItemClick listener & notifyDataSetChanged

I tried to have a customed listview with the itemclick listener. I
followed with some small modifications the tutorials from
If I put in comment "adapter.notifyDataSetChanged();", all work well:
when I click on an item, the listener is fired. But if I use
"adapter.notifyDataSetChanged();" (I need it because my list will be
constructed dynamically), no listener is fired when I click on any
Does someone have an idea about the error I made ? Or shall I use
another way to do what I want ?

Thanks !

Here are my codes:
public class CustomListViewOnClick extends Activity {
        private CustomListView l1;

        public void onCreate(Bundle savedInstanceState) {
                l1 = new CustomListView(this);

public class CustomListView extends ListView {
        private static Context context;
        private EfficientAdapter adapter;

        private class EfficientAdapter extends BaseAdapter {
                private LayoutInflater mInflater;

                public EfficientAdapter(Context context, int 
textViewResourceId) {
                        mInflater = LayoutInflater.from(context);


                public int getCount() {
                        return country.length;

                public Object getItem(int position) {
                        return position;

                public long getItemId(int position) {
                        return position;

                public View getView(int position, View convertView, ViewGroup
parent) {
                        ViewHolder holder;
                        if (convertView == null) {
                                convertView = 
mInflater.inflate(R.layout.listview, null);
                                holder = new ViewHolder();
                                holder.text = (TextView) convertView
                                holder.text2 = (TextView) convertView

                        } else {
                                holder = (ViewHolder) convertView.getTag();


                        return convertView;

                class ViewHolder {
                        TextView text;
                        TextView text2;

        public CustomListView(Context context) {
                CustomListView.context = context;
                adapter = new EfficientAdapter(context, R.layout.listview);
                setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView<?> a, View v, int 
                                        long id) {
                                AlertDialog.Builder adb;
                                adb = new 
                                adb.setMessage("Selected Item is = "
                                                + getItemAtPosition(position));
                                adb.setPositiveButton("Ok", null);

        protected void onDraw(Canvas canvas) {

        private static final String[] country = { "Iceland", "India",
                        "Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", 
                        "Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania",
                        "Luxembourg" };
        private static final String[] curr = { "ISK", "INR", "IDR", "IRR",
                        "EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ", 
"LRD", "LYD",
                        "LTL ", "EUR"



2. Is there an Android view/layout/widget that allows flinging/swiping between included views?

I'm looking for a layout that includes different views and allows
switching between them, but unlike the tab view which switches
instantly and uses a large amount of screen real estate, allows the
user to fling/swipe between the views (ideally with small images
indicating how many views are available and which one is the current
one like, forgive me, iPhone apps display).

The HorizontalScrollView view allows flinging, but does not have a
separation between screens.

If there isn't a standard component that does this, is there another
that is often used for this purpose?

I realize I could implement this myself without too much trouble using
gestures, but I'd like to avoid reinventing the wheel (if, that is,
it's already been invented in this case) and defining my own
thresholds for the fling action means my app and other apps that try
to do the same thing do not provide a consistent user experience.


3. Restlet api

4. Ym bermasalah

5. adb causing 100% cpu usage

6. Tegra 2 / GTablet does not expose reqGlEsVersion correctly...

7. Google Checkout Change - delayed bulk payments.