onItemClickListener не запускает пользовательский ArrayAdapter - PullRequest
21 голосов
/ 03 апреля 2011

У меня есть Activity, который извлекает данные из веб-службы.Эти данные представлены в ListView через ArrayAdapter, который раздувает RelativeLayout с тремя TextViews внутри, ничего сложного, и все работает нормально.

Теперь я хочу реализовать детали Activity, которые должнывызываться, когда пользователь щелкает элемент в ListView, звучит просто, но я не могу на всю жизнь заставить onItemClickListener работать с моим ArrayAdapter.

Это мой основной Activity:

public class Schema extends Activity {

    private ArrayList<Lesson> lessons = new ArrayList<Lesson>();
    private static final String TAG = "Schema";
    ListView lstLessons;
    Integer lessonId;

    // called when the activity is first created.
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // can we use the custom titlebar?
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        // set the view
        setContentView(R.layout.main);

        // set the title
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);

        // listview called lstLessons
        lstLessons = (ListView)findViewById(R.id.lstLessons);

        // load the schema
        new loadSchema().execute();

        // set the click listeners
        lstLessons.setOnItemClickListener(selectLesson);      

    }// onCreate

// declare an OnItemClickListener for the AdapterArray (this doesn't work)
private OnItemClickListener selectLesson = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int i, long l) {  
        Log.v(TAG, "onItemClick fired!");
    }                
};

private class loadSchema extends AsyncTask<Void, Void, Void> {

    private ProgressDialog progressDialog; 

    // ui calling possible
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(Schema.this,"", "Please wait...", true);
    }

    // no ui from this one
    @Override
    protected Void doInBackground(Void... arg0) {
    // get some JSON, this works fine
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
        // apply to list adapter
        lstLessons.setAdapter(new LessonListAdapter(Schema.this, R.layout.list_item, lessons));        
    }

Код моего ArrayAdapter:

// custom ArrayAdapter for Lessons 
private class LessonListAdapter extends ArrayAdapter<Lesson> {
    private ArrayList<Lesson> lessons;

    public LessonListAdapter(Context context, int textViewResourceId, ArrayList<Lesson> items) {
        super(context, textViewResourceId, items);
        this.lessons = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_item, null);
        }
        Lesson o = lessons.get(position);

        TextView tt = (TextView) v.findViewById(R.id.titletext);
        TextView bt = (TextView) v.findViewById(R.id.timestarttext);
        TextView rt = (TextView) v.findViewById(R.id.roomtext);

        v.setClickable(true);
        v.setFocusable(true);

        tt.setText(o.title);
        bt.setText(o.fmt_time_start);
        rt.setText(o.room);
        return v;
    }
}// LessonListAdapter

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:screenOrientation="portrait"
    >

    <!-- student name -->
    <TextView 
      android:id="@+id/schema_view_student"
      android:text="Name"  android:padding="4dip"
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
      android:gravity="center_vertical|center_horizontal"
      style="@style/schema_view_student"
    />

    <!-- date for schema -->    
    <TextView
      android:id="@+id/schema_view_title"   
      android:layout_height="wrap_content"
      android:layout_margin="0dip" 
      style="@style/schema_view_day"
      android:gravity="center_vertical|center_horizontal"
      android:layout_below="@+id/schema_view_student"         
      android:text="Date" android:padding="6dip"
      android:layout_width="fill_parent"
    />

    <!-- horizontal line -->
    <View
      android:layout_width="fill_parent"
      android:layout_height="1dip"
      android:background="#55000000"
      android:layout_below="@+id/schema_view_title"
    />

    <!--  list of lessons -->
    <ListView
      android:id="@+id/lstLessons" 
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
      android:layout_below="@+id/schema_view_title"
    />

</RelativeLayout>

The list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60px"
    android:padding="12dip">

    <TextView
        android:id="@+id/timestarttext"
        android:text="09:45"
        style="@style/LessonTimeStartText"

        android:layout_width="60dip"

        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"

        android:layout_height="fill_parent" android:gravity="center_vertical|right" android:paddingRight="6dip"/>

    <TextView
        android:id="@+id/titletext"
        android:text="Test"
        style="@style/LessonTitleText"

        android:layout_width="wrap_content"
        android:layout_height="fill_parent"

        android:layout_toRightOf="@+id/timestarttext"

        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" android:gravity="center_vertical|center_horizontal"/>

    <TextView
        android:id="@+id/roomtext"
        android:text="123"

        android:layout_width="wrap_content"
        android:layout_height="fill_parent"

        style="@style/LessonRoomText"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical" />

</RelativeLayout>

Возиться с этим дляпоследние пару часов, и я не могу понять, в чем проблема.Моя проблема выглядит очень похоже на этот вопрос , но я не расширяю ListActivity, поэтому я до сих пор не знаю, куда должен идти мой onListClickItem ().

UPDATE: Теперь я несколько дней ломал голову над этим и до сих пор не могу найти проблему.

Должен ли я переписать действие, на этот раз расширяя ListActivity вместо Activity?Поскольку он предоставляет сам метод onItemClick и, вероятно, его проще перезаписать.

Или я должен связывать слушателя непосредственно в каждом getView () в моем ArrayAdapter?Я считаю, что я прочитал, что это плохая практика (я должен делать то, что пытался и потерпел неудачу в своем посте).

Ответы [ 6 ]

51 голосов
/ 06 апреля 2011

Обнаружена ошибка - похоже, эта проблема . Добавление android:focusable="false" к каждому из элементов list_item.xml решило проблему, и теперь щелчок мыши запускается с исходным кодом.

34 голосов
/ 06 апреля 2012

Я столкнулся с той же проблемой и попробовал ваше исправление, но не смог заставить его работать.Для меня сработало добавление android:descendantFocusability="blocksDescendants" к <RelativeLayout> из макета элемента xml, list_item.xml в вашем случае.Это позволяет вызывать onItemClick().

9 голосов
/ 17 февраля 2013

Что сработало для меня:

1) Добавление android: downndantFocusability = "blocksDescendants" в тег Relative Layout.Результат показан ниже:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants" >

2) Добавление android: focusable = "false" к каждому элементу в примере list_item.xml:

<TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"     
        android:text="TextView"
        android:focusable="false" />
5 голосов
/ 14 апреля 2011

Однажды у меня была похожая проблема. Каждый элемент списка имел текстовое представление и флажок, и только потому, что флажок, весь элемент списка не был « включен » для запуска события. Я решил это, сделав небольшой трюк внутри адаптера, когда получал изображение.

Непосредственно перед возвратом вида я поставил:

v.setOnClickListener(listener);

(Объект listener - это onItemClickListener, который я дал конструктору Adapter).

Но я должен сказать вам, что проблема в том, что платформа, это ошибка.

1 голос
/ 11 июля 2014

У меня была такая же проблема, и я попытался ее решить, добавив

android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"

в мой item.xml, но она все еще не работает !!!Фактически я обнаружил, что проблема в относительной раскладке содержит

 android:focusableInTouchMode="true" android:focusable="true"

И когда я ее удалил, все в порядке

0 голосов
/ 03 апреля 2011
protected void onPostExecute(Void result) { 
     progressDialog.dismiss();  stLessons.setAdapter(new LessonListAdapter(Schema.this, R.layout.list_item, lessons)); 
    //add this  
ListView lv = getListView();  lv.setOnItemClickListener(new ListView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int i, long l) {
                    //do stuff
                }
            });
     }
...