0

I have been struggling for days trying to find the proper way to implement an action listener to a ListView. What I really want to accomplish is to create a ListView and whenever the user clicks any item, the previous Activity will be switched. I am really new at this so please help me, I would really appreciate it a lot. If you can tell me the what I'm doing wrong on my code that would be awesome!

I am using Fedor's code from [here][1].

        ///Here I tried to Implement an action listener but It doesn't work.

        list.setOnItemClickListener(new OnItemClickListener() {                                                                                                                                                             
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id)                               
            {                                                                                                                                                                           
                if(list.getItemAtPosition(position).equals(mStrings[1]))                                                       
                {                                                                                                           
                    Intent i = new Intent(MainActivity.this, Activity2.class);                                                                                        
                    startActivity(i);                                                                                
                    }                                                                                                    
                }                                                                                                       
            });


      }
Kuwame Brown
  • 533
  • 1
  • 10
  • 22
  • what exactly doesn't work? onItemClick is never called? the if statement fails? – MByD Mar 26 '11 at 13:59
  • The whole application is working fine, however, whenever I click any button it doesn't do anything. I actually want to bring the user from MainActiviy to another Activity. If you didn't notice, I used .setOnItemClickListener at my my MainActiviy.class, but failed to do the task? Thanks for respoding, I hope you have a good idea to help me. – Kuwame Brown Mar 26 '11 at 14:06
  • Let me rephrase it. when you click on an item in your list, does the application start executing `onItemClick` at all, but the if statement `if(list.getItemAtPosition(position).equals(mStrings[1])) ` return false and nothing happens or the pplication doest execute `onItemClick` at all? – MByD Mar 26 '11 at 14:12
  • No the application is not executing anything at all? It's like a ListView without any command, you can press it but nothing will happen. I once accomplished to implement a listener to a plain ListView, however, using Fedor's code from (http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview) is pretty though to do so. I you have any advice, I would really appreciate to hear it Thanks for the quick response! – Kuwame Brown Mar 26 '11 at 14:18
  • I think you don't need onItemClick, but onListItemClick. see the answer to this question: http://stackoverflow.com/questions/5170794/click-listener-on-listview – MByD Mar 26 '11 at 14:28
  • It still doesn't work my friend, it says "the method seOnListItemClick is undefined for the type ListView" :( Would you tell me the proper way to use it if you don't mind? :) – Kuwame Brown Mar 26 '11 at 14:37
  • @user678044 - this may be a trivial question, but did you step through the code and see that the `OnItemClick()` is not called? Also, put a log statement inside the function. – Rajath Mar 26 '11 at 14:53
  • Thanks for the response Rajath, if you look at the code above I called the onItemClick() but it is still not working! – Kuwame Brown Mar 26 '11 at 14:57

1 Answers1

0

You need to override onListItemClick in such a case. Refer to Click Listener on ListView for more help, the solution is a fairly good implementation.

Here is a possible implementation that should go in your Main activity. Please keep in mind that this will only work as long as you keep MainActivity extending Activity.

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
     // Intent launcher here
}

If you wish to extend ListActivity instead of Activity later on and you need to call to onListItemClick, do it following this scheme

public class YourClass extends ListActivity implements OnItemClickListener{

@Override
public void onCreate(Bundle icicle){
    super.onCreate(icicle);
    setContentView(R.layout.your_layout);

    getListView().setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // your stuff here
}
}
Community
  • 1
  • 1
dLobatog
  • 1,751
  • 16
  • 17
  • Where should I override the onListItemClick? On my MainActivity where my Strings could be found or in LazyAdapterClass? Thanks in advance eLobato! – Kuwame Brown Mar 26 '11 at 15:31
  • Override it in MainActivity. I am not sure if you can override it in the LazyAdapter class itself. – dLobatog Mar 26 '11 at 15:37
  • Thanks for the tips, but can you tell me how to use this onListItemClick according to my code. I only know how to use onItemClick but that one didn't work. – Kuwame Brown Mar 26 '11 at 16:00
  • I had included some snippets to help you out mate, I even tested it but if something turns out not to work for you keep commenting :)! – dLobatog Mar 26 '11 at 16:49
  • So how can I tell which item is clicked if you are not using "if stamement"? – Kuwame Brown Mar 26 '11 at 21:12