I solved the problem building a custom view list adapter. I post the
code here with some comments so it can help any body else :


import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;




public class ItemListAdapter extends SimpleCursorAdapter
{
    private Context context;
    private int layout_id;


        // initialize the on click listeners. It's static so it's share with
all the buttons to avoid object creation

    // navigation with the arrow button
        private static final OnClickListener arrow_button_on_click_listener =
new OnClickListener() {

            public void onClick(View v) {

                // on click, get the id of the item, make it the current node
                // then refresh the view so we see its children

                InContext list = (InContext) v.getContext();
                list.setCurrentNode(v.getId());
                list.refreshView();

            }
        };

        // validation with the check box button

        // initialize the listener. It's static so it's share with all the
buttons to avoid object creation
        private static final OnClickListener check_box_on_click_listener =
new OnClickListener() {

            public void onClick(View v) {

                // on click, get the id of the item, make it the current node
                // then refresh the view so we see its children

                InContext list = (InContext) v.getContext();

                // this state is the contrary of the previous one
                int checked = 1;
                if (((CheckBox) v).isChecked())
                        checked = 0;

                list.toggleItem(v.getId(), checked);
            }
        };


    public ItemListAdapter(Context context, int layout_id, Cursor
c,String[] from, int[] to)
    {
        super(context, layout_id, c, from, to);
        this.context = context;
        this.layout_id = layout_id;
    }


    /**
     * Custom view translates columns into checkbox, title and arrow
     *
     * @see android.widget.CursorAdapter#getView(int,
android.view.View, android.view.ViewGroup)
     */
    public View getView(int position, View convertView, ViewGroup
parent)
    {

        // no need to manage the cursor, thanks to the
"simplecursoradapter", it will be on the right position
        Cursor cursor = getCursor();
        cursor.moveToPosition(position);

        // get the id of the item, so we can store it in each view
        int index = cursor.getColumnIndex(ItemDbHelper.KEY_ID);
        int _id = cursor.getInt(index);

        /* inflate the list row view from the XML so we can
instanciate it's child with findViewById
         if we don't do that, we'll get null from findViewById and so
a null pointer exception */

        // first we get the instance of the inflater that is pre
configured for this phone
        LayoutInflater inflater = LayoutInflater.from(this.context);
        // then, we inflate the view with it
        View v = inflater.inflate(this.layout_id,
                                                                parent,
                                                                false); // we 
don't want the layout to be bound to any
root for the moment, setAdapter() will do that



        // set the value of the item title of each row
        TextView item_title = (TextView) v.findViewById
(R.id.item_title);
        index = cursor.getColumnIndex(ItemDbHelper.KEY_TITLE);
        item_title.setText(cursor.getString(index));

        // set the status of the checkbox of each row
        CheckBox  item_checkbox = (CheckBox) v.findViewById
(R.id.row_checkbox);
        item_checkbox.setId(_id);
        index = cursor.getColumnIndex(ItemDbHelper.KEY_CHECKED);
        if (cursor.getInt(index) == 0)
                item_checkbox.setChecked(false);
        else
                item_checkbox.setChecked(true);

        // set the check box click listener to perform db update on
clic
        item_checkbox.setOnClickListener(check_box_on_click_listener);

        // set the image of the image button according to the number
of childrdb en
        ImageButton  arrow = (ImageButton) v.findViewById
(R.id.arrow_right);
        arrow.setId(_id);
        index = cursor.getColumnIndex
(ItemDbHelper.KEY_CHILDREN_COUNT);
        if (cursor.getInt(index) == 0)
                arrow.setImageResource(R.drawable.arrow);
        else
                arrow.setImageResource(R.drawable.double_arrow);

        // set the arrow button click listener to navigate in depth in
the node list
        arrow.setOnClickListener(arrow_button_on_click_listener);



        return v;
    }
}


