未在列表项上打开的新活动单击

时间:2015-01-25 21:07:17

标签: list android-activity onitemclick

我是Android新手,在列表上找到了很多有用的线程。但不知何故,我无法在列表项单击上打开新活动。

直到现在我的应用程序有4个课程。

  1. 飞溅正常
  2. MyActivity-工作正常
  3. 诗歌 - 作品很好
  4. 测试
  5. 直到诗歌,我的应用程序工作正常。但是在诗歌中,当我点击列表项时它没有做任何事情。

    这是我的代码。 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>
    

1 个答案:

答案 0 :(得分:0)

尝试将自己指定为onItemClickListener

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

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

您的onItemClick永远不会被点击,因为如果没有setOnItemClickListener的电话,您就无法联系到它。自实施AdapterView.OnItemClickListener

以来,您需要明确将自己设置为侦听器