0

ListView在我的 XML 文件中创建了一个,但我想从我的ListView.

我知道我必须使用 anOnItemClickListener但我不知道那时我必须做什么。有人可以帮我吗?

ListView在 Java 中:

package com.example.listentomusic;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class March extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.march);

        ListView list = (ListView) findViewById(R.id.listView1);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
                // TODO Auto-generated method stub
                startActivity(new Intent("android.intent.action.ABOUT"));

            }
        });
    }

}

ListView在 XML 中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/standaard_background" >

     <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/maart"
        android:textSize="30sp"
        android:textColor="#FFFFFF"
        android:background="#000000" />

     <android.support.v7.widget.Space
         android:id="@+id/space1"
         android:layout_width="fill_parent"
         android:layout_height="15dp" />

     <ListView
         android:id="@+id/listView1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:entries="@array/songs_march"
         android:textColor="#FFFFFF" >
     </ListView>

</LinearLayout> 

我的字符串数组:

<string-array name="songs_march">
        <item >1. p!nk - just give me a reason </item>
        <item >2. Bingo Players - Get Up </item>
        <item >3. macklemore  -  thrift shop </item>
        <item >4. troublemaker - olly murs </item>
        <item >5. Netsky - We Can Only Live Today </item>
        <item >6. Dj F.R.A.N.K Feat. Miss Autumn Leaves - Yes You Run </item>
        <item >7. labyrinth – beneath your beautiful </item>
        <item >8. Nicole Scherzinger - Boomerang </item>
        <item >9. DJ Fresh - Gold Dust </item>
        <item >10. Justin Timberlake - suit and tie </item>
        <item >11. cascada – glorious </item>
        <item >12.  DJ Fresh ft Sian Evans - Louder</item>
        <item >13. DJ Fresh ft. Rita Ora - Hot Right Now</item>
        <item >14. one direction - kiss you </item>
        <item >15. Coone - Headbanger </item>
        <item >16. Alicia keys - girl on fire </item>
        <item >17. Skream And Example - Shot Yourself In The Foot Again </item>
        <item >18. Isaac - Ease My Mind</item>
        <item >19.  Headhunterz vs. Psyko Punkz - Disrespect</item>
        <item >20. pitbull - feel this moment </item>
        <item >21. Carrie Underwoods - Two Black Cadillac’s </item>
        <item >22. Alicia keys - Brand New Me </item>
        <item >23. Inna - More Than Friends</item>
        <item >24. Freestylers  -  Cracks (Ft. Belle Humble)(Flux Pavilion Remix)</item>
        <item >25. P!nk - Try</item>
        <item >26. Hien - No More </item>
        <item >27. Xilent - Boss Wave</item>
        <item >28. Christina Aguilera - Just A Fool </item>
        <item >29. Evol - Marina And The Diamond</item>
        <item >30. Kesha - C mon</item>
        <item >31. Matrix And Futurebound  -  Magnetic Eyes</item>
        <item >32. Adele  -  Skyfall  </item>
        <item >33. Baauer - Harlem Shake</item>
        <item >34. Mark With A K - Blow Your Brainz</item>
        <item >35. One Direction - One Way Or Another </item>
        <item >36. Calvin Harris - Drinking From The Bottle </item>
        <item >37. Skrillex And Damian - Make It Bun Dem</item>
        <item >38. Taylor Swift - I Knew You Were Trouble</item>
        <item >39. Coone  -  Monstah</item>
        <item >40. Kelly Clarson - Catch My Breath  </item>


    </string-array>
4

1 回答 1

0

您需要创建一个适配器,在您的情况下我建议使用ArrayAdapter

然后使用 list.setAdapter() 将适配器设置为您的 ListView。之后在 onItemClick 回调中,您将获得被点击项目的位置(从 0 开始)。另请参阅该示例

于 2013-04-21T21:18:25.223 回答