AlexAndro
AlexAndro

Reputation: 1928

Go to another layout from onListItemClick() in Android

I have a ListView, and I want that when I press an item, go to another screen, which I will later customize, but for the moment I just want go to that window, in this case is about a xml file defined in res/layout/mylayout.xml.

protected void onListItemClick (ListView l, View v, int position, long id){
    super.onListItemClick(l, v, position, id);
    setContentView(R.layout.mylayout);
}

I can't imagine what something else I should do.In LogCat I get some exception that my content has to contain a listview but why this should happens? maybe I want to put something else in that layout, maybe just a textview.Please tell me how could I do this.

Upvotes: 1

Views: 2028

Answers (3)

skynet
skynet

Reputation: 9908

What you should do is create a different Activity, and start it using an Intent.

Basically you want to use a different activity for every screen. More information on the Android developers site, which is a great resource

http://developer.android.com/guide/topics/fundamentals.html

Here is how you start an Activity from within an Activity using an intent. Assuming the Activity you want to start is called MyActivity:

Intent intent = new Intent(this, MyActivity.class);
startActivity(intent);

also be sure to add the activity to your manifest file inside of the <application></application> tags:

    <activity
        android:label="My Activity Title"
        android:name=".MyActivity" >
    </activity>

Upvotes: 2

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

You are extendind ListActivity , so view need a ListView . after first itemClick error prompt means now view has been reset by setContentView(R.layout.mylayout), which does not contain a listView .

so Extend activity instead of LsitActivity . inside it write

ListView lv = (ListView) findViewByID(R.id.listView) ;

onClikc issue will be resolved .

Upvotes: 0

Konstantin Pribluda
Konstantin Pribluda

Reputation: 12367

Proper way would be to fire up anothwer activity ( by firing an intent, possibly with some extras to commincate clicked item )

Upvotes: 0

Related Questions