ArrayAdapter

时间:2015-06-06 14:17:44

标签: android android-arrayadapter android-cursor

你好我需要帮助显示palylists名称,我最近由于获取播放列表ID时出错而从CursorAdapter更改为Array Adapter,但现在我需要知道如何获取播放列表名称;我使用Cursor.GetColumnIndexOrThrow() 但它返回错误我不知道任何事情:

PlaylistFragment:

public class PlaylistFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<Playlist>>,OnItemClickListener {

private PlaylistAdapter mAdapter;
GridView gridview;
public static long playlistID;
private Playlist mPlaylist;
Cursor cursor;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View myFragmentView = inflater.inflate(R.layout.fragment_album, container, false);
    cursor = Utils.makePlaylistCursor(getActivity());
    mAdapter = new PlaylistAdapter(getActivity(), cursor);
    gridview = (GridView) myFragmentView.findViewById(R.id.albumList);

    getLoaderManager().initLoader(0, null, this);
    return myFragmentView;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    gridview.setAdapter(mAdapter);
    gridview.setOnItemClickListener(this);

}


@Override
public Loader<List<Playlist>> onCreateLoader(final int id, final Bundle args) {
    return new PlaylistLoader(getActivity());
}


    @Override
public void onLoadFinished(final Loader<List<Playlist>> loader, final List<Playlist> data) {
    // Check for any errors
    if (data.isEmpty()) {
        return;
    }


    // Add the data to the adpater
    for (final Playlist playlist : data) {
        mAdapter.add(playlist);
    }

}

/**
 * {@inheritDoc}
 */
@Override
public void onLoaderReset(final Loader<List<Playlist>> loader) {
    // Clear the data in the adapter

}




@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    final Bundle bundle = new Bundle();
    mPlaylist = mAdapter.getItem(position);
    String playlistName;
    // Favorites list

    // User created
    playlistName = mPlaylist.mPlaylistName;
    bundle.putString(Config.MIME_TYPE, MediaStore.Audio.Playlists.CONTENT_TYPE);
    bundle.putLong(Config.ID, mPlaylist.mPlaylistId);


    bundle.putString(Config.NAME, playlistName);

    // Create the intent to launch the profile activity
    final Intent intent = new Intent(getActivity(), ProfileActivity.class);
    intent.putExtras(bundle);
    startActivity(intent);
}

PlaylistAdapter:

public class PlaylistAdapter extends ArrayAdapter<Playlist> {
Context context;
String PlaylistTitle;
ImageView PlaylistCover;
Cursor cursor;


public PlaylistAdapter(final Context context, final Cursor cursor1) {
    super(context, 0);
    cursor = cursor1;

}
@Override
public View getView(final int position, View view, final ViewGroup parent) {


    view = LayoutInflater.from(getContext()).inflate(R.layout.griditemlayout, parent, false);
    ViewHolder holder = (ViewHolder) view.getTag();
    if (holder == null) {
        holder = new ViewHolder();
        holder.playlistTitle = (TextView) view.findViewById(R.id.albumgrid);
        holder.playlistImage = (ImageView) view.findViewById(R.id.icongrid);
    }
    view.setTag(holder);
    PlaylistTitle = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.PlaylistsColumns.NAME));

    StringBuilder titleBuild = new StringBuilder();
    titleBuild.append(PlaylistTitle);
    titleBuild.setCharAt(0, Character.toUpperCase(titleBuild.charAt(0)));
    if (titleBuild.length() > 15) {
        titleBuild.setLength(15);
        PlaylistTitle = titleBuild.toString() + "...";
    }


    holder.playlistImage.setBackgroundResource(R.drawable.default_artwork);


    holder.playlistTitle.setText(PlaylistTitle);

       return view;
}

static class ViewHolder {
    TextView playlistTitle;
    ImageView playlistImage;

}


   }

光标:

public static final Cursor makePlaylistCursor(final Context context) {
    return context.getContentResolver().query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
            new String[] {
                    /* 0 */
                    BaseColumns._ID,
                    /* 1 */
                    MediaStore.Audio.PlaylistsColumns.NAME
            }, null, null, MediaStore.Audio.Playlists.DEFAULT_SORT_ORDER);
}

logcat的:

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java)
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java)
        at android.database.CursorWrapper.getString(CursorWrapper.java)
        at com.ray.mymp.adapters.PlaylistAdapter.getView(PlaylistAdapter.java:44)
        at android.widget.AbsListView.obtainView(AbsListView.java)
        at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
        at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:656)
        at android.widget.AbsListView.obtainView(Unknown Source)
        at android.widget.GridView.onMeasure(GridView.java)
        at android.view.View.measure(View.java)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java)
        at android.view.View.measure(View.java)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1455)
        at android.view.View.measure(View.java)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java)
        at android.view.View.measure(View.java)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java)
        at android.view.View.measure(View.java)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java)
        at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
        at android.view.View.measure(View.java)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java)
        at android.view.View.measure(View.java)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java)
        at android.view.View.measure(View.java)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java)
        at android.view.View.measure(View.java)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java)
        at android.view.View.measure(View.java)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java)
        at android.view.Choreographer.doCallbacks(Choreographer.java)
        at android.view.Choreographer.doFrame(Choreographer.java)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java)
        at android.os.Handler.handleCallback(Handler.java)
        at android.os.Handler.dispatchMessage(Handler.java)
        at android.os.Looper.loop(Looper.java)
        at android.app.ActivityThread.main(ActivityThread.java)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
        at de.robv.android.xposed.XposedBridge.main(Xpos

1 个答案:

答案 0 :(得分:1)

问题在于这一行

PlaylistTitle = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.PlaylistsColumns.NAME));

在访问之前,您必须将光标移动到当前位置。添加

cursor.moveToPosition(position);
cursor.getString之前

由于您正在处理Cursor我强烈建议您扩展CursorAdater或其具体实现之一,而不是ArrayAdapter。性能方面,您还应该在每次调用getView时避免分配新视图。

if (view == null) {
    view = LayoutInflater.from(getContext()).inflate(R.layout.griditemlayout, parent, false);

    holder = new ViewHolder();
    holder.playlistTitle = (TextView) view.findViewById(R.id.albumgrid);
    holder.playlistImage = (ImageView) view.findViewById(R.id.icongrid);
    view.setTag(holder);
}
holder = (ViewHolder) view.getTag();