0

我在我的列表视图中实现了自定义光标适配器。但列表视图始终为空。让我解释一下我的代码:

    OfflineAdapter offlineadapter = new OfflineAdapter(ctx, null);
    listview.setAdapter(offlineadapter);

这里没什么好解释的。进入我的离线适配器类:

public class OfflineAdapter extends CursorAdapter {

    public com.androidarabia.lazylist.ImageLoader imageLoader;

    @SuppressWarnings("deprecation")
    public OfflineAdapter(Context context, Cursor c) {
        super(context, c);

        imageLoader = new com.androidarabia.lazylist.ImageLoader(
                context.getApplicationContext());

正在初始化...

public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.pricelist, parent, false);
    bindView(v, context, cursor);
    return v;

}

膨胀布局...

@Override
public void bindView(View v, Context context, Cursor c) {
    try {
        String title = c.getString(c
                .getColumnIndexOrThrow(LebanonSamsung.KEY_PHONENAME));
        String date = c.getString(c
                .getColumnIndexOrThrow(LebanonSamsung.KEY_MODELNUMBER));
        String imagePath = c.getString(c
                .getColumnIndexOrThrow(LebanonSamsung.KEY_PRICE));
        String deletion = c.getString(c
                .getColumnIndexOrThrow(LebanonSamsung.KEY_URL));

        TextView title_text = (TextView) v.findViewById(R.id.title);
            title_text.setText(title);


        TextView date_text = (TextView) v.findViewById(R.id.modelnumber);
            date_text.setText(date);


        ImageView item_image = (ImageView) v.findViewById(R.id.list_image);
        imageLoader.DisplayImage(deletion, item_image);

        TextView del_image = (TextView) v.findViewById(R.id.details);
        del_image.setText(imagePath);
    } catch (Exception e) {
        e.printStackTrace();
    }

是的,数据库根本不为空,我运行检查以查看它是否为空,但事实并非如此!我能做些什么?列表视图始终为空。我做错了什么???谢谢!

**


更新 我注意到我没有关闭游标。这是我的数据库代码:

public class LebanonSamsung {
    public static final String KEY_PHONENAME = "phone_name";
    public static final String KEY_PRICE = "phone_price";
    public static final String KEY_MODELNUMBER = "phone_modelnumber";
    public static String prefName = "SecretFile";
    static SharedPreferences prefs;
    public static final String KEY_ROWID = "_id";
    public static final String KEY_URL = "phone_picurl";
    private static final String DATABASE_NAME = "PriceListDatabasesamsunglebanon";
    private static final int DATABASE_VERSION = 1;
    private static DbHelper ourHelper;
    private static Context ourContext;
    private static SQLiteDatabase mDb;
    private static final String SQLITE_SAMSUNGLEBANON = "SamsungLebanon";
    public static final String DATABASE_CREATESAMSUNGLEBANON = "CREATE TABLE IF NOT EXISTS "
            + SQLITE_SAMSUNGLEBANON
            + " ("
            + KEY_ROWID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + KEY_PHONENAME
            + " TEXT NOT NULL, "
            + KEY_URL
            + " TEXT NOT NULL, "
            + KEY_PRICE
            + " TEXT NOT NULL, " + KEY_MODELNUMBER + " TEXT NOT NULL);";

    private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context ourContext) {
            super(ourContext, DATABASE_NAME, null, DATABASE_VERSION);
            prefs = ourContext.getSharedPreferences(prefName,
                    Context.MODE_PRIVATE);

        }

        @Override
        public void onCreate(SQLiteDatabase arg0) {
            // TODO Auto-generated method stub
            arg0.execSQL(DATABASE_CREATESAMSUNGLEBANON);

        }

        @Override
        public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
            // TODO Auto-generated method stub
            arg0.execSQL("DROP TABLE IF EXISTS " + SQLITE_SAMSUNGLEBANON);

            onCreate(arg0);
        }
    }

    public LebanonSamsung(Context onNavigationListener) {
        ourContext = onNavigationListener;
    }

    public LebanonSamsung open() throws SQLException {
        ourHelper = new DbHelper(ourContext);
        mDb = ourHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        ourHelper.close();
    }

    public boolean deletesamsung() {

        int doneDelete = 0;
        doneDelete = mDb.delete(SQLITE_SAMSUNGLEBANON, null, null);
        Log.w("error bi deleting l table", Integer.toString(doneDelete));
        return doneDelete > 0;
    }

    public long createEntrySamsung(String phonename, String phoneprice,
            String modelnumber, String url) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_PHONENAME, phonename);
        cv.put(KEY_PRICE, phoneprice);
        cv.put(KEY_MODELNUMBER, modelnumber);
        cv.put(KEY_URL, url);

        return mDb.insert(SQLITE_SAMSUNGLEBANON, null, cv);

    }

    public Cursor listsamsung() {

        Cursor mCursor = mDb.query(SQLITE_SAMSUNGLEBANON,
                new String[] { KEY_ROWID, KEY_PHONENAME, KEY_PRICE,
                        KEY_MODELNUMBER, KEY_URL }, null, null, null, null,
                null);

        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public void insertsamsung() {
        // TODO Auto-generated method stub
        new insertentries().execute("");

    }

    public class insertentries extends AsyncTask<String, Integer, String> {

        protected void onPreExecute() {
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            InputStream isr = null;
            String result = "";
            try {
                String url;
                HttpClient httpclient = new DefaultHttpClient();
                String range = prefs.getString("range", "1");
                String sortby = prefs.getString("filtering", "ByDate");
                url = "there's a url";
                Log.e("url", url);
                HttpPost httpost = new HttpPost(url);
                HttpResponse resposne = httpclient.execute(httpost);
                HttpEntity entity = resposne.getEntity();
                isr = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(isr, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                isr.close();
                result = sb.toString();

                JSONArray jArray = new JSONArray(result);
                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json = jArray.getJSONObject(i);
                    createEntrySamsung(json.getString("PhoneName"),
                            json.getString("PhonePrice"),
                            json.getString("ModelNumber"),
                            json.getString("imageurl"));
                }
            } catch (Exception e) {
                Log.e("log_tag", "Error converting Result " + e.toString());
            }

            return result;
        }

        protected void onProgressUpdate(Integer... progress) {

        }

        protected void onPostExecute(String result) {

        }
    }
}Anybody

