NKPDQZ
NKPDQZ
2017-01-19 09:36

安卓新人求大神们的帮助,先谢过大家

  • android

下面这段代码为什么在我点击更新媒体库(就是menu里面的选项)没有反应呢?按理说不是应该用歌名把nameList填充满吗?
我是用真机测试的,本来是有歌曲的。
诚心求解。

 package com.example.tonyn.nkpdqzmusic;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
    private MediaPlayer mediaPlayer;
    private List<String> uriList = new ArrayList<>();
    private List<String> nameList = new ArrayList<>();
    Cursor cursor;
    ArrayAdapter<String> arrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        arrayAdapter = new ArrayAdapter<String>(Main2Activity.this,android.R.layout.simple_list_item_1,nameList);
        ListView listview = (ListView) findViewById(R.id.list_view);
        listview.setAdapter(arrayAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MyService.ACTION_PLAY);
                intent.putExtra("content_uri",uriList.get(position));
                startService(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main2,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.update_list:
                try {
                    ContentResolver contentResolver = getContentResolver();
                    cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
                    if (cursor != null) {
                        while (cursor.moveToNext()) {
                            String uri = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                            String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                            uriList.add(uri);
                            nameList.add(name);
                        }
                        arrayAdapter.notifyDataSetChanged();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                finally{
                if(cursor!=null)
                    cursor.close();
            }
                break;
            default:
        }
        return true;
    }
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答