1

The is my code.I am here pupulating a list view with some array data.when I am hitting the list I can be able to detect it by using a Toast.But I can't able to call another activity. in the place of Toast event.
public class ListView extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setListAdapter(new ArrayAdapter<String>(this, R.layout.foodjoint, RESTAURANTS));

    android.widget.ListView lv = getListView();
    lv.setTextFilterEnabled(true);


    lv.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();
            myClickHandler();
        }
    });

    //      ListView restuList=ListView();

}

static final String[] RESTAURANTS = new String[] {
    "Restaurant 1", "Restaurant 2", "Restaurant 3", "Restaurant 4", "Restaurant 5",
    "Restaurant 6", "Restaurant 7", "Restaurant 8", "Restaurant 9", "Restaurant 10",
    "Restaurant 11", "Restaurant 12", "Restaurant 13", "Restaurant 14", "Restaurant 15"
};
public void myClickHandler() {
    finish();
    Intent gotoLIst=new Intent(ListView.this,MenuActivity.class);
    startActivity(gotoLIst);
}

and here is my XmL file

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/AliceBlue "
    android:padding="10dp"
    android:textSize="16sp"
    android:focusableInTouchMode="false"
    android:clickable="false"
    android:focusable="false" >

I need to call another activity @ listView onitemClick.

Actually I have used another activity which also use some list-view.and that activity contains some error.that's why it's not working. Thnx to Luksprog for helping me to find the answer.

1 Answer 1

1

You'll probably want to finish() the current Activity after you start the new one:

public void myClickHandler() {
    Intent gotoLIst=new Intent(ListView.this,MenuActivity.class);
    startActivity(gotoLIst);
    finish();
}
4
  • so U wanna mean tht If I just call this methof after following ur editing then I can be able to call another method?ok lemme try Apr 9, 2012 at 11:59
  • 04-09 17:00:10.168: E/AndroidRuntime(271): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView here is the exception Apr 9, 2012 at 12:01
  • @kaushik Does the new activity MenuActivity has a ListView on which you set an ArrayAdapter?
    – user
    Apr 9, 2012 at 12:07
  • @kaushik If you use the same constructor for ArrayAdapter like you did in the first class then the int field(like R.layout.foodjoint) must point to a layout file that only contains a TextView and nothing else. If you want something besides that TextView there are other constructor to use.
    – user
    Apr 9, 2012 at 12:21

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.