【发布时间】:2011-07-28 10:13:38
【问题描述】:
我有一个Activity,它从 Web 服务中检索数据。该数据通过 ArrayAdapter 以 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>
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() 应该去哪里。
更新:现在我已经为此困惑了几天,但仍然找不到问题所在。
我是否应该重写 Activity,这次扩展 ListActivity 而不是 Activity?因为它本身提供了 onItemClick 方法,而且可能更容易覆盖。
或者,我应该在我的 ArrayAdapter 的每个 getView() 中直接绑定一个侦听器吗?我相信我已经读过这是不好的做法(我应该按照我在帖子中尝试但失败的方式去做)。
【问题讨论】:
-
我无法弄清楚这两件事都没有帮助我:stackoverflow.com/questions/18237218/…
标签: android listview android-arrayadapter