0

Hi In My Application I am using list view in that i used 3 lists named as History", "Our Vision", "Our Branches" now if i click on the history link i want to open the html page.before i used only one class it was open html files correctly after i added one more class in our school.java file then problem occurs.Now my problem is if i click the history page i want to open html file and if i click the our branches it will open another list what i gave in array

OurSchool.java:

public class OurSchool extends ListActivity{

 String[] listItems={"History", "Our Vision", "Our Branches"};
 boolean[] listImages= {true, true, true};


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.our_school);
        setListAdapter(new ImageAdapter(this, R.layout.our_school, R.id.text1, R.id.image1, listItems, listImages ));
        this.getListView().setOnItemClickListener(new OnItemClickListener() {


            public void onItemClick(AdapterView<?> parent, View view,
                    final int pos, long id) {
                Intent i1 = new Intent(getApplicationContext(), History.class);
                // sending data to new activity
                i1.putExtra("pos", pos);
                startActivity(i1);
            }
        });
this.getListView().setOnItemClickListener(new OnItemClickListener() {


            public void onItemClick(AdapterView<?> parent, View view,
                    final int pos, long id) {
                Intent i = new Intent(getApplicationContext(), OurBranches.class);
                // sending data to new activity
                i.putExtra("pos", pos);
                startActivity(i);
            }
        });
    }
    }

History.java: public class History extends Activity {

    WebView mWebView;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);   

        int pos = getIntent().getIntExtra("pos",0);      

        mWebView = (WebView)findViewById(R.id.webview);
        mWebView.setBackgroundColor(0);
        mWebView.setBackgroundResource(R.drawable.bg);
        mWebView.getSettings().setBuiltInZoomControls(true);
        switch (pos) {
        case 0:
            mWebView.loadUrl("file:///android_asset/History.html");
            break;
        case 1:
            mWebView.loadUrl("file:///android_asset/Mission And Objectives.html");
            break;

        case 2:
            /*Intent i = new Intent(getApplicationContext(), OurBranches.class);
            startActivity(i);
            break;*/
        default:
            mWebView.loadUrl("file:///android_asset/History.html");
            break;
        }


    }

      }

history.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center"
        android:text="About :Vivero International Pre-School"
        android:textColor="#0000FF"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />


</LinearLayout>

our_school.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layercontainer"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="#ffffff">
   <ListView
   android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

   <TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:padding="10dp"
    android:textSize="16sp"
    android:textColor="#008000" 
    android:typeface="sans"/>

   <ImageView
    android:id="@+id/image1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"/>
</RelativeLayout>

before i used only one class it was open html files correctly after i added one more class in our school.java file then problem occurs.Now my problem is if i click the history page i want to open html file and if i click the our branches it will open another list what i gave in array

Thank you,

2
  • What error you are getting? Commented May 8, 2014 at 8:12
  • hi i am not getting error if i click history i want to open the html page but now redirecting to branches page Commented May 8, 2014 at 8:17

1 Answer 1

3

The problem is that you override the first OnItemClickListener with the second, you have to do something like that:

public class OurSchool extends ListActivity{

String[] listItems={"History", "Our Vision", "Our Branches"};
boolean[] listImages= {true, true, true};


public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.our_school);
    setListAdapter(new ImageAdapter(this, R.layout.our_school, R.id.text1, R.id.image1, listItems, listImages ));

    this.getListView().setOnItemClickListener(new OnItemClickListener() {


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

            Intent i;

            switch(pos)
            {
                default:
                case 0:
                    i = new Intent(getApplicationContext(), History.class);
                    break;
                case 1:
                    i = new Intent(getApplicationContext(), OurVision.class); // I assumed that this is the class name.
                    break;
                case 2:
                    i = new Intent(getApplicationContext(), OurBranches.class);
                    break;
            }

            // sending data to new activity
            i.putExtra("pos", pos);
            startActivity(i);
        }
    });
}
}

So you just need to put one OnItemClickListener and implement inside a switch to create the different Intents for every Activity:

    this.getListView().setOnItemClickListener(new OnItemClickListener() {


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

            Intent i;

            switch(pos)
            {
                default:
                case 0:
                    i = new Intent(getApplicationContext(), History.class);
                    break;
                case 1:
                    i = new Intent(getApplicationContext(), OurVision.class); // I assumed that this is the class name.
                    break;
                case 2:
                    i = new Intent(getApplicationContext(), OurBranches.class);
                    break;
            }

            // sending data to new activity
            i.putExtra("pos", pos);
            startActivity(i);
        }
    });
Sign up to request clarification or add additional context in comments.

2 Comments

hi can you please tell me what i have to change
I added more info in the answer, please mark the answer as correct if it solved your problem :)

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.