I am trying to do a list on an Android´s ListFragment with an OnItemClickListener but I can´t, I click n list item and nothing happens. I am novice with fragments. Here is my code. Thank you!
public class MyFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.hello, container, false);
ListView lv =(ListView) v.findViewById(android.R.id.list);
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// getting values from selected ListItem
String titulo = ((TextView) v.findViewById(R.id.title))
.getText().toString();
String contenido = ((TextView) v.findViewById(R.id.content))
.getText().toString();
String fecha = ((TextView) v.findViewById(R.id.date))
.getText().toString();
// Starting new detail
Intent in = new Intent(getActivity().getApplicationContext(),
NoticiasFragment.class);
in.putExtra("title", titulo);
in.putExtra("content", contenido);
in.putExtra("date", fecha);
startActivity(in);
}
return v;
}
ListFragment
has a built-in onListItemClick
function to override.
You should do it like this:
public class MyFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.hello, container, false);
return v;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
//Do your stuff..
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With