列表中的ListView在单击时没有任何反应

时间:2017-02-16 10:30:47

标签: android database listview fragment clickable

我的代码有什么问题?这是我的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;

}

2 个答案:

答案 0 :(得分:0)

如果列表中的任何行项目包含可聚焦或可点击的视图,则OnItemClickListener将无法正常工作。

行项必须有一个像android:descendantFocusability =&#34; blocksDescendants&#34;这样的参数。

在这里你可以看到示例,你的列表项应该如何。您的列表项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>

答案 1 :(得分:0)

感谢您的关注Waheed,但我找不到解决方案。现在我的解决方案正在移动&#34; setOnItemClickListener&#34; to&#34; onStart()&#34;。它的工作

    @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);

        }
    });
}