? 日志猫:

08-06 17:13:10.753: E/SQLiteDatabase(532): close() was never explicitly called on database '/data/data/com.androidarabia.phones/databases/PriceListDatabasesamsunglebanon' 
08-06 17:13:10.753: E/SQLiteDatabase(532): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:770)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at com.androidarabia.databases.LebanonSamsung.open(LebanonSamsung.java:81)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at com.androidarabia.drawer.SamsungLB.onCreateView(SamsungLB.java:65)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.View.measure(View.java:12723)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.View.measure(View.java:12723)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.View.measure(View.java:12723)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.View.measure(View.java:12723)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.View.measure(View.java:12723)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.View.measure(View.java:12723)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at com.jeremyfeinstein.slidingmenu.lib.CustomViewAbove.onMeasure(CustomViewAbove.java:456)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.View.measure(View.java:12723)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.View.measure(View.java:12723)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.View.measure(View.java:12723)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.os.Looper.loop(Looper.java:137)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at java.lang.reflect.Method.invokeNative(Native Method)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at java.lang.reflect.Method.invoke(Method.java:511)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-06 17:13:10.753: E/SQLiteDatabase(532):  at dalvik.system.NativeStart.main(Native Method)
08-06 17:13:10.793: E/System(532): Uncaught exception thrown by finalizer
08-06 17:13:10.875: E/System(532): java.lang.IllegalStateException: Don't have database lock!
08-06 17:13:10.875: E/System(532):  at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090)
08-06 17:13:10.875: E/System(532):  at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2182)
08-06 17:13:10.875: E/System(532):  at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2178)
08-06 17:13:10.875: E/System(532):  at android.util.LruCache.trimToSize(LruCache.java:197)
08-06 17:13:10.875: E/System(532):  at android.util.LruCache.evictAll(LruCache.java:285)
08-06 17:13:10.875: E/System(532):  at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143)
08-06 17:13:10.875: E/System(532):  at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126)
08-06 17:13:10.875: E/System(532):  at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914)
08-06 17:13:10.875: E/System(532):  at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
08-06 17:13:10.875: E/System(532):  at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
08-06 17:13:10.875: E/System(532):  at java.lang.Thread.run(Thread.java:856)
4

1 回答 1

0

此行的第二个值应该是从您的数据库中检索的 Cursor

OfflineAdapter offlineadapter = new OfflineAdapter(ctx, null);

尝试:

LebanonSamsung lSamsung= new LebanonSamsung(ctx).open();
Cursor c = lSamsung.listsamsung();
OfflineAdapter offlineadapter = new OfflineAdapter(ctx, c);

您可能遇到的另一个问题是您正在使用后台任务填充数据库,您在哪里调用“insertsamsung()”?,您应该考虑在 onPostExecute 函数中将适配器设置为您的列表。

于 2013-08-06T17:55:30.453 回答