【问题标题】:ListView in fragment nothing happen when clicked单击时片段中的ListView没有任何反应
【发布时间】:2017-02-16 10:30:46
【问题描述】:

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

}

【问题讨论】:

  • 日志中有任何消息 (EventId) ??
  • Log.d("EventId", gid +"."+ gname);在 ItemClick 之后打印?
  • 您有一个自定义视图作为列表视图项?如果是,那么您可能会出现此问题。发布您的自定义视图代码,我可以给您一个解决方法
  • 在我迁移到 public void onStart() 上的 getListView() 之前,没有 Log.d("EventId", gid +"."+ gname);在 ItemClick 之后打印。
  • 使用下面的解决方案后,它现在可以工作了

标签: android database listview fragment clickable


【解决方案1】:

如果列表的任何行项包含可聚焦或可点击的视图,则 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>

【讨论】:

    【解决方案2】:

    感谢您关注 Waheed,但我没有找到解决方案。现在我的解决方案是将“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);
    
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多