Android专辑Art GridView

时间:2010-09-19 14:59:46

标签: android gridview

我是Android开发的新手,我正在开发媒体播放器应用程序作为学习体验。我目前正在尝试添加一个在gridview中使用专辑封面的菜单视图。这是我的代码:

public class coverMenu extends Activity {

 private Cursor audioCursor;
 public String artistInput;
 private static final String TAG = "coverMenu";

 @Override
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.cover);
        Bundle data = this.getIntent().getExtras();
        String artistKey = data.getString("artistPass");


       Toast.makeText(this, artistKey, Toast.LENGTH_LONG).show();

       audioCursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, 
         MediaStore.Audio.Albums.ARTIST + "='" + artistKey + "'", null,MediaStore.Audio.Albums.ALBUM + " ASC");
        startManagingCursor(audioCursor);

        String[]from = new String[]{MediaStore.Audio.Albums.ALBUM_ART};


        int[] to = new int []{android.R.id.text1};

        ListAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
          audioCursor, from, to);
        GridView gridview = (GridView) findViewById(R.id.gridview);
         gridview.setAdapter(mAdapter);

我的问题是网格项现在列出了艺术品的路径,但没有显示。任何人都可以通过解释如何将其解析为位图并将其发送到gridview来帮助我吗?

我花了一些时间看着股票Android音乐播放器,但找不到一个简单的解决方案。

感谢任何帮助。

[最新调试:]

DalvikVM[localhost:8615]    
    Thread [<3> main] (Suspended (exception RuntimeException))  
        ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2496  
        ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2512   
        ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119 
        ActivityThread$H.handleMessage(Message) line: 1863  
        ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
        Looper.loop() line: 123 
        ActivityThread.main(String[]) line: 4363    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 521  
        ZygoteInit$MethodAndArgsCaller.run() line: 860  
        ZygoteInit.main(String[]) line: 618 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<13> Binder Thread #2] (Running)    
    Thread [<11> Binder Thread #1] (Running)    
    Thread [<15> Binder Thread #3] (Running)

1 个答案:

答案 0 :(得分:0)

您需要创建一个自定义适配器类,该类根据光标的位置返回填充的网格视图项。这是一个非常粗略的例子。

custom_grid_item.xml:

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/album_image"
android:layout_height="wrap_content" android:layout_width="wrap_content">
</ImageView>
</LinearLayout>

MySimpleListAdapter.java:

import java.io.File;
import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.ImageView.ScaleType;

public class MySimpleListAdapter extends SimpleCursorAdapter {

private Context mContext;

public MySimpleListAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to) {
    super(context, layout, c, from, to);
    mContext = context;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    ImageView iv;
    if (convertView != null)
        v = convertView;
    else {
        LayoutInflater layout = LayoutInflater.from(mContext);
        v = layout.inflate(R.layout.custom_grid_item, null);
    }

    this.getCursor().moveToPosition(position);

    iv = (ImageView) v.findViewById(R.id.album_image);

    iv.setImageURI(Uri.fromFile(new File(this.getCursor().getString(9))));
    iv.setScaleType(ScaleType.CENTER_INSIDE);
    return v;

}}

的活动:

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.GridView;
import android.widget.Toast;

public class CoverMenu extends Activity {
private Cursor audioCursor;
public String artistInput;
private static final String TAG = "coverMenu";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Set to some artist you know
String artistKey = "Devo";

Toast.makeText(this, artistKey, Toast.LENGTH_LONG).show();

audioCursor = getContentResolver().query(
        MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null,
        MediaStore.Audio.Albums.ARTIST + "='" + artistKey + "'", null,
        MediaStore.Audio.Albums.ALBUM + " ASC");
startManagingCursor(audioCursor);

String[] from = new String[] { MediaStore.Audio.Albums.ALBUM_ART };

int[] to = new int[] { android.R.id.text1 };

MySimpleListAdapter mAdapter = new MySimpleListAdapter(this,
        android.R.layout.simple_list_item_1, audioCursor, from, to);

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(mAdapter);
}}

查看来自Google I/O on creating custom listviews的精彩视频,将来对您有很大帮助。