【问题标题】:ListView Not Working列表视图不工作
【发布时间】:2013-02-10 11:05:00
【问题描述】:

我已经制作了一个简单的列表视图,现在希望按钮与一个新活动相对应,我使用了一个案例语句来实现这一点,这是我的代码。

    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>

有谁知道我的问题是什么,为什么我的应用总是说没有响应?

【问题讨论】:

    标签: android android-listview


    【解决方案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);
    

    【讨论】:

    • 首先按照 Eran Answer 在您的活动中的指示进行更改,然后更改您的清单文件。再试一次
    • 很高兴听到它对您有所帮助
    猜你喜欢
    • 2022-11-22
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多