新 Activity 未打开列表项单击

New Activity not opening on List Item Click

我是 Android 的新手,在列表中发现了很多有用的话题。但不知何故,我无法在列表项单击时打开新的 activity。

到目前为止,我的应用程序有 4 个 类。

  1. Splash- 工作正常
  2. MyActivity- 工作正常
  3. 诗歌- 很好
  4. 测试

Till Poems 我的应用程序运行良好。但是在 Poems 上,当我单击一个列表项时它什么也没做。

这是我的代码。 Poems.class

package apps.panky.poemsnrhymes;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class Poems extends ActionBarActivity implements AdapterView.OnItemClickListener {

ListView list;
String poems[] = {"Aiken Drum", "A Was an Apple Pie", "A Wise Old Owl", "A-Tisket, A-Tasket"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    /** Hiding Title bar of this activity screen */
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    /** Making this activity, full screen */
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_poems);

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, poems);
    list = (ListView) findViewById(R.id.listView);
    list.setAdapter(adapter);
    list.setTextFilterEnabled(true);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    switch (position) {
        case 0:
            Intent myIntent = new Intent(Poems.this, test.class);
            startActivity(myIntent);
            break;

        case 1:
            //code specific to 2nd list item
            Intent myIntent1 = new Intent(Poems.this, test.class);
            startActivity(myIntent1);
            break;
        default:
            break;
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_poems, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

这是activity_poems布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/bg"
tools:context="apps.panky.poemsnrhymes.Poems">

<ListView
    android:id="@+id/listView"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_marginTop="47dp">
</ListView>

这是清单文件:

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAINACTIVITY" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Poems"
        android:label="@string/title_activity_poems"
        android:theme="@style/Theme.AppCompat.NoActionBar" >
        <intent-filter>
            <action android:name="apps.panky.poemsnrhymes.POEMS" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".test"
        android:label="@string/title_activity_test" >
    </activity>
</application>

尝试将自己指定为 onItemClickListener

list.setAdapter(adapter);
list.setTextFilterEnabled(true);
list.setOnItemClickListener(this);

(最后一行是唯一的补充)

您的 onItemClick 永远不会被击中,因为如果不调用 setOnItemClickListener,您将无法到达它。您需要明确地将自己设置为监听器,因为您实现了 AdapterView.OnItemClickListener