0

我正在尝试在 Fragment 类中实现 onItemClickListener 但不幸的是它无法正常工作......这是源代码......请让我知道错误是什么?

NetworkDetailsFragment.java(我的片段类)

公共类 NetworkDetailsFragment 扩展 Fragment 实现 AdapterView.OnItemClickListener{

        private ListView listView;
        private View networkDetailsView;
        private QOSNetworkDetailsAdapter qosNetworkDetailsAdapter;
        private QOSNetworkDetailsDatabaseHelper qosNetworkDetailsDatabaseHelper;
        private SimpleCursorAdapter simpleCursorAdapter;


        private String LOG_TAG = NetworkDetailsFragment.class.getSimpleName();




        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            Log.i(LOG_TAG, "Inside onCreateView() method");

            networkDetailsView = inflater.inflate(R.layout.network_details, container, false);
            listView = (ListView) networkDetailsView.findViewById(R.id.list_view);
            qosNetworkDetailsDatabaseHelper = new QOSNetworkDetailsDatabaseHelper(getActivity());

            Cursor cursor = qosNetworkDetailsDatabaseHelper.getRecord();

            String[] columns = {QOSNetworkDetailsDatabaseConstants.COLUMN_NETWORK_TYPE,
                                QOSNetworkDetailsDatabaseConstants.COLUMN_NETWORK_STATUS,
                                QOSNetworkDetailsDatabaseConstants.COLUMN_LATITUDE,
                                QOSNetworkDetailsDatabaseConstants.COLUMN_LONGITUDE,
                                QOSNetworkDetailsDatabaseConstants.COLUMN_TIME};

            int[]  to = new int[]{
                    R.id.networkType,
                    R.id.networkStatus,
                    R.id.latitudeAndLongitude,
                    R.id.date
            };

            /*SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
                    this, R.layout.activity_network_info,
                    cursor,
                    columns,
                    to,
                    0);*/

            simpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.activity_network_info, cursor, columns, to, 0);

            qosNetworkDetailsAdapter = new QOSNetworkDetailsAdapter(getActivity().getApplicationContext(), qosNetworkDetailsDatabaseHelper.getRecord(), false);
            listView.setAdapter(qosNetworkDetailsAdapter);


            return networkDetailsView;
        }


        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
             listView.setOnItemClickListener(this);
        }



        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Cursor cursor = (Cursor) parent.getItemAtPosition(position);

            String countryCode =
                    cursor.getString(cursor.getColumnIndexOrThrow("code"));
            Toast.makeText(getActivity().getApplicationContext(),
                    countryCode, Toast.LENGTH_SHORT).show();
              }


    }

network_details.xml(即list_view.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:contentDescription="@string/network_details_fragment_string"
     android:descendantFocusability="blocksDescendants" >

   <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 

       />

</LinearLayout>




 activity_network_info.xml(list items)



<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" 
    android:descendantFocusability="blocksDescendants">



        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/black" 
            >

            <TextView
                android:id="@+id/networkStatus"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="@drawable/white"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/networkType"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="@drawable/white"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/latitudeAndLongitude"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="@drawable/lightblue"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/date"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="@drawable/lightblue"
                android:textSize="15sp" />


        </LinearLayout>

    </ScrollView>

列表正在填充到屏幕上...但是 onItemClickListener 在此代码中不起作用...请让我知道错误是什么...谢谢

4

2 回答 2

0

尝试扩展 FragmentActivity 而不是 Fragment ...

于 2013-07-01T09:53:12.503 回答
0

如果使用 ListFragment,则布局中的 ListView Id 必须为 android.R.id.list

像那样:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list" />
于 2013-07-01T09:59:44.503 回答