ListView不起作用

时间:2013-02-10 11:05:00

标签: android android-listview

我已经创建了一个简单的列表视图,现在希望按钮对应于一个新的活动,我已经使用了一个case语句来实现我的代码。

    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!
                  }

              }
            });

    }



}

这是我的清单

    <?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>

有谁知道我的问题是什么,为什么我继续让我的应用程序说不响应?

1 个答案:

答案 0 :(得分:1)

修改您的清单文件。

你的清单代码

<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>

在此文件中进行更改..尝试这样

<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);