我在Stack Overflow上尝试了一些解决方案。但它仍然无法正常工作。我也改变了db名称,就像Stack Overflow中的一篇文章中提到的那样。
DBHelper.java
var r = {
username: '1',
txt: '{"body":"Hi","date":"2016-07-29 07:43:00"}',
};
// parse the JSON string and get the object
var a = JSON.parse(r.txt);
console.log(a.body)
的manifest.xml
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.HashMap;
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "jobsBucketDBNew.db";
//private static final int DATABASE_VERSION = 3;
//public static final String JOBS_TABLE_PRIORITY="priority";
public static final String JOBS_TABLE_NAME = "favtable";
public static final String JOBS_COLUMN_ID = "id";
public static final String JOBS_COLUMN_NAME = "name";
public static final String JOBS_COLUMN_HEADER="header";
public static final String JOBS_COLUMN_COMPANY="company";
public static final String JOBS_COLUMN_CITY="city";
public static final String JOBS_COLUMN_STATE="state";
public static final String JOBS_COLUMN_COUNTRY="country";
public static final String JOBS_COLUMN_FORMATEDLOCATION="formattedLocation";
public static final String JOBS_COLUMN_SOURCE="source";
public static final String JOBS_COLUMN_DATE="date";
public static final String JOBS_COLUMN_SNIPPET="snippet";
public static final String JOBS_COLUMN_URL="url";
public static final String JOBS_COLUMN_ONMOUSEDOWN="onmousedown";
public static final String JOBS_COLUMN_LATITUDE="latitude";
public static final String JOBS_COLUMN_LONGITUDE="longitude";
public static final String JOBS_COLUMN_JOBKEY="jobkey";
public static final String JOBS_COLUMN_SPONSORED="sponsored";
public static final String JOBS_COLUMN_EXPIRED="expired";
public static final String JOBS_COLUMN_FORMATTEDLOCATIONFULL="formattedLocationFull";
public static final String JOBS_COLUMN_FORMATTEDRELATIVETIME="formattedRelativeTime";
public static final String JOBS_COLUMN_CHECKED ="checked";
//SQLiteDatabase db= this.getWritableDatabase();
private HashMap hp;
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 3);
// this.onCreate(db);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
/* String create_table= "create table" +" " + JOBS_TABLE_NAME +
"("+ JOBS_COLUMN_ID +" integer primary key autoincrement," + JOBS_COLUMN_HEADER + " Text," + JOBS_COLUMN_NAME + " Text," + JOBS_COLUMN_COMPANY + " Text," + JOBS_COLUMN_CITY +" Text," + JOBS_COLUMN_STATE + " Text," + JOBS_COLUMN_COUNTRY + " Text," + JOBS_COLUMN_FORMATEDLOCATION + " Text," + JOBS_COLUMN_SOURCE + " Text," + JOBS_COLUMN_DATE+ " Text," + JOBS_COLUMN_SNIPPET+ " Text," + JOBS_COLUMN_URL + " Text," + JOBS_COLUMN_ONMOUSEDOWN + " Text," + JOBS_COLUMN_LATITUDE + " Text," + JOBS_COLUMN_LONGITUDE + " Text," + JOBS_COLUMN_JOBKEY + " Text," + JOBS_COLUMN_SPONSORED +" Text,"+ JOBS_COLUMN_EXPIRED +" Text," +JOBS_COLUMN_FORMATTEDLOCATIONFULL+ " Text," + JOBS_COLUMN_FORMATTEDRELATIVETIME + " Text," + JOBS_COLUMN_CHECKED +" Text" + ");";
db.execSQL(create_table);*/
db.execSQL(
"create table" +" " + JOBS_TABLE_NAME +
"("+ JOBS_COLUMN_ID +" Integer primary key autoincrement," + JOBS_COLUMN_HEADER + " Text," + JOBS_COLUMN_NAME + " Text," + JOBS_COLUMN_COMPANY + " Text," + JOBS_COLUMN_CITY +" Text," + JOBS_COLUMN_STATE + " Text," + JOBS_COLUMN_COUNTRY + " Text," + JOBS_COLUMN_FORMATEDLOCATION + " Text," + JOBS_COLUMN_SOURCE + " Text," + JOBS_COLUMN_DATE+ " Text," + JOBS_COLUMN_SNIPPET+ " Text," + JOBS_COLUMN_URL + " Text," + JOBS_COLUMN_ONMOUSEDOWN + " Text," + JOBS_COLUMN_LATITUDE + " Text," + JOBS_COLUMN_LONGITUDE + " Text," + JOBS_COLUMN_JOBKEY + " Text," + JOBS_COLUMN_SPONSORED +" Text,"+ JOBS_COLUMN_EXPIRED +" Text," +JOBS_COLUMN_FORMATTEDLOCATIONFULL+ " Text," + JOBS_COLUMN_FORMATTEDRELATIVETIME + " Text," + JOBS_COLUMN_CHECKED +" Text" + ");"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + JOBS_TABLE_NAME + ";");
onCreate(db);
//db.execSQL("DROP TABLE IF EXISTS priorities");
//onCreate(db);
}
public boolean insertContact( String header, String name,String company,String city,String state,String country,String formattedLocation,String source,String date,String snippet,String url,String onmousedown,String latitude,String longitude,String jobkey,String sponsored,String expired, String formattedLocationFull,String formattedRelativeTime)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
// contentValues.put(JOBS_COLUMN_ID,id);
contentValues.put(JOBS_COLUMN_HEADER,header);
contentValues.put(JOBS_COLUMN_NAME, name);
contentValues.put(JOBS_COLUMN_COMPANY, company);
contentValues.put(JOBS_COLUMN_CITY, city);
contentValues.put(JOBS_COLUMN_STATE, state);
contentValues.put(JOBS_COLUMN_COUNTRY, country);
contentValues.put(JOBS_COLUMN_FORMATEDLOCATION, formattedLocation);
contentValues.put(JOBS_COLUMN_SOURCE, source);
contentValues.put(JOBS_COLUMN_DATE, date);
contentValues.put(JOBS_COLUMN_SNIPPET, snippet);
contentValues.put(JOBS_COLUMN_URL, url);
contentValues.put(JOBS_COLUMN_ONMOUSEDOWN, onmousedown);
contentValues.put(JOBS_COLUMN_LATITUDE, latitude);
contentValues.put(JOBS_COLUMN_LONGITUDE, longitude);
contentValues.put(JOBS_COLUMN_JOBKEY, jobkey);
contentValues.put(JOBS_COLUMN_SPONSORED, sponsored);
contentValues.put(JOBS_COLUMN_EXPIRED, expired);
contentValues.put(JOBS_COLUMN_FORMATTEDLOCATIONFULL, formattedLocationFull);
contentValues.put(JOBS_COLUMN_FORMATTEDRELATIVETIME, formattedRelativeTime);
// contentValues.put(JOBS_COLUMN_CHECKED, checked);
long c = db.insert("favourites ", null, contentValues);
if(c!=-1)
return true;
else
return false;
}
public Cursor getData(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from "+JOBS_TABLE_NAME+" where id="+id+";", null );
return res;
}
/* public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
int numRows = (int) DatabaseUtils.queryNumEntries(db, JOBS_TABLE_NAME);
return numRows;
}*/
public boolean updateContact (Integer id, String name)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
db.update("favtable", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Integer deleteContact (Integer id)
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("favtable",
"id = ? ",
new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllCotacts()
{
ArrayList<String> array_list = new ArrayList<String>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from favtable", null );
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(JOBS_COLUMN_NAME)));
res.moveToNext();
}
return array_list;
}
}
MarkAsFav.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="portfolio.first_app.practice.com.indeedjobactivity3">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DetailsActivity"
android:label="Single Item Selected"></activity>
<activity android:name=".MarkAsFav"
android:label="Mark as Favourite"></activity>
<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
我正在按照在线教程执行此操作。