1

I didn't found the solution with the search :(

I'm programming an application for reading RSS feeds.
I'm using a listview for putting a screen title and description of every feed into 2 textview. I realized that with a SimpleAdapter.

In my project I have 2 classes: MainActivity and AsynkTask. In mainActivity I execute the asyncTask that reads RSS and puts them into the adapter and the listview

Now, how can I add a click listener on the listview for opening every feed into the browser?

I hope that I've explained correctly the problem and I'm sorry for my very bad english! Thanks to all.

PS: if you need some of my code i will post it.

1

2 Answers 2

7

In the onCreate method of your MainActivity, if it's a ListActivity, you need to get a reference to your list view like so:

ListView lv = getListView();

Then add a click listener to it like so:

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) { 
  //Open the browser here
}
6
  • on the first line, Eclipse send me this error: " The method getListView() is undefined for the type MainActivity " Dec 7, 2011 at 16:03
  • Are you adding a list view to your layout and using a plain Activity or are you using a ListActivity? The ListActivity is designed for activities that only have a list view as an element. If you're using an Activity and have a ListView on your layout, you'd need to get access to it like this: ListView lv = (ListView) findViewById(R.id.idOfListViewOnLayout);
    – Chris
    Dec 7, 2011 at 16:06
  • 1
    ok, done.. but when i call setonitemclicklistener eclise send me this error: Multiple markers at this line - OnItemClickListener cannot be resolved to a type - The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (new OnItemClickListener(){}) Dec 7, 2011 at 16:15
  • 1
    Are you importing OnItemClickListener in your class: import android.widget.AdapterView.OnItemClickListener;
    – Chris
    Dec 7, 2011 at 16:24
  • yes sorry i've solved in this way.. sorry for my mistake ... just an explanation.. to take the position of the element i've clicked how can i do? i've tried with lv.getSelectedItemPosition() but eclipse tell me that lv have to be a final variable but declaring lv as final the app crash when i click :S Dec 7, 2011 at 16:27
1

On your list view, you need to use setOnItemClickListener

1
  • I have to set this property into the AsynkTask class or mainActivity class? Dec 7, 2011 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.