片段中的ListView单击时什么都不会发生



我的代码有什么问题?这是我在片段上的listView,以显示数据库。它已经成功查看数据库,但是当我单击时什么都不会发生。

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    db = new MyDatabase(getActivity());
    //View view = inflater.inflate(R.layout.fragment_event, container, false);

    View rootView = inflater.inflate(R.layout.fragment_event, container, false);
    //ArrayList<ListviewContactItem> listContact = GetlistContact();
    final ArrayList<event_constructor> arayList = new ArrayList<event_constructor>();
    //ListView lv = (ListView)getActivity().findViewById(R.id.lv_contact);
    eventListView = (ListView) rootView.findViewById(android.R.id.list);
    //lv.setAdapter(new ListviewContactAdapter(getActivity(), listContact));
    Log.d("EventNul", "Start");
    HashMap<String, String> map = new HashMap<String, String>();
    List<event_constructor> allEvents = db.getAllEvent();
    for (event_constructor event : allEvents) {
        arayList.add(new event_constructor(event.getId(), event.getEventName()));
    }
    // Don't forget to close database connection
    db.closeDB();
    ListAdapter eventAdapter = new ArrayListAdapter(getActivity(), arayList);
    eventListView.setAdapter(eventAdapter);
    eventListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // getting values from selected ListItem
            String gid = ((TextView) view.findViewById(R.id.noId)).getText()
                    .toString();
            String gname = ((TextView) view.findViewById(R.id.event)).getText()
                    .toString();
            Log.d("EventId", gid +". "+ gname);
            //Log.d("Item", ItemListId.toString());
            Intent intent = new Intent(getActivity(), NewPostActivity.class);
            intent.putExtra("idEvent", gid);
            startActivity(intent);
        }
    });
    return rootView;
}

如果列表的任何行项目包含可聚焦或可单击的视图,则OnItemClickListener将无法使用。

行项目必须具有类似android的参数:descendantFocusability =" blocksdescendants"。

在这里您可以看到示例,列表项目的外观。您的列表项目xml应该是... row_item.xml(your_xml_file.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" >
 // your other widgets here
</LinearLayout>

感谢您的关注,但我找不到解决方案。现在,我的解决方案是将" setOnitemClicklistener"移至" onstart()"。这是工作

    @Override
public void onStart() {
    super.onStart();
    getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> av, View v, int pos,
                                long id) {
            String gname = ((TextView) v.findViewById(R.id.event)).getText()
                    .toString();
            String gid = ((TextView) v.findViewById(R.id.noId)).getText()
                    .toString();
            Log.d("EventId", gid +". "+ gname);
            Integer i = Integer.parseInt(gid);
            Intent intent = new Intent(getActivity(), NewPostActivity.class);
            intent.putExtra("idEvent",i);
            startActivity(intent);
        }
    });
}

相关内容

  • 没有找到相关文章

最新更新