Android Tutorial – List View And Detail Application in Android Studio – Part 6

             Hello, I’m Andy. We will continue the last tutorial.

 

            We complete the list view and need to start develop the detail page now.

 

            First, please create an Activity named “DetailActivity” and Android Studio will also create a layout named “activity_detail” for you.

 

            Then, we modify the text view in the layout; create a new string for it – “Welcome to detail.

 

 76-1-android-tutorial

            [Image 1]

           

            Next we go to MainActivity. Add below code at the end of onCreate method.

 

        // Set OnItemClickListener

        listView.setOnItemClickListener(this);

 

     An error should be appear because of below reason:

           76-2-android-tutorial

            [Image 2]

            It is very easy to fix it, just let your activity implement that interface is OK. (This time the interface is OnItemClickListener.)

           

            Let me show that for you

 

        // Let Activity implement Interface

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener

           

                // Handle the OnItemClickListener itself

@Override

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        // Go to Detail Activity

        Intent i = new Intent(MainActivity.this, DetailActivity.class);

        // Send the position number to Detail Activity too.

        i.putExtra(“position”, position);

        // Run the process

        startActivity(i);

    }

 

            Run the application. Now you can go to the DetailActivity by click each item in listView and back to the MainActivity by click the back button in simulator.

 76-3-android-tutorial

            [Image 3]

 76-4-android-tutorial

            [Image 4]

 

 

참조:http://www.androidapptutorial.com/android-tutorial-list-view-and-detail-application-in-android-studio-part-6/

+ Recent posts