Reputation: 21007
I'm trying to set an OnItemClickListener
for my ListView in Android, but i can't get it to work.
This is what i have so far:
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()) {
// ...
}
}
But this gives me an error on OnItemClickListener()
:
The method OnItemClickListener() is undefined for the type AdapterView.
So my qyestion is, how can i set an OnItemClickListener
on my ListView??
Upvotes: 3
Views: 9247
Reputation: 6682
try this one
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
Upvotes: 0
Reputation: 497
Try this:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// ...
}
});
Upvotes: 0
Reputation: 29199
make sure you have imported correct packages:
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
Upvotes: 2
Reputation:
try this
list.setOnItemClickListener(new AdapterView.OnItemClickListener()) {
// ...
}
Upvotes: 1
Reputation: 4489
You should implement a customAdapter for having more control on your listView, Here is the link after visiting this you should be able to do what is required. Or you can have this code to quickly do what you need.
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long itemID) {
}
});
Upvotes: 0
Reputation: 6317
check this code
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();
}
});
Upvotes: 10