2011-04-02 50 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" 

当我删除了所有的事情是确定的

相关问题