1

我正在通过 JSON 向我的应用程序发送数据,它显示在我的应用程序中ListView,没有互联网无法正常工作,所以我想创建一个数据库来单独存储 JSON 解析数据。我试图创建一个用于 JSON 存储的数据库,但它不正确完毕。我怎么解决这个问题?

Myadapterclass.java

public class GinfyDbAdapter {

    private static final String DATABASE_NAME = "ginfy.db";
    private static final String DATABASE_TABLE_PROJ = "prayers";
    private static final int DATABASE_VERSION = 2;
    public static final String CATEGORY_COLUMN_ID = "id";
    public static final String CATEGORY_COLUMN_TITLE = "title";
    public static final String CATEGORY_COLUMN_CONTENT = "content";
    public static final String CATEGORY_COLUMN_COUNT = "count";


    private static final String TAG = "ProjectDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    public GinfyDbAdapter(MainActivity mainActivity) {
        // TODO Auto-generated constructor stub
    }

    public GinfyDbAdapter GinfyDbAdapter(Context context){
        mDbHelper = new DatabaseHelper(context);

        mDb = mDbHelper.getWritableDatabase();
         return this;
    }

    public void saveCategoryRecord(String id, String title, String content, String count) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(CATEGORY_COLUMN_ID, id);
        contentValues.put(CATEGORY_COLUMN_TITLE, title);
        contentValues.put(CATEGORY_COLUMN_CONTENT, content);
        contentValues.put(CATEGORY_COLUMN_COUNT, count);
        mDb.insert(DATABASE_NAME, null, contentValues);
        }
    public Cursor getTimeRecordList() {
        return mDb.rawQuery("select * from " + DATABASE_NAME, null);
        }
    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }



    private static final String DATABASE_CREATE_PROJ =
            "create table " + DATABASE_TABLE_PROJ + " ("
                    + CATEGORY_COLUMN_ID + " integer primary key autoincrement, "
                    + CATEGORY_COLUMN_TITLE + " text not null, " + CATEGORY_COLUMN_CONTENT + " text not null, " + CATEGORY_COLUMN_COUNT + " integer primary key autoincrement   );" ;

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE_PROJ + "( "
                + CATEGORY_COLUMN_ID + " INTEGER PRIMARY KEY, "
                + CATEGORY_COLUMN_TITLE + " TEXT, " + CATEGORY_COLUMN_CONTENT + " TEXT, " + CATEGORY_COLUMN_COUNT + " INTEGER PRIMARY KEY )" );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS"+ DATABASE_NAME);
        onCreate(db);
    }

}


}

这是我的主要活动,其中包含数据列表视图

   public class MainActivity extends Activity implements FetchDataListener,OnClickListener{
    private static final int ACTIVITY_CREATE=0;
    private static final String TAG_CATEGORY = "post";
    private static final String CATEGORY_COLUMN_ID = "id";
    private static final String CATEGORY_COLUMN_TITLE = "title";
    private static final String CATEGORY_COLUMN_CONTENT = "content";
    private static final String CATEGORY_COLUMN_COUNT = "count";
    private static final int Application = 0;
    private ProgressDialog dialog;
    ListView lv;
    ListView lv1;
    private List<Application> items;
    private Button btnGetSelected;
    private Button praycount;
    public int pct;
    private String stringVal;
    private TextView value;
    private int prayers;
    private int prayerid;
    EditText myFilter;
    ApplicationAdapter adapter;
     private GinfyDbAdapter mDbHelper;
     JSONArray contacts = null;
        private SimpleCursorAdapter dataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_item); 
        mDbHelper=new GinfyDbAdapter(MainActivity.this);

        lv1 =(ListView)findViewById(R.id.list); 
        lv =(ListView)findViewById(R.id.list);

         ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            String url = "http://www.ginfy.com/api/v1/posts.json";
            // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(url);

            try {
                // Getting Array of Contacts
                contacts = json.getJSONArray(TAG_CATEGORY);

                // looping through All Contacts
                for(int i = 0; i < contacts.length(); i++){
                    JSONObject c = contacts.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(CATEGORY_COLUMN_ID);
                    String title = c.getString(CATEGORY_COLUMN_TITLE);
                    String  content = c.getString(CATEGORY_COLUMN_CONTENT);
                    String  count = c.getString(CATEGORY_COLUMN_COUNT);

                    mDbHelper.saveCategoryRecord(id,title,content,count);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(CATEGORY_COLUMN_ID, id);
                    map.put(CATEGORY_COLUMN_TITLE, title);
                    map.put(CATEGORY_COLUMN_CONTENT, content);
                    map.put(CATEGORY_COLUMN_COUNT, count);
                    // adding HashList to ArrayList
                    contactList.add(map);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }





        btnGetSelected = (Button) findViewById(R.id.btnget);
        btnGetSelected.setOnClickListener(this);

        myFilter = (EditText) findViewById(R.id.myFilter);



        initView();
    }

    private void initView(){
        // show progress dialog
        dialog = ProgressDialog.show(this, "", "Loading...");
        String url = "http://www.ginfy.com/api/v1/posts.json";
        FetchDataTask task = new FetchDataTask(this);
        task.execute(url);


    }   

我像这样更改了我的代码,它在 JSONparser 行中显示错误

4

0 回答 0