2014-04-28 46 views
1

一個ListView我有我在哪裏顯示列表視圖如何檢測單擊事件在片段

我的問題 ::

  • 片段如何檢測的onClick事件的行在列表中,以便當我點擊它們時,我可以啓動一個新的活動
  • 我不知道我是否需要檢測,單擊事件適配器或 片段本身
  • 如果是這樣的話,怎麼樣?

FragRatingAscending.java

public class FragRatingAscending extends SherlockFragment { 

    ListView list; 
    ListViewSortingAdapter adapter; 
    private CustomCursorAdapter customAdapter; 
    private DatabaseAdapter databaseHelper; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.sorting_listview, container, 
       false); 

     databaseHelper = new DatabaseAdapter(getActivity()); 


     // Locate the ListView in fragmenttab1.xml 
     list = (ListView) rootView.findViewById(R.id.listview); 

     // Database query can be a time consuming task .. 
     // so its safe to call database query in another thread 
     // Handler, will handle this stuff for you 

     new Handler().post(new Runnable() { 
      @Override 
      public void run() { 
       customAdapter = new CustomCursorAdapter(getActivity(), databaseHelper.getAllContacts(), 0); 
       list.setAdapter(customAdapter); 
      } 
     }); 

     return rootView; 

    } 


    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onActivityCreated(savedInstanceState); 

     /*list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // TODO Auto-generated method stub 

       Intent intent=new Intent(getActivity(),BuffetContents.class); 
       startActivity(intent); 
      } 
     });*/ 

     list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // TODO Auto-generated method stub 
       Intent intent=new Intent(getActivity(),BuffetContents.class); 
       startActivity(intent); 
      } 
    }); 


    } 


    @Override 
    public void onStart() { 
     // TODO Auto-generated method stub 
     super.onStart(); 
    } 

} 

我試圖與片段,但它不工作

list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // TODO Auto-generated method stub 
       Intent intent=new Intent(getActivity(),BuffetContents.class); 
       startActivity(intent); 
      } 
    }); 
+0

您是否找到解決方案? – GunnerFan

+0

@GunnerFan ...是的!我也接受了答案! – Devrath

回答

1

這個工程

list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 
      //intent 
     } 
    }); 
0

在onCreateView()方法中使用此方法。這可以幫助您

list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // TODO Auto-generated method stub 
       Intent intent=new Intent(getActivity(),BuffetContents.class); 
       startActivity(intent); 
      } 
    }); 
相關問題