Repository navigation
ListFragment can't use @ItemClick #432
Description
Activity
Hi @fly1ddid
What do you exactly mean by "Didn't work" ? Is there any compile error, a runtime exception, or just nothing hapenning ?
By the way, the method should be name
listItemClicked(). You forgot theed.Hi @pyricau
Sorry, I wrote this issue too brief.
I'm using android.support.v4.app.ListFragment (using android-support-v4.jar).
I wrote below code using @ItemClick, however nothing happenning.I think onItemClickedListener is overwritten because ListFragment has onListItemClicked method.
package jp.fly1ddid.listfragmenttest; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.widget.ArrayAdapter; import android.widget.Toast; import com.googlecode.androidannotations.annotations.EFragment; import com.googlecode.androidannotations.annotations.ItemClick; @EFragment public class MyListFragment extends ListFragment { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); String[] listItemTexts = { "test", "test", "test" }; setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, listItemTexts)); } @ItemClick void listItemClicked(String string) { Toast.makeText(getActivity(), string, Toast.LENGTH_SHORT).show(); } // @ItemClick(android.R.id.list) // void listItemClicked(String string) { // Toast.makeText(getActivity(), string, Toast.LENGTH_SHORT).show(); // } // @Override // public void onListItemClick(ListView l, View v, int position, long id) { // Toast.makeText(getActivity(), (String) l.getAdapter().getItem(position), // Toast.LENGTH_SHORT).show(); // } }
and this is generated code.
// // DO NOT EDIT THIS FILE, IT HAS BEEN GENERATED USING AndroidAnnotations. // package jp.fly1ddid.listfragmenttest; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; public final class MyListFragment_ extends MyListFragment { private View contentView_; private void init_(Bundle savedInstanceState) { } @Override public void onCreate(Bundle savedInstanceState) { init_(savedInstanceState); super.onCreate(savedInstanceState); } private void afterSetContentView_() { { AdapterView<?> view = ((AdapterView<?> ) findViewById(android.R.id.list)); if (view!= null) { view.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { listItemClicked(((String) parent.getAdapter().getItem(position))); } } ); } } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { contentView_ = super.onCreateView(inflater, container, savedInstanceState); afterSetContentView_(); return contentView_; } public View findViewById(int id) { if (contentView_ == null) { return null; } return contentView_.findViewById(id); } public static MyListFragment_.FragmentBuilder_ builder() { return new MyListFragment_.FragmentBuilder_(); } public static class FragmentBuilder_ { private Bundle args_; private FragmentBuilder_() { args_ = new Bundle(); } public MyListFragment build() { MyListFragment_ fragment_ = new MyListFragment_(); fragment_.setArguments(args_); return fragment_; } } }
Hi @pyricau
I examine
ListFragmentsource code.OnItemClickedListenerforListfragment#onListItemClickis set inonViewCreated().@ItemClickinjects code inonCreateView().Documents says
onViewCreated()Called immediately afteronCreateView(), soonClickListeneris overwritten.AA injects
@ItemClickcode not inonCreateView()but inonViewCreated().Nice one @fly1ddid, you indeed found a bug. It seems that we should override
onViewCreated(), and do the view injection / binding from there.Meanwhile, I guess you'll have to use the
onListItemClickedmethod instead of using@ItemClickI encountered that issue, too.
Not sure it was a bug, though :)Nice report.
Hello,
I face the same issue, how I could workaround it ?
Hi Atali,
This issue as been fixed on the development branch, you can check out the latest snapshot built on this branch here :
https://oss.sonatype.org/content/repositories/snapshots/org/androidannotations/androidannotations-bundle/3.0-SNAPSHOT/Thanks Mathieu, I will give a try :-)
Le 21 janv. 2013 10:19, "Mathieu Boniface" notifications@github.com a
écrit :Hi Atali,
This issue as been fixed on the development branch, you can check out the
latest snapshot built on this branch here :—
Reply to this email directly or view it on GitHubhttps://github.com//issues/432#issuecomment-12489740.
For Example, below code didn't work in the
ListFragment.@ItemClickannotation is not able to use inListFragment?I wan't to hook the
ListViewclick (android.R.id.list)