尝试将setOnItemClickListener
添加到片段时遇到问题。
这是我的片段。
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));
}
});
}
永远不会执行setOnItemClickListener。有任何想法吗? 我需要实现其他一些方法吗?
感谢。 最好的问候。
答案 0 :(得分:0)
您在onActivityCreated
方法上启动AsyncTask,因为在附加布局之前,您正在启动异步,这可能是侦听器设置的原因。
@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();
}
答案 1 :(得分:0)
您的LocalesAdapter可能是原因。您正在将上下文(getActivity())传递给适配器两次。
locales_list.setAdapter(new LocalesAdapter(getActivity(), getActivity(), localesList));
使传递给LocalesAdapter的参数正确无误。