0

My list includes eight items positioned beneath a 'header' row, all contained within a tab on my application. I cannot figure out how to change to an activity (or possibly a different tab) based upon the item clicked on in the list.

I'm currently extending the Activity class, not sure if this is an issue. I've attempted to use the onListItemClick whilst extending the ListActivity class; this however caused the application to crash.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class Tab2 extends Activity {

    private ListView listView1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab2);

        Cinema cinema_data[] = new Cinema[]{
            new Cinema(R.drawable.blue, "Blue Cinema"),
            new Cinema(R.drawable.green, "Green Cinema"),
            new Cinema(R.drawable.purple, "Purple Cinema"),
            new Cinema(R.drawable.red, "Red Cinema"),
            new Cinema(R.drawable.yellow, "Gold Cinema"),
            new Cinema(R.drawable.blue, "Cyan Cinema"),
            new Cinema(R.drawable.green, "Lime Cinema"),
            new Cinema(R.drawable.purple, "Magenta Cinema")
        };

        CinemaAdapter adapter = new CinemaAdapter(this,
                R.layout.listview_item_row, cinema_data);

        listView1 = (ListView)findViewById(R.id.listView1);

        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        listView1.addHeaderView(header);

        listView1.setAdapter(adapter);

    }   
}

any help will be greatly appreciated!

edit:

protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        try {
            Intent i = new Intent("android.lab.two.Tab1");
            startActivity(i);
        } catch(Exception e){
            e.printStackTrace();
        }
    }
Josh
  • 41
  • 1
  • 1
  • 5
  • where you write your onListItemClick event code paste that code here – Pratik Dec 12 '11 at 13:41
  • so you want to start your activity within your tab? it's in current listview tab or in different tab? – Pratik Dec 12 '11 at 13:47
  • i'd mainly like to know how to start any activity from within this tab & list. or at least detect that the user clicks on an item. – Josh Dec 12 '11 at 13:49
  • Also, could you post the error when the application crashes ? Your question is quite vague, we don't know what is the real problem... – chamel Dec 12 '11 at 14:03
  • The error message is quite vague too; "Unfortunately has stopped working" - strangely it only happens when i click on the tab that contains the list. – Josh Dec 12 '11 at 14:12

5 Answers5

1

if the OnItemClickListener doesn't work, you can try setting it in your adapter :

in your adapter, in the getView method, you can add an OnClickListener to your row view like this :

public View getView(int arg0, View arg1, ViewGroup arg2) {
//Create the view for your row
    AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT, 64);
    TextView rowView = new TextView(getApplicationContext());
    rowView.setLayoutParams(lp);
    rowView.setText("your value");
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           try {
                //Starting a new intent ( here a Dial Activity )
                Intent newIntent = new Intent(Intent.ACTION_DIAL);
                newIntent.setData(Uri.parse("tel:"+value));
                startActivity(newIntent);
            } catch (ActivityNotFoundException e) {
                Log.e("your application", "Dial failed", e);
            }
        }  
    });
}
chamel
  • 367
  • 4
  • 16
  • "The method startActivity(Intent) is undefined for the type new View.OnClickListener(){}" when I attempt to set the onClickListener in my CinemaAdapter class – Josh Dec 12 '11 at 14:08
  • I have defined my Adapter class in my Activity class, thus it has access to the activity's methods. If you defined it separately, you need to get a reference to your activity to call `myActivity.startActivity(myIntent);` – chamel Dec 12 '11 at 14:14
  • Sorry, my Adapter class extends ArrayAdapater, which is why I'm guessing i cant use startActivity(), could you elaborate on how i would work around this? Thanks. – Josh Dec 12 '11 at 14:22
  • see [this question](http://stackoverflow.com/questions/4197135/how-to-start-activity-in-adapter) to learn how to start an activity from the adapter. – chamel Dec 12 '11 at 14:23
  • Ok. I now seem to be able to log indv. item clicks, just trying to start an activity from this atm... – Josh Dec 12 '11 at 14:38
0

if you set onListItemClick() method inside your tab(activity) then you have to use

protected void onListItemClick(ListView l, View v, int position, long id) 
{
            super.onListItemClick(l, v, position, id);

            Intent i = new Intent(this,tab1.class);
            startActivity(i);           
}

if you set onListItemClick() method is define in other class then you have to use

protected void onListItemClick(ListView l, View v, int position, long id) 
{
            super.onListItemClick(l, v, position, id);

           Intent i = new Intent(v.getContext(),tab1.class);
           startActivity(i);            
}

if tab1(activity) is a main activity and you have to call tab2(activity) inside the tab1(activity) then you have to use this way...

protected void onListItemClick(ListView l, View v, int position, long id) 
{
                super.onListItemClick(l, v, position, id);

                System.exit(0);               
}
Parag Ghetiya
  • 421
  • 2
  • 14
0

You just set the click listener on your listview. You get the position and perform you logic from there. The code below should help you get started. I don't know what error you were getting, but there is not much that can go wrong.

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent myIntent = new Intent(view.getContext(),
                    NextClass.class);

            startActivity(myIntent);
        }
    });
Mike L.
  • 589
  • 6
  • 16
  • I need to extend ListActivity to use this method, have no idea why but when I then click on the tab that contains the list i get: "Unfortunately has stopped working" – Josh Dec 12 '11 at 13:59
  • Can you get the stacktrace using logcat ? – chamel Dec 12 '11 at 14:04
  • Yes, from when I attempted to extend ListActivity... where should I upload the information? – Josh Dec 12 '11 at 14:11
0
  1. Use the setCurrentTab method in TabHost to change tab based on the list item index.
  2. Fire intent to this activity itself keeping it in the single top launch modes & then using the bundled data change the tab via tab host.

I hope this helps..

R.daneel.olivaw
  • 2,681
  • 21
  • 31
0

You cannot start an activity from a TabHost. You are in a inner class. You must use NestedClasses. Instead of starting an Intent in your onClickListener, try to Log something, and you will see that will work. Log.d("LOG","Message");

Dany's
  • 943
  • 1
  • 7
  • 17
  • Log whithin the onItemClickListener. Please delete "Intent myIntent = new Intent(view.getContext(), NextClass.class); " and instead put a log message and you will see it in LogCat. and you will not get any error – Dany's Dec 12 '11 at 14:17
  • cant seem to add an onItemClickListener: "The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView is not applicable for the arguments (new OnItemClickListener(){})" – Josh Dec 12 '11 at 14:26
  • protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); try { Intent i = new Intent("android.lab.two.Tab1"); startActivity(i); } catch(Exception e){ e.printStackTrace(); } }You have this function written by you. Replace Intent i = new Intent("android.lab.two.Tab1"); startActivity(i); with Log.d("LOG","Message"); – Dany's Dec 12 '11 at 14:29
  • void is apparently an invalid return type? – Josh Dec 12 '11 at 14:42
  • I dont understand what you mean with that? Tell me if you replaced the code, and if it works. If you delete the Intent thhing, then your app will not crash...if you tell me that the app dont crash anymore, i will tell you what to do further to start an activity from your List – Dany's Dec 12 '11 at 14:49
  • I changed the code as you suggested, but it wont compile. I get "void is an invalid type for the variable onListItemClick" on the method... – Josh Dec 12 '11 at 14:51