0

Hi I came across the listview example referencing a string array defined by an external class. I was wanting to inquire how would one change it to when the items in the list are clicked it would bring up a new page with info on it.

Thank you in advance sorry for the noob question I'm new at android/Java.

strings.xml
        <?xml version="1.0" encoding="utf-8"?>
        <resources>
            <string name="hello">Hello World, HelloListView!</string>
            <string name="app_name">HelloListView</string>
        <string-array name="countries_array">
                <item>Bahrain</item>
                <item>Bangladesh</item>
                <item>Barbados</item>
                <item>Belarus</item>
                <item>Belgium</item>
                <item>Belize</item>
                <item>Benin</item>
            </string-array>
        </resources>




            package com.hope;

            import android.app.Activity;
            import android.app.ListActivity;
            import android.os.Bundle;
            import android.view.View;
            import android.widget.AdapterView;
            import android.widget.AdapterView.OnItemClickListener;
            import android.widget.ArrayAdapter;
            import android.widget.ListView;
            import android.widget.TextView;
            import android.widget.Toast;

            public class HelloListView extends ListActivity {
                /** Called when the activity is first created. */
                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);

                    String[] countries = getResources().getStringArray(R.array.countries_array);
                    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));

                    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();
                      }
                    });
                  }
            }
        list_item.xml
        <?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:padding="10dp"
            android:textSize="16sp" >
        </TextView>
1
  • 1
    when you say 'new page' what do you mean? A dialog? a new activity? something else? If you mean a new activity @Reno has got you on the right path. You'd probably want a switch(position) and for each of the rows you'd have a case row_ID: specify the correct activity for this item and start it. Commented Feb 19, 2011 at 5:55

1 Answer 1

1

You will need an onClick Listener like this. Read about adapters first

 OnItemClickListener itemListener = new OnItemClickListener() {  
        public void onItemClick(AdapterView<?> parent, View v,
          int position, long rowid) {
            Intent intent = new Intent().setClass(this, YourNewActivity.class);
            startActivity(intent);      
        }
    };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.