在Gridview

时间:2016-08-18 17:44:48

标签: android android-gridview

我试图获得所有音乐专辑,但我似乎无法得到它。我已经尝试了很多代码,但是这里是我的最新代码而且它不起作用。这是我得到的错误:android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x1181798

我的Gridadapter:

public class GridviewAdapter extends BaseAdapter {
    private Context context;
    private static final String sdcard = Environment.getExternalStorageDirectory().getPath();
    File interna;
    Bitmap bit;
    public ArrayList<String> songs = new ArrayList<>();
    public ArrayList<Bitmap> bitmapArray = new ArrayList<>();
    ArrayList arr;
    ArrayList images;
    private static LayoutInflater inflater = null;
    private String album;


    public GridviewAdapter(Context c, ArrayList values, ArrayList image){
        context = c;
        images = image;
        arr = values;
        inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount(){
        return arr.size();
    }

    public Object getItem(int position){
        return null;
    }

    public class Holder{
        ImageView im;
        TextView tv;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        Holder holder = new Holder();
        View rowView;

        rowView = inflater.inflate(R.layout.custom_grid, null);
        holder.tv = (TextView)rowView.findViewById(R.id.name);
        holder.im = (ImageView)rowView.findViewById(R.id.img);
        album = arr.get(position).toString().replace("{genre=", "").replace("}", "");
        if(album.equals("null")){
            holder.tv.setText("Unknown");
        }
        else{
        holder.tv.setText(album);
        }
        holder.im.setImageResource((int) images.get(position));

        return rowView;
    }

    public long getItemId(int position){
        return 0;
    }

}

将使用适配器的片段:

public class MyFragment extends Fragment {
    private static final String sdcard = Environment.getExternalStorageDirectory().getPath();
    File interna;
    private ArrayList<HashMap<String, String>> addArtist = new ArrayList<HashMap<String, String>>();
    private ArrayList<HashMap<String, Drawable>> addImage = new ArrayList<HashMap<String, Drawable>>();
    MediaMetadataRetriever metaRetriever;
    HashMap<String, String> genre;
    String albumName;
    TextView lv;
    GridView albums;
    Bitmap bt;
    HashMap<String, Drawable> img;
    Drawable dra;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_lay, container, false);
        //lv = (TextView) v.findViewById(R.id.b);
        updateSongList();
        albums = (GridView)v.findViewById(R.id.alb);
        albums.setAdapter(new GridAdapter(getActivity(), addArtist,addImage));
        return v;
    }

    public void updateAlbumArt(){
        File homeDir = new File(sdcard);
        interna = getActivity().getFilesDir();
        if(homeDir.listFiles(new NameFilter()).length > 0 || interna.listFiles(new NameFilter()).length > 0) {
            for (File file : homeDir.listFiles(new NameFilter())) {
                genre = new HashMap<String, String>();
                metaRetriever = new MediaMetadataRetriever();
                metaRetriever.setDataSource(sdcard + "/"+ file.getName());
                Cursor cursor = getActivity().getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                            new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
                            MediaStore.Audio.Albums._ID+ "=?",
                            new String[] {String.valueOf(MediaStore.Audio.Albums.ALBUM_ID)},
                            null);

                assert cursor != null;
                if (cursor.moveToFirst()) {
                    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                    dra = Drawable.createFromPath(path);
                    }
                img.put("draw", dra);
                addImage.add(img);
                albumName = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
                genre.put("genre", albumName);
                addArtist.add(genre);
            }
        }
    }

}

0 个答案:

没有答案