ListView,OnItemClick 没有被调用? [英] ListView, OnItemClick not being called?
问题描述
我有一个控制 ListFragment
的 FragmentActivity
;ListFragment
包含一个通用的 ListView
、Adapter
,并从 Singleton
中绘制一个 ArrayList
> 我创建的.
I have a FragmentActivity
that controls a ListFragment
; that ListFragment
contains a generic ListView
, Adapter
, and draws an ArrayList
from a Singleton
that I have created.
当在我的 ListFragment
的 onCreateView
方法中时,我输入了以下代码:
When in the onCreateView
method of my ListFragment
I put the following code:
public View onCreateView(LayoutInflater viewInflation, ViewGroup container,
Bundle SavedInstantState) {
cycleviewfragment = viewInflation.inflate(
R.layout.cycleviewfragment_page, container, false);
context = getActivity().getApplicationContext();
Singleton mySingleton = Singleton.getInstance();
basicList = (ListView) cycleviewfragment.findViewById(android.R.id.list);
adapter = new ArrayAdapter<listControlObject>(getActivity()
.getApplicationContext(), android.R.layout.simple_list_item_1,
mySingleton.getA1());
this.setListAdapter(adapter);
addButton = (Button) cycleviewfragment.findViewById(R.id.addbutton);
addButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(),
listaddactivity.class);
getActivity().startActivity(myIntent);
}
});
basicList.setOnItemClickListener(new ListView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.d("basicListtester", "Testing onClickItem call");
Intent myIntent = new Intent(getActivity(),
listdetailactivity.class);
myIntent.putExtra("selectedObjectIndex",arg2);
getActivity().startActivity(myIntent);
}
});
adapter.notifyDataSetChanged();
return cycleviewfragment;
}
关于为什么当我将项目添加到我的列表时它们没有反应并且没有调用 OnItemClick 的任何想法?
Any ideas as to why when I add items to my list they do not react and the OnItemClick is not called?
谢谢各位.
[更新]
我尝试用 basicList.setAdapter(adapter);
实现它,但仍然没有用.
I tried implementing it with basicList.setAdapter(adapter);
which still did not work.
还尝试让我的 ListFragment
实现 OnItemClickListener
并将该方法添加到类中;这也不起作用.
also tried having my ListFragment
implement OnItemClickListener
and added the method to the class; which did not work either.
推荐答案
由于您使用了 ListFragment
,因此您不应将 onItemClickListener
设置为您的列表.ListFragment
中已经有一个您应该重写的方法.
Since you use ListFragment
you shouldn't set onItemClickListener
to your list. There is already a method in ListFragment
that you should override.
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//Do your thingy.
}
这篇关于ListView,OnItemClick 没有被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!