2013-07-01 63 views
0

我想在片段類中實現onItemClickListener,但不幸的是它不能正常工作......這裏是源代碼..請讓我知道什麼是錯誤?如何使用onitemClickListener和Fragments?

NetworkDetailsFragment.java(我的片段類)

公共類NetworkDetailsFragment擴展片段實現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不在這個代碼中工作...請讓我知道什麼是錯誤..謝謝

+0

你有什麼錯誤嗎? – Blackbelt

+0

不,我沒有收到任何錯誤 –

回答

0

儘量延長FragmentActivity而不是片段...

+0

我將這個片段添加到FragmentActivity .... –

0

如果使用ListFragment,你的ListView標識的佈局必須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" /> 
+0

我說因爲你有onItemClick方法(女巫被覆蓋)所以,我想你使用一個ListFragment。 listView.setOnItemClickListener(this);在onActivityCreated在這種情況下是無用的 – Pauland

相關問題