English 中文(简体)
与习俗有关的问题
原标题:Issue with custom SimpleCursorAdapter

I m trying to fill a grid view using data from a db cursor using a custom SimpleCursorAdapter. My cursor has data (I checked), but nothing is shown in the GridView, and the getView() method is not even called.

谁能提供帮助? 为什么不打字?

增 编

dbAdapter = new DBAdapter(this);    
dbAdapter.open();

Cursor c;
c = dbAdapter.fetchPCList();
startManagingCursor(c);     

String[] from = new String[] {};
int[] to = new int[] {};

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new PCIconAdapter(this, R.layout.pc_icon, c, from, to));

c.close();
dbAdapter.close();

<Adapter>

public class PCIconAdapter extends SimpleCursorAdapter {
    private final Context mContext;
    private final int mLayout;
    private final Cursor mCursor;
    private final int mPCIDIndex;
    private final int mClassNameIndex;
    private final LayoutInflater mLayoutInflater;

    private final class ViewHolder {
        public TextView pc_id_view;
        public TextView clas_name_view;
    }

    public PCIconAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);

        this.mContext = context;
        this.mLayout = layout;
        this.mCursor = c;
        this.mPCIDIndex = mCursor.getColumnIndex(DBAdapter.KEY_PC_LM_ID);
        this.mClassNameIndex = mCursor.getColumnIndex(DBAdapter.KEY_PC_CLAS_NAME);
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (mCursor.moveToPosition(position)) {
            ViewHolder viewHolder;

            if (convertView == null) {
                convertView = mLayoutInflater.inflate(mLayout, null);

                viewHolder = new ViewHolder();
                viewHolder.pc_id_view = (TextView) convertView.findViewById(R.id.pc_id);
                viewHolder.clas_name_view = (TextView) convertView.findViewById(R.id.clas_name);

                convertView.setTag(viewHolder);
            }
            else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            String pc_id = mCursor.getString(mPCIDIndex);
            String clas_name = mCursor.getString(mClassNameIndex);

            viewHolder.pc_id_view.setText(pc_id);
            viewHolder.clas_name_view.setText(clas_name);
        }

        return convertView;
    }
}
最佳回答

如果是,

c.close();
dbAdapter.close();

......

因此,我为什么要关闭这个 cur子?

问题回答

我有同样的问题。 我正在使用阿雷拉Adapter,然后我改成了一个简单的CursorAdapter,但是它没有显示屏幕上的任何东西。

I 删除

c.close();
dbAdapter.close();

如今,它的工作罚款。

because CursorAdapter does not have getView method . it have newView method . so extend SimpleCursorAdapter and overRide newWiev and other methods like

public class ContactListCursorAdapter extends SimpleCursorAdapter implements Filterable {

    private Context context;

    private int layout;

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

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        Cursor c = getCursor();

        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);

        int nameCol = c.getColumnIndex(People.NAME);

        String name = c.getString(nameCol);

        /**
         * Next set the name of the entry.
         */     
        TextView name_text = (TextView) v.findViewById(R.id.name_entry);
        if (name_text != null) {
            name_text.setText(name);
        }

        return v;
    }

    @Override
    public void bindView(View v, Context context, Cursor c) {

        int nameCol = c.getColumnIndex(People.NAME);

        String name = c.getString(nameCol);

        /**
         * Next set the name of the entry.
         */     
        TextView name_text = (TextView) v.findViewById(R.id.name_entry);
        if (name_text != null) {
            name_text.setText(name);
        }
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }

        StringBuilder buffer = null;
        String[] args = null;
        if (constraint != null) {
            buffer = new StringBuilder();
            buffer.append("UPPER(");
            buffer.append(People.NAME);
            buffer.append(") GLOB ?");
            args = new String[] { constraint.toString().toUpperCase() + "*" };
        }

        return context.getContentResolver().query(People.CONTENT_URI, null,
                buffer == null ? null : buffer.toString(), args, People.NAME + " ASC");
    }
}

你的列名阵列和观点阵列都是空的。 因此,从来就永远不会出现对绘图的看法。 这可能是因为你在听取意见时没有退步。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...