在没有SQLLite数据库(可行)的此代码的先前版本中,我能够读取并将远程MySQL数据库中的json数据解析为ContactAdapter,该ViewAdapter在ViewPager中的RecyclerView中显示数据。每次更改页面时,代码都会运行一个调用php脚本的AsyncTask,后者又会使用不同的参数从MySQL数据库中提取数据。
但是在更改页面之间总是存在延迟/延迟,因此我决定尝试将数据加载并存储到手机上的SQLLite数据库中。
我添加了OppProvider,它扩展了ContentProvider,然后尝试调整asynctask以将MySQL数据库中的数据直接存储到SQLLite数据库中。不幸的是,我做错了,现在当我运行代码时,cardview / recyclerview或view pager中没有显示任何数据。它一片空白。
我已经检查过以前的相关问题,但它们都与略微不同的将JSON读入SQLLite的方法有关。
我觉得问题可能是ContentAdapter,但我已经包含了下面其他两个类的链接,任何想法?
的 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);
}
}
我的其他代码在这里:
MainFragment - http://codepad.co/s/d2108a
OppProvider - http://codepad.co/s/f0e298