[英]onItemClickListener not firing on custom ArrayAdapter
我有一个Activity
,它从Web服务中检索数据。 这个数据通过ArrayAdapter在ListView
显示,它通过内部三个TextViews
对RelativeLayout
进行膨胀,没什么特别的,它可以正常工作。
现在我想实现一个Details 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>
在过去的几个小时里一直在搞乱我,我似乎无法理解问题所在。 我的问题看起来非常类似于这个问题 ,但我没有扩展ListActivity,所以我仍然不知道我的onListClickItem()应该去哪里。
更新:现在我已经困惑了几天,仍然无法找到问题。
我应该重写活动吗,这次扩展ListActivity而不是Activity? 因为它本身提供了onItemClick方法,并且可能更容易覆盖。
或者,我应该直接在我的ArrayAdapter中的每个getView()中绑定一个侦听器吗? 我相信我已经读过这是不好的做法(我应该这样做,因为我尝试过并且在我的帖子中失败了)。
发现了错误 - 似乎是这个问题 。 向每个list_item.xml元素添加android:focusable="false"
解决了问题,现在使用原始代码触发onclick。
我遇到了同样的问题,并尝试了你的修复,但无法让它工作。 对我android:descendantFocusability="blocksDescendants"
是在你的情况下从项目布局xml
, list_item.xml
中将android:descendantFocusability="blocksDescendants"
到<RelativeLayout>
。 这允许onItemClick()
。
什么对我有用:
1)将android:descendantFocusability =“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)将list: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" />
一旦我遇到类似的问题。 每个列表项都有一个文本视图和一个复选框,只是因为复选框,整个listitem没有“ 启用 ”来触发事件。 我在获取视图时通过在适配器内部制作一个小技巧来解决它。
就在返回视图之前我说:
v.setOnClickListener(listener);
(对象listener
是我给Adapter
的构造函数的onItemClickListener
)。
但我必须告诉你,问题是因为平台,这是一个bug。
我有同样的问题,我试图通过添加解决它
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
到我的item.xml但它仍然无法正常工作! 事实上,我发现了相对布局中的问题
android:focusableInTouchMode="true" android:focusable="true"
当我删除它所有的东西都没问题
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
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.