1

I have a problem with focus a ListView that contains an edittext. I use the method OnItemClickListener to select the row of the Listview, but when I click on the edit text it doesn't select the row.

I want that when I click on the edittext the row is selected thanks to the method OnItemClickListener, and after, the edittext takes focus to modify the text.

I'm losing very much time on this. Thanks.

this is the code.

list.xml

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="312dp"
    android:descendantFocusability="beforeDescendants">            
</ListView>

row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/record"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"

<TextView
    android:id="@+id/itemOra"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Ora"

 <TextView
    android:id="@+id/itemPrezzo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.8"
   android:focusable="false"
    android:paddingBottom="3dip"
      android:text="Prezzo"
    android:textColor="#4d4d4d"
    android:textSize="18dip"
       />

  <EditText
    android:id="@+id/itemMotivo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.8"
    android:text="Motivo"
    android:textColor="#4d4d4d"
    android:textSize="18dip">

activity.java

..........

public void attachItemListener(){       

    searchResults.setOnItemClickListener(new OnItemClickListener() {
        private Flusso corrente;

        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub

            //NOW EDITTEXT CAN TAKE THE FOCUS.

Adapter.java

            public class Adapter extends ArrayAdapter<Flusso> {
                public List<Flusso> flussi;

                public Adapter(Context context, int textViewResourceId,
                        List<Flusso> flussi) {
                    super(context, textViewResourceId, flussi);
                    this.flussi = flussi;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    return getView(position, convertView, parent, R.layout.row);
                }
                public View getView(int position, View convertView, ViewGroup parent, int layout) {
                    View v = convertView;
                    if (v == null) {
                        LayoutInflater vi = (LayoutInflater)    
                                this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        v = vi.inflate(layout, null);
                    }

                    Flusso flow = flussi.get(position);
                    if (flow != null) {
                        TextView prezzo = (TextView) v.findViewById(R.id.itemPrezzo);
                        EditText motivo = (EditText) v.findViewById(R.id.itemMotivo);

                        TextView ora = (TextView) v.findViewById(R.id.itemOra);

                        prezzo.setText(Float.toString(flow.get_euro()));
                        motivo.setText(flow.get_perche());

                    }
                    return v;
                }

            }

This is the code. I don't know if there are problem with focus or other attributer. Itried with the method onTouchEvent but it doesn't work.

I need your help. Thanks

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Marc
  • 77
  • 1
  • 9

2 Answers2

0

In order to do what you are talking about, you will have to override the onTouchEvent(MotionEvent) of the EditText to return false upon being touched. Something like this:

boolean onTouchEvent(MotionEvent e) {
    super.onTouchEvent(e);
    return false;
}

Note: This could be inconsistent as now both views will receive the MotionEvent. This is generally not a good thing.

What is happening is, your list view gets sent a MotionEvent from your contentView. The list view then says "Better check if any of my children want the MotionEvent before me" and it passes it to its children to see if they would like the cookies first. Then if there are any cookies left (all of the childrens' onTouchEvent(MotionEvent) calls return false) then the list view will eat the event and do its thing. The touch heirarchy in android is generally bottom to top.

ahodder
  • 11,353
  • 14
  • 71
  • 114
0

so..it's not that you know that your OnClickListener isn't responding, but that your EditText isn't taking focus once the listener has been activated, is that correct? Have you simply tried to request focus in your listener?

    searchResults.setOnItemClickListener(new OnItemClickListener() {
        private Flusso corrente;

        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            ......
            edittext.requestFocus();                                
        }
    });
mango
  • 5,577
  • 4
  • 29
  • 41