2015-12-31 48 views
1

我的代碼在下面提到,不知道它有什麼問題。我在Fragment中有列表視圖,並且想要在點擊列表項目上調用活動。如何從點擊清單項目調用活動

public class fren_fragment extends Fragment { 
    FrenListAdapter cAdapter; 
    ListView cListView; 


    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.friend_fragment, container, false); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 

     super.onActivityCreated(savedInstanceState); 

     cAdapter = new FrenListAdapter(getActivity()); 
     cListView = (ListView) getActivity().findViewById(android.R.id.list); 
     cListView.setAdapter(cAdapter); 
     cListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       cListView.setItemChecked(position,true); 
       Context context = view.getContext(); 
       Intent intent = new Intent(context, CompanyDetailActivity.class); 
       context.startActivity(intent); 
      } 
     }); 

    } 
} 
+0

取代'context'取代'getActivity()' –

+0

你的問題在這裏很不明確,你能編輯它來描述你的具體問題嗎?它可能有助於閱讀[我如何問一個好問題?](http://stackoverflow.com/help/how-to-ask) – DaveyDaveDave

+0

請確保你的'onItemClick'正在通過寫一些logcat來檢查它 –

回答

0

ContextIntent是錯誤的,試試這個。

cListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     cListView.setItemChecked(position,true); 
     Intent intent = new Intent(getActivity(), CompanyDetailActivity.class); 
     startActivity(intent); 
    } 
}) 
+0

不工作:(:( –

+0

後,我單擊列表中的項目 –

+0

發表您的崩潰日誌中的應用進行了崩潰 – Jas

0

你應該改變

Context context = view.getContext(); 
Intent intent = new Intent(context, CompanyDetailActivity.class); 
context.startActivity(intent); 

Intent intent = new Intent(getActivity(), CompanyDetailActivity.class); 
startActivity(intent); 
+0

不工作:(:( –

+0

我想我已經做了愚蠢的錯誤在CompanyDetailActivity –

+0

如果您的應用程序崩潰,錯誤請張貼logcat的 –

0

試試這個:

Intent intent = new Intent(getActivity(), CompanyDetailActivity.class); 
startActivity(intent); 
1

試試這個:

cListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      cListView.setItemChecked(position, true); 

      Intent intent = new Intent(getActivity(), CompanyDetailActivity.class); 
      getActivity().startActivity(intent); 
     } 
    }); 
0

更改您的代碼如下

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
View view= inflater.inflate(R.layout.friend_fragment, container, false); 
cAdapter = new FrenListAdapter(getActivity()); 
cListView = (ListView) view.findViewById(android.R.id.list); 
cListView.setAdapter(cAdapter); 
cListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     cListView.setItemChecked(position,true); 
     Intent intent = new Intent(getActivity(), CompanyDetailActivity.class); 
     getActivity().startActivity(intent); 
    } 
}); 
} 

刪除

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
} 
相關問題