filling listview using cursoradapter

不问归期 提交于 2019-12-13 03:37:47

问题


i implemented Custom cursor adapter in my listview. but the listview is always null. Let me explain my codes:

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

There's nothing really to explain Here. Moving into my offlineadapter Class:

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());

Initializing...

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;

}

Inflating layout...

@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();
    }

Yes, the database isn't null at all, i ran a check to view if it's null but it's not! What can i do? the listview is always empty. what i'm doing wrong??? Thanks!

**


UPDATE i noticed that im not closing cursor. Here's my database codes:

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

? LOGCAT:

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)

回答1:


This line the second value should be a Cursor retrived from your DB

OfflineAdapter offlineadapter = new OfflineAdapter(ctx, null);

Try:

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

Another issue you might have is that you are populating your DB with a background task, where are you calling "insertsamsung()"? , you should consider setting the adapter to your list in the onPostExecute function.



来源:https://stackoverflow.com/questions/18034372/filling-listview-using-cursoradapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!