0

我已经制作了一个ListView媒体播放器应用程序,但我对如何播放该特定媒体文件感到困惑。这是代码:

package com.example.Quran;
public class MainActivity extends Activity {

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

    final ListView listview = (ListView) findViewById(R.id.listview);
    String[] values = new String[] { "Surah1", "Surah2", "Surah3",
        "Surah4", "Surah5", "Surah6", "Surah7", "Surah8",
        "Surah9", "Surah10" };

    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
      list.add(values[i]);
    }
    final StableArrayAdapter adapter = new StableArrayAdapter(this,
        android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {

        // TODO Auto-generated method stub

    }

    });
  }

  private class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
        List<String> objects) {
      super(context, textViewResourceId, objects);
      for (int i = 0; i < objects.size(); ++i) {
        mIdMap.put(objects.get(i), i);
      }
    }

    @Override
    public long getItemId(int position) {
      String item = getItem(position);
      return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
      return true;
    }

  }

} 

请帮助我制作这个古兰经应用程序。

4

2 回答 2

0

ListView只需像这样在主活动中 调用您的..

final ListView lv1 = (ListView) findViewById(R.id.listV_main);

之后onclickListener做这样的事情

lv1.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position,
                long id) {
            // TODO Auto-generated method stub

            if (position == 0) {
                // code specific to first list item

                Intent i = new Intent(ListViewImageActivity.this,
                        Video_play.class);
                i.putExtra("key_video", 1);
                ListViewImageActivity.this.startActivity(i);

            } else

            if (position == 1) {
                // code specific to second list item


                Intent i = new Intent(ListViewImageActivity.this,
                        Video_play.class);
                i.putExtra("key_video", 2);
                ListViewImageActivity.this.startActivity(i);
            } else

            if (position == 2) {
                // code specific to third list item


                Intent i = new Intent(ListViewImageActivity.this,
                        Video_play.class);
                i.putExtra("key_video", 3);
                ListViewImageActivity.this.startActivity(i);
            }

等等......在你的 MediaPlayer(Audio/Video) 类做这样的事情

final Button play_button = (Button) findViewById(R.id.btnPlay);
final VideoView myVideoView1 = (VideoView) findViewById(R.id.MyVidView);

之后将此代码放入:

// Getting Key from List Item to Play Video
    Bundle mydata = getIntent().getExtras();
    myKey = mydata.getInt("key_video");



play_button.setOnClickListener(new OnClickListener() {

            @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (myKey) {
            case 1:
                myVideoView1.setVideoURI(Uri.parse("android.resource://"
                        + getPackageName() + "/" + R.raw.star));
                myVideoView1.start();
                play_button.setVisibility(View.GONE);
                myVideoView1.requestFocus();

                break;
            case 2:
                myVideoView1.setVideoURI(Uri.parse("android.resource://"
                        + getPackageName() + "/" + R.raw.abc));
                myVideoView1.start();
                play_button.setVisibility(View.GONE);
                myVideoView1.requestFocus();
                break;

            case 3:
                myVideoView1.setVideoURI(Uri.parse("android.resource://"
                        + getPackageName() + "/" + R.raw.sheep));
                myVideoView1.start();
                play_button.setVisibility(View.GONE);
                myVideoView1.requestFocus();
                break;


            default:
                myVideoView1.setVideoURI(Uri.parse("android.resource://"
                        + getPackageName() + "/" + R.raw.star));
                myVideoView1.start();
                play_button.setVisibility(View.GONE);
                myVideoView1.requestFocus();
            }

        }
    });
于 2013-10-13T11:40:19.797 回答
0

不需要 StableAdapter,您可以使用简单的 Arrayadapter 来填充这样的列表视图。

并假设您的媒体文件存储在一个数组中说

String[] media = new String[] { "path of song1", "path of song2", "path of song3"
    " };

package com.example.Quran;

公共类 MainActivity 扩展 Activity {

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

final ListView listview = (ListView) findViewById(R.id.listview);
String[] values = new String[] { "Surah1", "Surah2", "Surah3",
    "Surah4", "Surah5", "Surah6", "Surah7", "Surah8",
    "Surah9", "Surah10" };

final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
  list.add(values[i]);
}

listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));  

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

  @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {

   your_method_for playing_media_files(Strong int arg2);

}

});

}

于 2013-10-13T11:32:23.827 回答