Search code examples
androidandroid-listview

ListView Not Working


I have made a simple listview and now want the buttons to correspond to a new activity I have used a case statement for this to happen heres my code.

    package com.example.lookingatics;

import android.app.Activity;
import android.content.Intent;
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;

public class OtherActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other_activity);

        ListView listView = (ListView) findViewById(R.id.mylist);
        final String[] values = new String[] { "Science", "Maths", "English", "History", "Geography",
                "R.E", "I.C.T", "Arabic", "Art", "MainActivity" };

        // Define a new Adapter
        // First parameter - Context
        // Second parameter - Layout for the row
        // Third parameter - ID of the TextView to which the data is written
        // Forth - the Array of data

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_selectable_list_item, android.R.id.text1, values);


        // Assign adapter to ListView
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
                  final TextView mTextView = (TextView)view;
                  switch (position) {
                    case 0:
                     Intent newActivity0 = new Intent("com.example.lookingatics.OtherActivity");     
                     startActivity(newActivity0);
                    break;
                    case 1:
                     Intent newActivity1 = new Intent("com.example.lookingatics.MainActivity");     
                     startActivity(newActivity1);
                    break;
                    case 2:
                     Intent newActivity2 = new Intent("com.example.lookingatics.OtherActivity");     
                     startActivity(newActivity2);
                    break;
                    case 3:
                     Intent newActivity3 = new Intent("com.example.lookingatics.MainActivity");     
                     startActivity(newActivity3);
                    break;
                    default:
                      // Nothing do!
                  }

              }
            });

    }



}

and here's my manifest

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

    <uses-sdk
        android:minSdkVersion="2"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.lookingatics.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="com.example.lookingatics.OtherActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.OTHER" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Does anyone know what my problem is and why do I keep on getting my App saying not responding?


Solution

  • modify your manifest file.

    YOUR Manifest code

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.lookingatics.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="com.example.lookingatics.OtherActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.OTHER" />
    
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
    

    make changes in this file.. try like this

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.lookingatics.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="com.example.lookingatics.OtherActivity"
            android:label="Other Activity" >
    
        </activity>
    </application>
    

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