2011-04-02 80 views
20

我有一個Activity從Web服務檢索數據。該數據通過ArrayAdapter在ListView中呈現,該數字將RelativeLayout與三個TextViews一起膨脹,沒有任何花哨,並且工作正常。onItemClickListener未觸發自定義ArrayAdapter

現在我想實現一個細節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> 

的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> 

在過去的幾個小時裏一直在搞這個,我似乎無法解決問題所在。我的問題看起來與this question非常相似,但我沒有擴展ListActivity,所以我仍然不知道我的onListClickItem()應該放在哪裏。

更新:現在我已經困惑了這幾天,仍然無法找到問題。

我應該重寫活動,這次擴展ListActivity而不是活動?因爲它本身提供onItemClick方法,可能更容易覆蓋。或者,我應該直接在我的ArrayAdapter中的每個getView()中綁定一個監聽器嗎?我相信我已經讀過這是不好的做法(我應該做的,因爲我嘗試過,並在我的帖子失敗)。

+0

我無法弄清楚這些都不兩件事情讓我: HTTP://計算器.com/questions/18237218/android-listview-onitemclicklistener-being-blocked-by-progressbar – 2013-08-14 16:29:19

回答

48

找到了這個bug - 它似乎是this issue。爲每個list_item.xml元素添加android:focusable="false"解決了該問題,並且現在使用原始代碼觸發onclick。

+4

花了我一個多小時才找到您的解決方案(哪些工作)。 Android平臺可以非常出色,但這是完全失敗的一部分。 – 2011-05-21 00:12:09

+0

工作很奇怪,這是一個錯誤。 – 2016-02-24 04:25:42

+0

似乎跟蹤球/ dpad問題。但是,謝謝,這個解決了這個問題。 – Meet 2016-07-25 06:57:45

0
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 
       } 
      }); 
    } 
+0

「方法getListView()對於Schema.loadSchema類型是未定義的」 - 不會像lstLessons一樣(lstLessons是ListView)? – cvaldemar 2011-04-03 12:00:36

+0

我認爲這可能是因爲架構不擴展ListActivity。 – cvaldemar 2011-04-03 12:11:08

4

曾經有類似的問題。每個列表項都有一個文本視圖和一個複選框,僅僅因爲複選框,整個列表項不是'啓用'來觸發事件。當我獲得視圖時,我通過在適配器內部製作一個小技巧來解決它。

剛回國的觀點之前,我提出:

v.setOnClickListener(listener); 

(對象listeneronItemClickListener我給了Adapter的構造函數)。

但是我得告訴你,問題是因爲這個平臺,它是一個bug。

+0

這對我有用。 – jsDebugger 2012-09-16 19:01:33

33

我遇到了同樣的問題,並嘗試了您的修復,但無法讓它工作。什麼對我來說是android:descendantFocusability="blocksDescendants"<RelativeLayout>從項目佈局xml,list_item.xml你的情況。這允許調用onItemClick()

+3

android:focusable =「false」和android:desendantFocusability =「blocksDescendants」都不適合我。 – jsDebugger 2012-09-16 19:01:05

+4

如果有'android:clickable =「true」'將其刪除。 – proxylittle 2012-12-06 17:38:27

+0

你救了我的一天.... – Deepak 2012-12-11 17:07:44

7

什麼工作對我來說:

1)添加機器人:descendantFocusability = 「blocksDescendants」 到相對佈局標籤。 的結果如下所示:

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

2)添加機器人:在list_item.xml可聚焦=「假」的每一個元件在 例如:

<TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"  
     android:text="TextView" 
     android:focusable="false" /> 
+0

向單元佈局的父級添加android:descendantFocusability =「blocksDescendants」爲我工作 – MbaiMburu 2018-01-19 12:53:11

1

我有同樣的問題,我試圖解決它加入

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

到我的item.xml,但它仍然無法正常工作!逸岸,我發現在相對佈局女巫問題包含

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

當我刪除了所有的事情是確定的

相關問題