2017-10-15 100 views
0

下面是我第一次尝试设置列表片段的代码,该列表片段点击一个活动(就像测试一样)。 我遵循Android文档示例:List Fragment ExampleonListItemClick显示为“从未使用过”

但是,onListItemClick显示为“从未使用过”。当我运行的应用程序我的列表中显示,但是当我点击其中的项目我只是得到下面的返回:

d/ViewRootImpl:ViewPostImeInputStage ACTION_DOWN

的XML:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="com.example.administrationuser.piclo.ListViewFragment" 
     tools:showIn="@layout/activity_list_view" 
     tools:layout_editor_absoluteY="0dp" 
     tools:layout_editor_absoluteX="0dp" 
     > 

    <FrameLayout 
     android:id="@+id/list_container_id" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     tools:layout_editor_absoluteY="8dp" 
     tools:layout_editor_absoluteX="8dp"> 

     <ListView 
      android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:drawSelectorOnTop="false" > 
     </ListView> 

    </FrameLayout> 

</android.support.constraint.ConstraintLayout> 

这里是列表片段:

package com.example.administrationuser.piclo; 

import android.app.Activity; 
import android.content.Intent; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.support.v4.app.ListFragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListAdapter; 



public class ListViewFragment extends ListFragment { 

    public ListViewFragment() {} 

     String[] myStringArray = new String[]{"aa","bbb","ccccc","dddddd"}; 

     @Override 
     public void onActivityCreated(Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState); 

      // Populate list with our static array of titles. 
      setListAdapter(new ArrayAdapter<String>(getActivity(), 
        android.R.layout.simple_list_item_activated_1, myStringArray)); 
     } 


    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     showDetails(position); 
    } 

     void showDetails(int index) { 

      int mCurCheckPosition = index; 
      Intent intent = new Intent(); 
      intent.setClass(getActivity(), MessageDetails.class); 

      intent.putExtra("Inty", mCurCheckPosition); 
      startActivity(intent); 

      startActivity(intent); 
      } 

} 

回答

0

我已经解决了这个问题。 这对于任何经验丰富的Android开发人员来说都是微不足道的,但对于那些可能陷入此陷阱的初学者来说,这似乎是显而易见的......

...不要将您的父级活动命名为'ListView'。 ..它混淆了OnListItemClick覆盖,因为它不知道它应该重写哪一类ListView ...实际的一个或者你可能通过父类活动的最糟糕可能的名称选择创建的那个。