On Apr 7, 8:56 pm, e-satis <info.ksam...@googlemail.com> wrote:
> My row layout is in a separate XML. It appears that findViewById only
> can find an inflated view.
>
> To solve that, I think I will need to create a custom ListAdapter that
> will bind an event to each button while inflating each view.
>
> If you see any better way, let me know.
>
> Cheers
>
> On 7 avr, 20:14,e-satis<info.ksam...@googlemail.com> wrote:
>
> > Update : setContentView first and before all, checked :-)
>
> > On 7 avr, 20:09,e-satis<info.ksam...@googlemail.com> wrote:
>
> > > Update : Reading other posts, I tried to trash the R.java file but it
> > > was not the cause.
>
> > > On 7 avr, 19:48,e-satis<info.ksam...@googlemail.com> wrote:
>
> > > > Hi,
>
> > > > My java code look like this :
>
> > > >         // right_arrow button
> > > >         ImageButton arrow_button = (ImageButton)findViewById
> > > > (R.id.arrow);
> > > >         arrow_button.setOnClickListener(new OnClickListener() {
> > > >             public voidonClick(View v) {
> > > >                  ((View)v.getParent().getParent()).setSelected(true);
> > > >             }
> > > >         });
>
> > > > And the corresponding XML look like :
>
> > > > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> > > > android"
> > > >     android:id="@+id/row_layout" >
>
> > > >     <LinearLayout android:id="@+id/row_checkbox_container">
>
> > > >         <CheckBox android:id="@+id/row_checkbox" />
>
> > > >     </LinearLayout>
>
> > > >    <LinearLayout android:id="@+id/item_title_container">
>
> > > >         <TextView android:id="@+id/item_title"/>
>
> > > >     </LinearLayout>
>
> > > >     <LinearLayout android:id="@+id/right_arrow_container" >
>
> > > >      <ImageButton android:id="@+id/arrow"
> > > >                     android:layout_width="18px"
> > > >                     android:layout_height="fill_parent"
> > > >                     android:layout_gravity="bottom"
> > > >                     android:layout_weight="1"
> > > >                     android:src="@drawable/arrow"
> > > >                     android:background="#00000000"/>
>
> > > >     </LinearLayout>
>
> > > > </LinearLayout>
>
> > > > I got anullpointerexceptionright after running the app in the
> > > > emulator from eclipse on Ubuntu 8.10 :
>
> > > > E/AndroidRuntime( 1426): java.lang.RuntimeException: Unable to start
> > > > activity ComponentInfo{com.getincontext/com.getincontext.InContext}:
> > > > java.lang.NullPointerException
> > > > E/AndroidRuntime( 1426):        at
> > > > android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
> > > > 2141)
> > > > E/AndroidRuntime( 1426):        at
> > > > android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
> > > > 2157)
> > > > E/AndroidRuntime( 1426):        at 
> > > > android.app.ActivityThread.access$1800
> > > > (ActivityThread.java:112)
> > > > E/AndroidRuntime( 1426):        at 
> > > > android.app.ActivityThread$H.handleMessage
> > > > (ActivityThread.java:1581)
> > > > E/AndroidRuntime( 1426):        at android.os.Handler.dispatchMessage
> > > > (Handler.java:88)
> > > > E/AndroidRuntime( 1426):        at 
> > > > android.os.Looper.loop(Looper.java:123)
> > > > E/AndroidRuntime( 1426):        at android.app.ActivityThread.main
> > > > (ActivityThread.java:3739)
> > > > E/AndroidRuntime( 1426):        at java.lang.reflect.Method.invokeNative
> > > > (Native Method)
> > > > E/AndroidRuntime( 1426):        at java.lang.reflect.Method.invoke
> > > > (Method.java:515)
> > > > E/AndroidRuntime( 1426):        at com.android.internal.os.ZygoteInit
> > > > $MethodAndArgsCaller.run(ZygoteInit.java:739)
> > > > E/AndroidRuntime( 1426):        at 
> > > > com.android.internal.os.ZygoteInit.main
> > > > (ZygoteInit.java:497)
> > > > E/AndroidRuntime( 1426):        at dalvik.system.NativeStart.main(Native
> > > > Method)
> > > > E/AndroidRuntime( 1426): Caused by: java.lang.NullPointerException
> > > > E/AndroidRuntime( 1426):        at com.getincontext.InContext.onCreate
> > > > (InContext.java:51)
> > > > E/AndroidRuntime( 1426):        at
> > > > android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
> > > > 1122)
> > > > E/AndroidRuntime( 1426):        at
> > > > android.app.ActivityThread.performLaunchActiv
>
> > > > I searched in the other posts something similar, and It seems that it
> > > > must be because the ImageButton is set tonull.
>
> > > > Unfortunately I can't find anything that would lead to it in the XML
> > > > or the Java code. The button is displayed properly and eclipse does
> > > > not detect any error. I guess this is runtime.
>
> > > > I tried to cast from ImageButton to button but it doesn't change
> > > > anything. So I image the  trouble is from findViewById(), but arrow to
> > > > exists, is a regular pgn file and is displayed by the emulator if this
> > > > line is commented.
>
> > > > The weirdest is that I got a similar statement just above, and I can't
> > > > see any difference.
>
> > > > Any clue ?
--~--~---------~--~----~------------~-------~--~----~
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