2015-06-29 68 views
1

這是Android應用程序項目的一部分,我有這段代碼我不能夠明白,setOnItemClickListener需要(AdapterView.OnItemClickListener監聽器)作爲參數,但其他方法void onItemClick也是在參數空間。我無法理解AdapterView.OnItemClickListener()的對象如何調用/使用onItemClick?基本代碼閱讀在android

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
      String forecast = mForecastAdapter.getItem(position); 
      Intent intent = new Intent(getActivity(), MainActivity2Activity.class) 
        .putExtra(Intent.EXTRA_TEXT, forecast); 
      startActivity(intent); 
     } 
    }) 
+0

listView通過OnItemClickListener接口調用onItemClick。 https://docs.oracle.com/javase/tutorial/java/concepts/interface.html – Toumash

+0

請參閱此鏈接http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html –

回答

2

new AdapterView.OnItemClickListener() {...}anonymous class的示例。這只是實施AdapterView.OnItemClickListenerinterface並手動覆蓋onItemClick方法(它是abstract method)的快捷方式,然後將其實例化並將其引用傳遞給setOnItemClickListener方法。如果不使用匿名類,這是代碼的樣子:

class MyOnItemClickListener implements AdapterView.OnItemClickListener{ 

    /** 
    * Callback method to be invoked when an item in this AdapterView has 
    * been clicked. 
    * <p/> 
    * Implementers can call getItemAtPosition(position) if they need 
    * to access the data associated with the selected item. 
    * 
    * @param parent The AdapterView where the click happened. 
    * @param view  The view within the AdapterView that was clicked (this 
    *     will be a view provided by the adapter) 
    * @param position The position of the view in the adapter. 
    * @param id  The row id of the item that was clicked. 
    */ 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     //your code here 
    } 
} 

然後實例並將其分配給您的list view

MyOnItemClickListener clickListener = new MyOnItemClickListener(); 
listView.setOnItemClickListener(clickListener); 
1

你必須熟悉匿名類。

new AdapterView.OnItemClickListener(){...} 

創建實現AdapterView.OnItemClickListener偵聽器的新的未命名(匿名)類。 在括號內,您只需從接口實現方法。

2

它不是一個真正的Android的問題 - 這是更根本的Java語法主題。你提到的東西叫做Anonymous Class

setOnItemClickListener方法接受AdapterView.OnItemClickListener對象作爲參數。所以爲了打電話setOnItemClickListener你需要這樣一個監聽器的實例。獲得這樣一個實例的一種方法是創建一個實例。

請看下面的例子:

class MyAwesomeListener implements AdapterView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
     //my awesome click handling logic 
    } 
} 


......... 
MyAwesomeListener listener = new MyAwesomeListener(); 
listView.setOnItemClickListener(listener); 

在這裏,我創建了一個自定義類類,它實現AdapterView.OnItemClickListener接口。由於它是AdapterView.OnItemClickListener的一個實例,我可以將此實例作爲方法參數傳遞給setOnItemClickListener方法。

作爲替代方案,我可以打電話給setOnItemClickListener這樣的:

listView.setOnItemClickListener(new MyAwesomeListener()); 

在這種情況下,我並不需要創建額外的變量。

但是你可以更進一步 - 你能避免創建通過使用匿名類實現AdapterView.OnItemClickListener自定義類:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { 
     //my awesome click handling logic 
    } 
); 

因此,在這種情況下,我創建的AdapterView.OnItemClickListener權當我設置一個監聽器自定義實現。所以這只是上面提到的兩種方法的簡化風格。