I am using import android.support.v4.app.ListFragment to create a ListFragment. Inside the fragment itself -- not the parent activity -- I want to implement the onItemClick method. Will someone please provide a simple example on how this might work?
public class MyListFragment extends ListFragment {
Spannable[] stuff = {};//to be filled
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayAdapter<Spannable> adapter = new ArrayAdapter<Spannable>(inflater.getContext(), android.R.layout.simple_list_item_1, stuff);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//this is red underlined by eclipse
}
}
Have you done some research before asking that? Anyway, it can be done by overriding onListItemClick method, as following:
public class Test extends ListFragment {
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//do the stuff
}
}
Related
Don't know why the onItemClick event not working...
public class FragmentList extends Fragment {
View rootView;
ListView list;
public FragmentList (){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_list, container, false);
list=(ListView)rootView.findViewById(R.id.list);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Log.i("test", String.valueOf(position));
}
});
return rootView;
}
}
Anyone has idea on this. thanks so much
If any row item of list contains focusable or clickable view then OnItemClickListener won't work.
The row item must have a param like android:descendantFocusability="blocksDescendants".
Click here for more information.
I have a problem when try to add setOnItemClickListener into a Fragment.
This is my Fragment.
public class LocalesFragmento extends Fragment {
ListView locales_list;
private List localesList;
private Context context;
public LocalesFragmento() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View vista = inflater.inflate(R.layout.locales_list, container, false);
locales_list = (ListView) vista.findViewById(R.id.localesList);
new getLocalesAsyncTask().execute();
return vista;
}
private void setListAdapter() {
Log.e("Adapter","Adapter...");
locales_list.setAdapter(new LocalesAdapter(getActivity(), getActivity(), localesList));
locales_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//detailInstagram(list.get(position));
}
});
}
The setOnItemClickListener is never executed. Any ideas?
Do I need to implement some other method?
Thanks.
Best regards.
You start your AsyncTask on onActivityCreated method because here before attaching the layout you are starting an async and that may be the reason why the listener is setting up.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
locales_list.setAdapter(new LocalesAdapter(getActivity(), getActivity(), localesList));
locales_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//detailInstagram(list.get(position));
}
});
new getLocalesAsyncTask().execute();
}
Your LocalesAdapter might be the cause. You are passing the context (getActivity()) to the adapter twice.
locales_list.setAdapter(new LocalesAdapter(getActivity(), getActivity(), localesList));
Make your that the arguments passed to the LocalesAdapter are correct.
this is my first post on stackoverflow, so please excuse any shortcomings I might be having with norms.
I'm trying to have a tabbed view on an application, and have each tabbed view (just a normal fragment) be a gridview. I thought I could just put the initialization code for the gridview in the onCreateView, but ive been at this problem for a couple hours now, and the gridview is returning null in the findviewbyid call. Here's the relevant code (I'm trying not make this question overwhelming , so I won't post all the code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// View rootView = inflater.inflate(
// R.layout.fragment_main_ingredient_dummy, container, false);
GridView gridview = (GridView) inflater.inflate(R.id.gridview, container);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
}
});
gridview.setAdapter(new ImageAdapter(getActivity()));
return gridview;
}
As I said. the fragment is an ordinary fragment, and its being "managed" by a FragmentPagerAdapter. However, I also have an extension of a BaseAdapter, for teh content inside the actual tab.
Thank you in advance!
You can't inflate a view like that. The inflate call needs an R.layout. reference, not an R.id. reference.
You should only create the view in onCreateView. Then do the other work in onViewCreated.
private GridView mGridView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {;
mGridView = (GridView) inflater.inflate(R.layout.frag_grid, container);
return mGridView;
}
#Override
public void onViewCreated(){
mGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
}
});
mGridView.setAdapter(new ImageAdapter(getActivity()));
}
I am using fragments. I have a spinner in the fragment. I want to lauch a new activity when spinner item is selected. I am getting this error
Error
The constructor Intent(UserHomeActivity, Class) is undefined UserHomeActivity.java /SwipeyTabs/src/com/recscores/android line 28 Java Problem
public class UserHomeActivity extends SherlockFragment{
Spinner spinnerTeam;
Spinner spinnerLeague;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view =inflater.inflate(R.layout.user_home, container, false);
// Team Spinner
spinnerTeam = (Spinner)view.findViewById(R.id.spinner_team);
spinnerTeam.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
**Intent ii = new Intent(UserHomeActivity.this,TeamHomeActivity.class);
startActivity(ii); **
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
Start new Activity as :
Intent ii = new Intent(getActivity(),TeamHomeActivity.class);
startActivity(ii);
because Context is not super class of SherlockFragment class and you will need to use getActivity() which return Activity to current fragment is associated.
I can not get setOnItemClickListener of gridView in Fragment. What can be the problem?
Here is my code::
public class MainMenuFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.main_menu_fragment, container, false);
itemsGridViewObj = (GridView) view.findViewById(R.id.itemsGridView);
itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Log.d(TAG, "--> onItemClick listener..."); // Can not getting this method.
/*if(position == 1) {
FruitMenuFragment fruitMenuFragment = new FruitMenuFragment();
fragmentTransaction.replace(android.R.id.content, fruitMenuFragment);
fragmentTransaction.commit();
}*/
}
});
return view;
}
}`
You may need to set the following in your ButtonView.
android:focusable="false"
android:focusableInTouchMode="false"
see adding CheckBox to list row loses my onItemClick events?
When using Fragments the initialisation of the view occurs over two stages.
The view is only inflated (and therefore accessible) after the onCreateView method. This method is only for inflating a view and returning it to the Fragment.
Therefore, any logic to do with finding views and setting up onClickListeners should be done in the onActivityCreated() function as this is the first point at which you can access the inflated view.
Have a look at the Google docs at http://developer.android.com/reference/android/app/Fragment.html#Lifecycle
Below is you code adjusted to comply to what I have described above:
public class MainMenuFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.main_menu_fragment, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
GridView itemsGridViewObj = (GridView) findViewById(R.id.itemsGridView);
itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Log.d(TAG, "--> onItemClick listener..."); // You should see this now
/*if(position == 1) {
FruitMenuFragment fruitMenuFragment = new FruitMenuFragment();
fragmentTransaction.replace(android.R.id.content, fruitMenuFragment);
fragmentTransaction.commit();
}*/
}});
}
}