What I try is that when clicking on an item ListView
that is in a fragment
it takes me to a Activity
.
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listaconimagenes adapter;
frutas = getResources().getStringArray(R.array.arraycincofrutas);
lista = (ListView) getActivity().findViewById(R.id.lista_frutas);
adapter = new listaconimagenes(getActivity(), frutas, imagenes);
lista.setAdapter(adapter);
lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
Intent intent = new Intent(R.id.main_content, Main3Activity.class);
break;
}
}
});
}
}
What you are doing is wrong:
you need as the first parameter to the intent, the context , and as the second parameter the
Activity
one you want to start.In the case of a
Fragment
, you can get the context of theActivity
viagetActivity()
. To start the Activity you must use the startActivity() method :Review the documentation .
This would be your code, which starts a new one
Activity
by clicking on the first element of theListView
(first element is position 0):You are doing very well, you just need to start the activity after the intent.