Hello,

I declared (partial) success prematurely. Below you will find a copy
of:
1) getView
2) ViewHolder
3) layout xml file

I still have not been able to wire all buttons in the list.

I obtain the (incorrect) position of the button which is wired via the
statements

kill2.setTag(new Integer(position));
found after the if/else block

and

Integer itemPosition = (Integer) v.getTag();
found in the onClick block

note my unsuccessful attempts at retrieving the position of the
clicked button commented out in the onClick block:
                                        /*
                                        ViewHolder holder1 = new ViewHolder();
                                        holder1 = (ViewHolder) ((View) 
v.getParent
()).getTag();
                                        Integer itemPosition = holder1.pos;
                                                                */

                                        //Integer itemPosition=(Integer)((View)
v.getParent()).getTag();

Clearly, I am missing something pretty basic, yet I can't see what it
is. I tried to model my getView, viewHolder, and XML file after
List14.java in the Android sample APIs, and the sample code in the
Fancy Lists tutorial 4.

I would appreciate any comments/insights/suggestions.

Thanks.

Alex Donnini

        public View getView(int position, View convertView, ViewGroup
parent) {
            // A ViewHolder keeps references to children views to
avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;
            Button kill2;

            System.out.println("**************************position --
"+position);

            // When convertView is not null, we can reuse it directly,
there is no need
            // to reinflate it. We only inflate a new View when the
convertView supplied
            // by ListView is null.

            try
            {
                if (convertView == null)
                {

                        System.out.println("**************************view is
empty");

                    convertView = mInflater.inflate
(R.layout.list_item_icon_text, null);

                    holder = new ViewHolder();

                    kill2 =  (Button) findViewById(R.id.kill2);

                            View.OnClickListener k2 = new View.OnClickListener
()
                            {
                        public void onClick(View v)
{
                                try
                                {

                                        /*
                                        ViewHolder holder1 = new ViewHolder();
                                        holder1 = (ViewHolder) ((View) 
v.getParent
()).getTag();
                                        Integer itemPosition = holder1.pos;
                                                                */

                                        //Integer itemPosition=(Integer)((View)
v.getParent()).getTag();

                                        Integer itemPosition = (Integer) 
v.getTag();

                                                System.out.println
("**************************itemPosition - "+itemPosition);

                                                
checkMap(activityList.get(itemPosition));

                                                
//System.out.println("rebuilding list after item
was removed via new screen");

                                                fromMaptoList();

                                                
//System.out.println("displaying list after item
was removed");

                                                displayList();

                                                notifyDataSetChanged();

                                }
                                catch(Exception e)
                                {
                                        e.printStackTrace();
                                }
                                }
                };

                kill2.setOnClickListener(k2);

                holder.text = (TextView) convertView.findViewById
(R.id.text);
                holder.icon = (ImageView) convertView.findViewById
(R.id.icon);
                holder.button = (Button) convertView.findViewById
(R.id.kill2);
                holder.pos = position;

                convertView.setTag(holder);

               }
               else
               {

                        System.out.println("**************************list is
already populated");

                    holder = (ViewHolder) convertView.getTag();
                    kill2 =  (Button) findViewById(R.id.kill2);

               }

                    System.out.println("**************************beginning
item processing phase");

                    // Bind the data with the holder.

                        kill2.setTag(new Integer(position));

                    String item = (String)getItem(position);
                    holder.text.setText(item);
                    holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 :
mIcon2);
                    holder.pos = position;

                                
System.out.println("**************************holder.pos -
"+holder.pos);

            }
            catch(Exception e)
            {
                        e.printStackTrace();
            }

            return convertView;

        }


    //I do not use all fields in ViewHolder
    static class ViewHolder {
        TextView text;
        ImageView icon;
        ListView list;
        Button button;
        int pos;
    }



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
        android:id="@+id/layout"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="15sp"
    >

                <Button android:id="@+id/kill2"
        android:layout_gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/list_8_kill2"/>

     <ImageView android:id="@+id/icon"
        android:layout_width="48dip"
        android:layout_height="48dip" />

             <TextView android:id="@+id/text"
        android:layout_gravity="center_vertical"
        android:layout_width="0dip"
        android:layout_weight="1.0"
        android:layout_height="wrap_content"
        android:textSize="15sp"
         />

</LinearLayout>


On Nov 19, 9:44 am, Mark Murphy <[EMAIL PROTECTED]> wrote:
> for android wrote:
> >     Hey i have got a similar prb,but i dont exactly understand what u
> >     mean by "* When your button
>
> >     clicks, get the ViewHolder from the button's parent, get the position
> >     out of the ViewHolder, and use that position."
>
> I though it was fairly self-explanatory:
>
> -- Add an OnClickListener to your Button
> -- In the OnClickListener, call getParent() on the Button to get the
> LinearLayout that contains it
> -- Then call getTag() on the LinearLayout and cast it to be the ViewHolder
> -- Then get the position out of the ViewHolder
> -- Then use that position
>
> --
> Mark Murphy (a Commons Guy)http://commonsware.com
>
> Android Training on the Ranch! -- Mar 16-20, 
> 2009http://www.bignerdranch.com/schedule.shtml
--~--~---------~--~----~------------~-------~--~----~
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
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to