我是Android开发的新手,所以可能是我在这里缺少一些基本的东西,但经过近两天的谷歌搜索后,我仍然无法弄清楚原因。
我已按照以下article创建了我的项目,只是在点击按钮时我从其他活动调用此活动。
我还创建了自己的列表采用者public class MyListAdapter extends ArrayAdapter<MyData>
,并且在此适配器的getView()
方法中,我实际上提供了如下所示的不同表示。
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.my_list_view, parent, false);
}
这一切正常我能够绑定数据并在列表上显示结果而没有任何问题。现在,我想在用户点击列表项时显示所选MyData
的详细信息,但无论我尝试什么,这都不起作用。
我确保我的活动正在实施如下的回调界面:
public class MyItemListActivity extends FragmentActivity
implements MyItemListFragment.Callbacks
并且回调本身也非常简单:
public interface Callbacks {
public void onItemSelected(MyData item);
}
同样在我的MyItemListFragment
onAttched
已正确连接mCallbacks = (Callbacks) activity;
我真正注意到的是当我点击列表项时没有调用以下函数:
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
MyData data = DataContent.ITEMS.get(position);
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(data);
}
我显示列表项的片段如下所示:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myitem_list"
android:name="path.MyItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context="path.MyItemListActivity"
tools:layout="@android:layout/list_content" />
,实际的自定义适配器视图my_list_view
如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >....</RelativeLayout>
我可以看到它在这里缺少android:onClick
标签,但不确定是否是原因以及谁需要实现此处理程序,因为此布局上没有直接上下文。
非常感谢任何帮助。
此致 基兰