Loading json data into sqllite db returning empty recycler view

In a previous version of this code, without the SQLLite database (which worked), I was able to read and parse json data from a remote MySQL database in a ContactAdapter that was displaying the data in a RecyclerView in a ViewPager. Each time you changed pages, the code would run AsyncTask, which would invoke a php script, which in turn would pull data from the MySQL database with different parameters.

But there was always a lag / lag between page changes, so I decided to try loading and saving the data in the SQLLite database on the phone.

I added an OppProvider that extends the ContentProvider and then tried to set up an asynthesis to store data from the MySQL database directly in the SQLLite database. Unfortunately I did something wrong and now when I run the code, the data is not showing in the map view / view or view mode. Everything is empty.

I've checked the previous related questions, but they all refer to slightly different formulations for reading JSON in SQLLite.

I have a feeling the ContentAdapter might be the problem, but I've included links to the other two classes below, Any ideas?
ContentAdapter.java:

     package com.glam.ui;



import android.content.ContentUris;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.app.Activity;

import com.glam.AdvocateCheckout;
import com.glam.ApplyCheckout;
import com.glam.CheckoutActivity;
import com.glam.ConnectCheckout;
import com.glam.GiveCheckout;
import com.glam.LearnCheckout;
import com.glam.OppProvider;
import com.glam.ProductDetail;
import com.glam.R;
import com.glam.ServeCheckout;
import com.glam.ShareProductDetail;
import com.glam.utils.ContactInfo;
import com.squareup.picasso.Picasso;

import android.widget.ImageView;
import android.content.Context;
import android.widget.Toast;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.lang.annotation.Target;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static android.app.PendingIntent.getActivities;
import static android.app.PendingIntent.getActivity;


public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {

Cursor cursor;
    int idColumnIndex;
    int headingColumnIndex;
    int typeColumnIndex;
    int excerptColumnIndex;
    int moreinfoColumnIndex;
    int opptypeColumnIndex;
    int startdateColumnIndex;
    int enddateColumnIndex;
    int contactnameColumnIndex;
    int contactemailColumnIndex;
    int addressColumnIndex;
    int externalurlColumnIndex;

    public void swapCursor(Cursor c){
        cursor = c;
        if (cursor!=null) {
            cursor.moveToFirst();
            idColumnIndex = cursor.getColumnIndex(OppProvider.COLUMN_OPPID);
            headingColumnIndex = cursor.getColumnIndex(OppProvider.COLUMN_HEADING);
            typeColumnIndex = cursor.getColumnIndex(OppProvider.COLUMN_TYPE);
            excerptColumnIndex = cursor.getColumnIndex(OppProvider.COLUMN_EXCERPT);
            moreinfoColumnIndex = cursor.getColumnIndex(OppProvider.COLUMN_MOREINFO);
            opptypeColumnIndex = cursor.getColumnIndex(OppProvider.COLUMN_OPPTYPE);
            startdateColumnIndex = cursor.getColumnIndex(OppProvider.COLUMN_STARTDATE);
            enddateColumnIndex = cursor.getColumnIndex(OppProvider.COLUMN_ENDDATE);
            contactnameColumnIndex = cursor.getColumnIndex(OppProvider.COLUMN_CONTACTNAME);
            contactemailColumnIndex = cursor.getColumnIndex(OppProvider.COLUMN_CONTACTEMAIL);
            addressColumnIndex = cursor.getColumnIndex(OppProvider.COLUMN_ADDRESS);
            externalurlColumnIndex = cursor.getColumnIndex(OppProvider.COLUMN_EXTERNALURL);
        }
        notifyDataSetChanged();
        }


    public void setExtraText(String text){

    }
    private List<ContactInfo> contactList;

    public ContactAdapter(List<ContactInfo> contactList) {
        this.contactList = contactList;
    }


    @Override
    public int getItemCount() {

        //return contactList.size();
        return cursor!=null ? cursor.getCount() : 0;
    }

    @Override
    public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
        ContactInfo ci = contactList.get(i);

        Context context = contactViewHolder.vHeading.getContext();
        long id = getItemId(i);
        cursor.moveToPosition(i);
        contactViewHolder.vHeading.setText(cursor.getString(headingColumnIndex));

        if(cursor.getString(excerptColumnIndex).length()>140)
        {
            contactViewHolder.vStarterExcerpt.setText(cursor.getString(excerptColumnIndex).substring(0,139) + " . . . ");
        }
        else
        {
            contactViewHolder.vStarterExcerpt.setText(cursor.getString(excerptColumnIndex));
        }

        contactViewHolder.vExcerpt.setText(cursor.getString(excerptColumnIndex));
        contactViewHolder.vOppType.setText(cursor.getString(opptypeColumnIndex));
        contactViewHolder.vMoreInfo.setText(cursor.getString(moreinfoColumnIndex));
        contactViewHolder.vStartDate.setText((cursor.getString(startdateColumnIndex)));
        contactViewHolder.vEndDate.setText((cursor.getString(enddateColumnIndex)));
        contactViewHolder.vContactName.setText(cursor.getString(contactnameColumnIndex));
        contactViewHolder.vContactEmail.setText(cursor.getString(contactemailColumnIndex));
        contactViewHolder.vAddress.setText(cursor.getString(addressColumnIndex));
        contactViewHolder.vExternalUrl.setText(cursor.getString(externalurlColumnIndex));
        /*contactViewHolder.vHeading.setText(ci.heading);
        contactViewHolder.vType.setText(ci.type);

        if(ci.excerpt.length()>140)
        {
            contactViewHolder.vStarterExcerpt.setText(ci.excerpt.substring(0,139) + " . . .");
        }
        else
        {
            contactViewHolder.vStarterExcerpt.setText(ci.excerpt);
        }

        contactViewHolder.vExcerpt.setText(ci.excerpt);
        contactViewHolder.vOppType.setText(ci.opptype);
        contactViewHolder.vMoreInfo.setText(ci.moreinfo);
        contactViewHolder.vStartDate.setText((ci.start_date));
        contactViewHolder.vEndDate.setText((ci.end_date));
        contactViewHolder.vContactName.setText(ci.contact_name);
        contactViewHolder.vContactEmail.setText(ci.contact_email);
        contactViewHolder.vAddress.setText(ci.address);
        contactViewHolder.vExternalUrl.setText(ci.external_url);
        //contactViewHolder.vImage.setImageBitmap(ci.img);
*/

        //contactViewHolder.vImage.setImageDrawable(ci.img);
    }

    @Override
    public long getItemId(int position) {
        cursor.moveToPosition(position);
        return cursor.getLong(idColumnIndex);
    }

    @Override
    public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.share_card_view, viewGroup, false);
        itemView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {
                //Context whatthef = v.getContext();
                Context whatthef = v.getContext();
                Bundle b = new Bundle();
                String key = "keystring";
                String theimage = null;


                TextView whazzup = (TextView) v.findViewById(R.id.txtHeading);
                String theheading = whazzup.getText().toString();

                TextView whatmoreinfo = (TextView) v.findViewById(R.id.txtMoreInfo);
                String themoreinfo = whatmoreinfo.getText().toString();

                TextView whatexcerpt = (TextView) v.findViewById(R.id.txtExcerpt);
                String theexcerpt = whatexcerpt.getText().toString();

                TextView whattype = (TextView) v.findViewById(R.id.txtType);
                String thetype = whattype.getText().toString();

                TextView opptypeview = (TextView) v.findViewById(R.id.oppType);
                String theopptype = opptypeview.getText().toString();


                TextView  whatstartdate = (TextView)  v.findViewById(R.id.txtStartDate);
                String thestartdate = (String) whatstartdate.getText().toString();


                TextView whatenddate = (TextView)  v.findViewById(R.id.txtEndDate);
                String theenddate = (String)whatstartdate.getText().toString();


                TextView whatcontactname = (TextView)  v.findViewById(R.id.txtContactName);
                String thecontactname = (String)whatcontactname.getText().toString();

                TextView whatcontactemail = (TextView)  v.findViewById(R.id.txtContactEmail);
                String thecontactemail = (String)whatcontactemail.getText().toString();

                TextView whataddress = (TextView)  v.findViewById(R.id.txtAddress);
                String theaddress = (String)whataddress.getText().toString();

                TextView whatexternalurl = (TextView)  v.findViewById(R.id.txtExternalUrl);
                String theexternalurl = (String) whatexternalurl.getText().toString();

                /*ImageView whatimage = (ImageView) v.findViewById(R.id.OppIMG);
                if (whatimage!=null) {
                    Bitmap imagebitmap = ((BitmapDrawable) whatimage.getDrawable()).getBitmap();


                    theimage = imagebitmap.toString();
                }*/

                theimage = null;

                //Toast.makeText(v.getContext(), "theopptype is: "+theopptype, Toast.LENGTH_LONG).show();
                b.putStringArray(key, new String[]{theheading, themoreinfo, thetype,theimage, theexcerpt, GetDate(thestartdate), GetDate(theenddate), thecontactname, thecontactemail, theaddress,theexternalurl});
                Intent bintent;
                switch(theopptype) {
                    case "serve":
                        bintent = new Intent(whatthef, ServeCheckout.class);
                        break;
                    case "learn":
                        bintent = new Intent(whatthef, LearnCheckout.class);
                        break;
                    case "give":
                        bintent = new Intent(whatthef, GiveCheckout.class);
                        break;
                    case "apply":
                        bintent = new Intent(whatthef, ApplyCheckout.class);
                        break;
                    case "advocate":
                        bintent = new Intent(whatthef, AdvocateCheckout.class);
                        break;
                    case "connect":
                        bintent = new Intent(whatthef, ConnectCheckout.class);
                        break;
                   /* case "null":
                        bintent = new Intent(whatthef, CheckoutActivity.class);
                        break;*/
                    default:
                    bintent = new Intent(whatthef, CheckoutActivity.class);
                        break;


                }
                bintent.putExtras(b);
                whatthef.startActivity(bintent);

            }
        });

        return new ContactViewHolder(itemView);
    }

    public static class ContactViewHolder extends RecyclerView.ViewHolder {

        //protected ImageView vImage;
        protected TextView vHeading;
        protected TextView vType;
        protected TextView vStarterExcerpt;
        protected TextView vExcerpt;
        protected TextView vMoreInfo;
        protected TextView vOppType;
        protected TextView vStartDate;
        protected TextView vEndDate;
        protected TextView vContactName;
        protected TextView vContactEmail;
        protected TextView vAddress;
        protected TextView vExternalUrl;


        public ContactViewHolder(View v) {
            super(v);
            //vImage = (ImageView) v.findViewById(R.id.OppIMG);
            vHeading =  (TextView) v.findViewById(R.id.txtHeading);
            vType = (TextView)  v.findViewById(R.id.txtType);
            vExcerpt = (TextView)  v.findViewById(R.id.txtExcerpt);
            vStarterExcerpt = (TextView) v.findViewById(R.id.txtStartExcerpt);
            vMoreInfo = (TextView)  v.findViewById(R.id.txtMoreInfo);
            vOppType = (TextView)  v.findViewById(R.id.oppType);
            vStartDate = (TextView)  v.findViewById(R.id.txtStartDate);
            vEndDate = (TextView)  v.findViewById(R.id.txtEndDate);
            vContactName = (TextView)  v.findViewById(R.id.txtContactName);
            vContactEmail = (TextView)  v.findViewById(R.id.txtContactEmail);
            vAddress = (TextView)  v.findViewById(R.id.txtAddress);
            vExternalUrl = (TextView)  v.findViewById(R.id.txtExternalUrl);

        }
    }


    public String GetDate (String inputdate) {

        DateFormat srcDF = new SimpleDateFormat("yyyymmdd");
        Date date = null;
        try {
            date = srcDF.parse(inputdate);
            DateFormat destDF = new SimpleDateFormat("MMM dd, yyyy");

            return destDF.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return inputdate;
        }





    }

    private void deleteOpp (Context context, long id) {
        context.getContentResolver()
                .delete (
                        ContentUris.withAppendedId(
                                OppProvider.CONTENT_URI,id),null, null);


    }

}

      

My other code is here:

MainFragment - http://codepad.co/s/d2108a
OppProvider - http://codepad.co/s/f0e298

+3


source to share





All Articles