单击ListView项目时播放声音

时间:2013-08-01 14:01:19

标签: android listview

我很生气。应用程序相当简单,但我不能让我的大脑为这个工作。我只想在按ListView项目时播放声音。这可能很容易,但我不想随意发出声音。声音应放在确切的ListView项目位置。这就是我现在所拥有的:

MainActivity.java:

package com.moomob.despicablemesounds;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import ca.demo.demolistview.complex.R;

public class MainActivity extends Activity {
    private List<Minions> myMinions = new ArrayList<Minions>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        populateMinionsList();
        populateListView();
        registerClickCallback();
    }

    private void populateMinionsList() {
        myMinions.add(new Minions("Assemble the Minions", R.drawable.min1));
        myMinions.add(new Minions("Banana Minions", R.drawable.min2));
        ..................
            ..................
    }

    private void populateListView() {
        ArrayAdapter<Minions> adapter = new MyListAdapter();
        ListView list = (ListView) findViewById(R.id.minListView);
        list.setAdapter(adapter);
    }

    private void registerClickCallback() {
        ListView list = (ListView) findViewById(R.id.minListView);
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked,
                    int position, long id) {
                            // Don't know what I should write in here
            }
        });
    }

    private class MyListAdapter extends ArrayAdapter<Minions> {
        public MyListAdapter() {
            super(MainActivity.this, R.layout.item_view, myMinions);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Make sure we have a view to work with (may have been given null)
            View itemView = convertView;
            if (itemView == null) {
                itemView = getLayoutInflater().inflate(R.layout.item_view,
                        parent, false);
            }

            // Find the minion to work with.
            Minions currentCar = myMinions.get(position);

            // Fill the view
            ImageView imageView = (ImageView) itemView
                    .findViewById(R.id.item_icon);
            imageView.setImageResource(currentCar.getIconID());

            // Make:
            TextView makeText = (TextView) itemView
                    .findViewById(R.id.item_txtMake);
            makeText.setText(currentCar.getMake());

            return itemView;
        }
    }
}

Minions.java(只是为了设置我的构造函数和getter和setter):

    package com.moomob.despicablemesounds;

public class Minions {
    private String make;
    private int iconID;

    public Minions(String make, int iconID) {
        super();
        this.make = make;
        this.iconID = iconID;
    }

    public String getMake() {
        return make;
    }

    public int getIconID() {
        return iconID;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public void setIconID(int iconID) {
        this.iconID = iconID;
    }

}

也许我需要某种SoundAdapter但不知道。希望有人能理解我想要的东西,因为我绝对坚持这个:(

1 个答案:

答案 0 :(得分:3)

你发出这样的声音:

mp = MediaPlayer.create(Test.this, R.raw.mysound);
mp.setOnCompletionListener(new OnCompletionListener() {

     @Override
     public void onCompletion(MediaPlayer mp) {
            mp.release();
     }

});   
mp.start();

但是你还需要知道哪个声音对哪个小兵来说很明显,所以你应该为你的小兵类添加一个属性。

在你的onclick处理程序中,你点击了这个位置,这样你就可以得到点击的小兵。