带有ListView的媒体播放器

时间:2013-10-13 10:41:41

标签: android listview

我在媒体播放器应用程序中制作了一个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;
    }

  }

} 

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

2 个答案:

答案 0 :(得分:0)

不需要StableAdapter,您可以使用简单的Arrayadapter来填充listview。

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

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

package com.example.Quran;

公共类MainActivity扩展了Activity {

@覆盖   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);

}

});

}

答案 1 :(得分: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(音频/视频)课上做类似的事情

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();
            }

        }
    });