Never Back Down
Never Back Down

Reputation: 148

Not showing new activity after clicking List Item

i am tryin to make an app , In which We have a list view in main activity On clickin on any of the item , the app should start a new Activity through Intent

The App compiles and runs with no errors , But on clicking on any of the items in the main activity , a new activity is not displayed.!

Here is my Main Activity :

package com.panzer.papers;

import java.util.ArrayList;  
import java.util.Arrays;  
import android.app.Activity;  
import android.content.Intent;
import android.os.Bundle;  
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;  
import android.widget.ListView;  
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {  

  private ListView universityListView ;  
  private ArrayAdapter<String> listAdapter ;  

  /** Called when the activity is first created. */  
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  

    // Find the ListView resource.   
    universityListView = (ListView) findViewById( R.id.universityListView );  
    // Create and populate a List of university names.  
    ArrayList<String> universityList = new ArrayList<String>();   
    universityList.addAll(Arrays.asList(getResources().getStringArray(R.array.university_names)));

    // Create ArrayAdapter using the university list.  
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, universityList);  

    // Add more universitys. If you passed a String[] instead of a List<String>   
    // into the ArrayAdapter constructor, you must not add more items.   
    // Otherwise an exception will occur.  
    listAdapter.add( "Vidarbh University" );  

    // Set the ArrayAdapter as the ListView's adapter.  
    universityListView.setAdapter( listAdapter );  



    universityListView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

            //Toast.makeText(getApplicationContext(),
            //      ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

          // selected item 
          String university = ((TextView) view).getText().toString();
          // Launching new Activity on selecting single List Item
          Intent i = new Intent(getApplicationContext(), SelectedUniversity.class);
          // sending data to new activity
          i.putExtra("university", university);
          startActivity(i);

        }
      });
  }  
}  

Here is the code of my selected activity ,(the java class for the new activity)

package com.panzer.papers;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SelectedUniversity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.simplerow);

        TextView txtProduct = (TextView) findViewById(R.id.selectedUniversityName);

        Intent i = getIntent();
        // getting attached intent data
        String university = i.getStringExtra("university");
        // displaying selected product name
        txtProduct.setText(university);

    }
}

Here is my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff" >

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:gravity="center"
        android:text="Select University"
        android:textSize="25dp"
        android:background="#ffffff" />

    <ListView
        android:id="@+id/universityListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_below="@id/TextView1" />

</RelativeLayout>

Here is the xml file of my selected activity:

<?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/selectedUniversityName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="25dp"
            android:textStyle="bold"
            android:padding="10dip" />

</LinearLayout>

Android Manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.panzer.papers"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SelectedUniversity"
                android:label="Selected_University_Label">

        </activity>
    </application>

</manifest>

Upvotes: 0

Views: 143

Answers (2)

steveen zoleko
steveen zoleko

Reputation: 395

you can change this line listAdapter = new ArrayAdapter(this, R.layout.simplerow, universityList);

with this: listAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, universityList);

if you want your custom listview you can do as follows:

replace the line: listAdapter = new ArrayAdapter(this, R.layout.simplerow, universityList);

with : listAdapter = new ArrayAdapter(this, R.layout.simplerow, R.id.selectedUniversityName, universityList);

and correct the line : String university = ((TextView)view.findViewById(R.id.selectedUniversityName)).getText().toString();

as mentioned above

that's it

Upvotes: 1

Julio Domingo
Julio Domingo

Reputation: 36

You are casting a LinearLayout to TextView.

To fix this problem change this line: String university = ((TextView)view.findViewById(R.id.selectedUniversityName)).getText().toString();

Upvotes: 1

Related Questions