簡體   English   中英

將json數據插入數據庫並在listview中顯示會使logcat錯誤

[英]Inserting json data to database and display in listview make logcat error

我想從那里將​​json數據發送到數據庫部分,我想在列表視圖中顯示數據,因為我的json部分是正確的,我還創建了數據庫來發送json數據以存儲在該數據庫中,但是它顯示logcat錯誤,應用程序也停了

public class MainActivity extends Activity implements FetchDataListener,OnClickListener{
    private static final int ACTIVITY_CREATE=0;
    private static final int TAG_CATEGORY = 0;
    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);
        mDbHelper.open();
        fillData();
        registerForContextMenu(getListView());
        //mDbHelper.saveCategoryRecord(new Category(id,title,content,count));

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



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

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


        // Adding items to listview


        /**
         * Enabling Search Filter
         * */




        new GetDataAsyncTask().execute();
        //praycount.setOnClickListener(this);
        //initView();
    }

    private View getListView() {
        // TODO Auto-generated method stub
        return null;
    }

    /*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);


    }   */

    private class GetDataAsyncTask extends AsyncTask<Void, Void, Boolean> {
        private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);

        protected void onPreExecute() {
            Dialog.setMessage("Loading.....");
            Dialog.show();
        }
        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            Dialog.dismiss();
            Intent intent = new Intent(MainActivity.this, MainActivity.class);
            startActivity(intent);
        }
        @Override
        protected Boolean doInBackground(Void... params) {
            getProdData();
            return null;
        }
    }

    public void getProdData() {
        // getting JSON string from URL
        JSONParser  jsonObject = new JSONParser();
        //JSONObject jsonObject = new JSONObject();
            //JSONArray aJson = jsonObject.getJSONArray("post");
            String url = "http://www.ginfy.com/api/v1/posts.json";
            // getting JSON string from URL
            //JSONArray aJson = jsonObject.getJSONArray(url);
            JSONArray Json = jsonObject.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(new Category(id,title,content,count));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    private void fillData() {
        mDbHelper.open();   
        Cursor projectsCursor = mDbHelper.fetchAllProjects();
        //startManagingCursor(projectsCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{GinfyDbAdapter.CATEGORY_COLUMN_TITLE, GinfyDbAdapter.CATEGORY_COLUMN_CONTENT, GinfyDbAdapter.CATEGORY_COLUMN_COUNT};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text2, R.id.text2, R.id.count};

        /* Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter projects = 
                new SimpleCursorAdapter(this, R.layout.activity_row, projectsCursor, from, to);
        setListAdapter(projects);
        */
        // create the adapter using the cursor pointing to the desired data 
        //as well as the layout information
         dataAdapter  = new SimpleCursorAdapter(
          this, R.layout.activity_row, 
          projectsCursor, 
          from, 
          to,
          0);
         setListAdapter(dataAdapter);




    }



    private void setListAdapter(SimpleCursorAdapter dataAdapter2) {
        // TODO Auto-generated method stub

    }

這是我的數據庫部分

public class GridviewAdapter extends BaseAdapter
{
    private ArrayList<String> listginfy;
    private ArrayList<Integer> listimage;
    private Activity activity;

    public GridviewAdapter(GinfyActivity ginfyActivity,ArrayList<String> listginfy, ArrayList<Integer> listimage) {
        super();
        this.listginfy = listginfy;
        this.listimage = listimage;
        this.activity = (Activity) ginfyActivity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listginfy.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listginfy.get(position);
    }

    public int getItemId() {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder
    {
        public ImageView imgViewGinfy;
        public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if(convertView==null)
        {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.gridview_row, null);

            view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
            view.imgViewGinfy = (ImageView) convertView.findViewById(R.id.imageView1);

            convertView.setTag(view);
        }
        else
        {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewTitle.setText(listginfy.get(position));
        view.imgViewGinfy.setImageResource(listimage.get(position));

        return convertView;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }


}

它顯示logcat錯誤

07-04 11:01:47.825: E/SQLiteLog(1186): (1) near "prayers": syntax error
07-04 11:01:47.855: D/AndroidRuntime(1186): Shutting down VM
07-04 11:01:47.865: W/dalvikvm(1186): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
07-04 11:01:47.975: E/AndroidRuntime(1186): FATAL EXCEPTION: main
07-04 11:01:47.975: E/AndroidRuntime(1186): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsonandroid/com.example.jsonandroid.MainActivity}: android.database.sqlite.SQLiteException: near "prayers": syntax error (code 1): , while compiling: prayers
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.os.Looper.loop(Looper.java:137)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.app.ActivityThread.main(ActivityThread.java:5039)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at java.lang.reflect.Method.invokeNative(Native Method)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at java.lang.reflect.Method.invoke(Method.java:511)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at dalvik.system.NativeStart.main(Native Method)
07-04 11:01:47.975: E/AndroidRuntime(1186): Caused by: android.database.sqlite.SQLiteException: near "prayers": syntax error (code 1): , while compiling: prayers
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at com.example.jsonandroid.GinfyDbAdapter$DatabaseHelper.onCreate(GinfyDbAdapter.java:63)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at com.example.jsonandroid.GinfyDbAdapter.open(GinfyDbAdapter.java:99)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at com.example.jsonandroid.MainActivity.onCreate(MainActivity.java:66)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.app.Activity.performCreate(Activity.java:5104)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-04 11:01:47.975: E/AndroidRuntime(1186):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-04 11:01:47.975: E/AndroidRuntime(1186):     ... 11 more

這三行顯示問題

mDbHelper.open();

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

    }

您的數據庫表有問題,同一表中不能有兩個整數主鍵。

刪除一個主鍵或更改其類型(只需從“ id”或“ count”列類型之一中刪除主鍵)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM