我正在尝试为Android中的ListView设置OnItemClickListener
,但我无法让它工作。
这是我到目前为止所做的:
public class MenuFragment extends SherlockFragment
{
ArrayList<Item> items = new ArrayList<Item>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
ListView list = (ListView)view.findViewById(R.id.list_mainmenu);
// some code here where i add items to an ArrayList...
// Then i add the ArrayList to an EntryAdapter
EntryAdapter adapter = new EntryAdapter(this.getActivity().getBaseContext(), items);
list.setAdapter(adapter);
list.setClickable(true);
list.setOnItemClickListener(AdapterView.OnItemClickListener()) {
// ...
}
}
但是这给了我OnItemClickListener()
的错误:
对于AdapterView类型,未定义OnItemClickListener()方法。
所以我的问题是,如何在我的ListView上设置OnItemClickListener
?
答案 0 :(得分:10)
检查此代码
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:2)
确保您导入了正确的包:
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
答案 2 :(得分:1)
试试这个
list.setOnItemClickListener(new AdapterView.OnItemClickListener()) {
// ...
}
答案 3 :(得分:0)
你应该实现customAdapter 以便对listView进行更多控制,Here是访问此链接之后的链接,您应该能够执行所需的操作。或者您可以使用此代码快速完成所需的操作。
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long itemID) {
}
});
答案 4 :(得分:0)
试试这个:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// ...
}
});
答案 5 :(得分:0)
尝试这个
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});