2013-12-21 184 views
0

我在我的應用程序中獲得了一個簡單的ListFragment,以顯示類似Strings的靜態數據。我需要使單選項列表項可點擊。這是我到目前爲止所嘗試的。我可以看到列表項目,但我無法從單擊列表項中獲取任何日誌。 該代碼有什麼問題?在ListFragment中的ListView點擊項目

public class SettingsTab extends ListFragment implements OnItemClickListener{ 
String[] numbers={"one","two","three","four"}; 
ListView list ; 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.d("Settings tab", "Fragment Created (onCreate)"); 
    } 
@Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     Log.d("Settings tab", "Fragment Created (onActivityCreate)"); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.settingsfragment, container, false); 
     list = (ListView)v.findViewById(android.R.id.list); 
     list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     list.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.listitemlayout, 
       R.id.listitemUniName, names)); 
     list.setOnItemClickListener(this); 
     Log.d("Settings tab", "Fragment Created (onCreateView)"); 
     return v; 
    } 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
      long arg3) { 
     Toast.makeText(getActivity().getBaseContext(), "Item clicked: " + arg2, Toast.LENGTH_LONG).show(); 
     Log.d("settings", "click worked"); 
    } 

回答

0

@Jukka Raanamo是對的,這幫了我,你可以擺脫OnItemClickListener和方法onItemClick()。然後覆蓋onListItemClick()

例如:

@Override 
    public void onListItemClick(ListView l, View v, int position, long id) 
    { 
     super.onListItemClick(l, v, position, id); 
     // Do whatever you need to do here. 
     Toast.makeText(getActivity().getBaseContext(), "Item clicked: " + arg2, Toast.LENGTH_LONG).show(); 
     Log.d("settings", "click worked"); 
    } 

由於@Jukka Raanamo