New Activity not opening on List Item Click

Panks

I am new to Android and found so many helpful threads on lists. But somehow I am not able to open a new activity on List Item Click.

Till now My app has 4 classes.

  1. Splash- works fine
  2. MyActivity- Works fine
  3. Poems- Works fine
  4. test

Till Poems my app works fine. But on Poems when i click a list item it doesn't do anything.

Here is my code. 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);
}

}

Here is activity_poems layout:

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

Here is Manifest file:

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

Max Worg

Try assigning yourself as the onItemClickListener with:

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

(The last line is the only addition)

Your onItemClick is never hit because you don't have a way to reach it without that call to setOnItemClickListener. You need to explicitly set yourself as the listener since you implement AdapterView.OnItemClickListener

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Android - ListView item click show data to new activity

分類Dev

Click on link not opening in new window

分類Dev

Opening a new fragment when a ListView item is clicked

分類Dev

Open new Activity when pressing on an item

分類Dev

Opening new page with data from selected item (JSON)

分類Dev

Clicking a list view item and passing a database item ID to the next activity?

分類Dev

Adding a button that launches a new List Activity to a Fragment

分類Dev

Can I create a new Activity on the first click of a button and after the first click open the previously created activity?

分類Dev

on-click handler for a list item reagent clojurescript

分類Dev

How to add a new Activity type to the Task work item in TFS 2018

分類Dev

Opening select options on click

分類Dev

Click on element inside list item does not work - VueJS

分類Dev

I want to click my list item in random but can't

分類Dev

Opening popup in new tab

分類Dev

How can i use espresso to click an item from a list view based on item' text?

分類Dev

Activity turns to landscape after opening the camera

分類Dev

Notification using setFullScreenIntent() for BigTextStyle opening Activity automatically

分類Dev

How to avoid re-rendering the whole List instead of adding the new item to the DOM list in react JS?

分類Dev

Chrome keeps opening new tabs as new windows

分類Dev

Android listview item click

分類Dev

Android - displaying wrong data in toast when i click on list view item

分類Dev

Datatables - Child Rows not opening on second click

分類Dev

2 popovers opening on click on single button

分類Dev

In a Flask app, how to print each item of a list in the new paragraphs inside my HTML page

分類Dev

Flutter - Dialog start new Activity

分類Dev

Calling New Activity From ActionBar

分類Dev

onClick to a new activity but the app stopped

分類Dev

#Button new on click listener

分類Dev

TestCafe: Testing a Page Opening a PDF in a New Tab

Related 関連記事

  1. 1

    Android - ListView item click show data to new activity

  2. 2

    Click on link not opening in new window

  3. 3

    Opening a new fragment when a ListView item is clicked

  4. 4

    Open new Activity when pressing on an item

  5. 5

    Opening new page with data from selected item (JSON)

  6. 6

    Clicking a list view item and passing a database item ID to the next activity?

  7. 7

    Adding a button that launches a new List Activity to a Fragment

  8. 8

    Can I create a new Activity on the first click of a button and after the first click open the previously created activity?

  9. 9

    on-click handler for a list item reagent clojurescript

  10. 10

    How to add a new Activity type to the Task work item in TFS 2018

  11. 11

    Opening select options on click

  12. 12

    Click on element inside list item does not work - VueJS

  13. 13

    I want to click my list item in random but can't

  14. 14

    Opening popup in new tab

  15. 15

    How can i use espresso to click an item from a list view based on item' text?

  16. 16

    Activity turns to landscape after opening the camera

  17. 17

    Notification using setFullScreenIntent() for BigTextStyle opening Activity automatically

  18. 18

    How to avoid re-rendering the whole List instead of adding the new item to the DOM list in react JS?

  19. 19

    Chrome keeps opening new tabs as new windows

  20. 20

    Android listview item click

  21. 21

    Android - displaying wrong data in toast when i click on list view item

  22. 22

    Datatables - Child Rows not opening on second click

  23. 23

    2 popovers opening on click on single button

  24. 24

    In a Flask app, how to print each item of a list in the new paragraphs inside my HTML page

  25. 25

    Flutter - Dialog start new Activity

  26. 26

    Calling New Activity From ActionBar

  27. 27

    onClick to a new activity but the app stopped

  28. 28

    #Button new on click listener

  29. 29

    TestCafe: Testing a Page Opening a PDF in a New Tab

ホットタグ

アーカイブ