Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: onListItemClick in Activity

Previous time I asked a question here I learned a lot so I guess it's worth a shot to try it again.

I am using the lazy list by Fedor from this link: Lazy load of images in ListView

It's working like a charm. BUT, Fedor is making his main class extend Activity instead of ListActivity. Because of this, I am no longer able to use a listItemClick listener. Eclipse declares some errors around onListItemClick(). It works when I turn

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

into

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

But the intent launcher doesn't work. Neither does a toast notification.

When I turn the Activity in a ListActivity, Eclipse doesn't stagger, but my emulator gives me a force close.

How do I get

  • Either onListItemClick() click in the activity (preferable)
  • Or do I transform the code into a ListActivity without force close?

Thanks a lot in advance.

like image 770
Vic V Avatar asked Nov 02 '10 17:11

Vic V


3 Answers

A listItemClickListener is attached to a ListView. When you changed ListActivity to Activity, your class no longer has a view associated with it and thus an Activity class has no idea what to do with an onListItemClickListener.

You just have to attached a listener to your ListView:

listview.setOnItemClickListener(new OnItemClickListener(){
    @Override
    protected void onListItemClick(AdapterView<?> parent, View view, int position, long id){
        //Do stuff
    }
});
like image 123
Falmarri Avatar answered Oct 21 '22 13:10

Falmarri


I am writing my answer as:

1) the code by @Falmarri needs some update
2) My suggested edit was totally rejected XD
3) Stackoverflow is not allowing me to write a comment.

Here is the code:

ListView listView = (ListView) findViewById(R.id.my_listview_in_layout);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        //Do stuff
        //...
    }
});


Reference: According to android.widget.AdapterView.OnItemClickListener , public method onItemClick() is the method invoked when an item is clicked {instead of unknown protected method onListItemClick() }

like image 33
Vinay Vissh Avatar answered Oct 21 '22 11:10

Vinay Vissh


I have been working on this all day and after making my own ArrayAdapter I couldn't figure out how to change classes in my list.

Here's how I found out how to do it. After I called my array i simply finished out my code in that method by doing.

ListView lv =getListView();
lv.setOnItemClickListener(this);

Then after all my text i put

public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
    String item = (String) getListAdapter().getItem(position);

    if (item.equals("Economy"))
    {
        Intent intent = new Intent(packages.this, economy.class);
        startActivity(intent);
    }
    else if (item.equals("Basic"))
    {
        Intent intent = new Intent(packages.this, basic.class);
        startActivity(intent);
    }
    else if (item.equals("Professional"))
    {
        Intent intent = new Intent(packages.this, professional.class);
        startActivity(intent);
    }
    else if (item.equals("Custom Applications"))
    {
        Intent intent = new Intent(packages.this, applications.class);
        startActivity(intent);
    }
}

Between I managed to completely customize my ListView with custom font and backgrounds. I'm sure a ton of you don't really care. But I'm exited and was hoping by posting this that I might help someone in the future.

like image 38
Nathan Kellert Avatar answered Oct 21 '22 13:10

Nathan Kellert