jeudi 24 septembre 2015

one line caused 4 issues

Im trying insert data, what do I get through parsing json into my DB. But when I insert this line, then 4 error appeared. Line with "!!!!". Problem lines with "*", or you can tell me how can I save ArrayList in db. (Some lines below !!!,jsonlist"). Do you have solutions for this? MainActivity:

    public class MainActivity extends ListActivity {
    private Context context;
    SqlHelper dbHelper;
        Intent intent;
        private static String url = "http://ift.tt/1MiJRNN";
        private static final String TITLE = "title";
        private static final String DESCRIPTION = "description";
        private static final String IMAGE = "image";
        ArrayList<HashMap<String,String>> jsonlist = new ArrayList<HashMap<String, String>>();
        ArrayList<HashMap<String,String>> bdList;
       ListView lv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if(isNetworkConnected()==true) {
                new ProgressTask(MainActivity.this).execute();
            }
    lv=(ListView) findViewById(android.R.id.list);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String title1 = jsonlist.get(position).get("title");
                    String description1 = jsonlist.get(position).get("description");
                    String url1 = jsonlist.get(position).get("image");

                    intent = new Intent(MainActivity.this, DetailInfo.class);

                    // bdList = dbHelper.getAllData();
                    intent.putExtra("title", title1);
                    intent.putExtra("description", description1);
                    intent.putExtra("url", url1);
                    startActivity(intent);

        }
    });



        }



        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);

            return true;
        }



        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.reload) {
             new ProgressTask(MainActivity.this).execute();
            }
    else if(id == R.id.menu_item_share){
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, "Put whatever you want");
                startActivity(Intent.createChooser(intent,"Share via"));
            }
            return super.onOptionsItemSelected(item);
        }

        private class ProgressTask extends AsyncTask<String,Void,Boolean> {
            private ProgressDialog dialog;
            private ListActivity activity;
            private Context context;
            public ProgressTask(MainActivity activity) {
                this.activity = activity;
                context = activity;
                dialog = new ProgressDialog(context);
            }

            protected void onPreExecute(){
               this.dialog.setMessage("Progress start");
               this.dialog.show();
            }
            protected void onPostExecute(final Boolean success){
                try{
           if((this.dialog != null)&& this.dialog.isShowing()){
    this.dialog.dismiss();
                }
                CustomListAdapter adapter =  new CustomListAdapter(MainActivity.this,jsonlist, R.layout.list_item,new String[]{TITLE,DESCRIPTION},new int[]{R.id.title,R.id.description});
    lv.setAdapter(adapter);
    //setListAdapter(adapter);


            }catch (final IllegalArgumentException e){e.printStackTrace();}
            }
            protected Boolean doInBackground(String... args) {
               JSONParser jParser = new JSONParser();
                JSONArray json = jParser.getJSONFromUrl(url);
                for(int i =0;i<json.length();i++) {
                    try {
                        JSONObject c = json.getJSONObject(i);
                        String vtitle = c.getString(TITLE);
                        String vdescription = c.getString(DESCRIPTION);
                        String vimage = c.getString(IMAGE);
                      !!  dbHelper.open();
   !!!! dbHelper.createEntry(vtitle,vdescription,vimage);
                      !!  dbHelper.close();
                        HashMap<String, String> map = new HashMap<>();
                        map.put(TITLE, vtitle);
                        map.put(DESCRIPTION, vdescription);
    map.put(IMAGE, vimage);
                        jsonlist.add(map);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }

                return null;
            }
        }
    /*private void displaysavedlv(){
        Cursor cursor = dbHelper.fetchAllCountries();
    CustomCursorAdapter adapter1 = new CustomCursorAdapter(MainActivity.this,cursor);
    lv.setAdapter(adapter1);


    }*/
        private boolean isNetworkConnected() {
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo ni = cm.getActiveNetworkInfo();
            if (ni == null) {
                // There are no active networks.
                return false;
            } else
                return true;
        }
    }

SQLHelper:

public class SqlHelper {
String title;
    String description;
    String image;
    public static final String KEY_ROWID = "_id";
    public static final String KEY_TITLE = "title";
    public static final String KEY_DESCRIPTION = "description";
    public static final String KEY_IMAGE = "image";
private SqlHelper mDb;
    private static final String DATABASE_NAME = "DBCategory";
    private static final String DATABASE_TABLE = "categoryTable";
    private static final int DATABASE_VERSION = 1;

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    public SqlHelper(Context c){
        ourContext = c;
    }

    public SqlHelper open() throws SQLException{
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        ourHelper.close();
    }





    private static class DbHelper extends SQLiteOpenHelper{

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                            KEY_TITLE + " TEXT NOT NULL," +KEY_DESCRIPTION +"TEXT NOT NULL ," +KEY_IMAGE + "TEXT NOT NULL );"
            );
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);
        }

    }

    public long createEntry(String title, String description,String image) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_TITLE, title);
        cv.put(KEY_DESCRIPTION,description);
        cv.put(KEY_IMAGE, image);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);
    }

    public ArrayList<HashMap<String, String>> getAllData()
    {
        ArrayList<HashMap<String, String>> array_list = new ArrayList<HashMap<String, String>>();

        //hp = new HashMap();
        SQLiteDatabase db = this.ourHelper.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from categoryTable", null );
        res.moveToFirst();

        while(res.isAfterLast() == false){

            HashMap<String,String>  hashmap = new HashMap<String, String>();
            hashmap.put("title", res.getString(res.getColumnIndex(title)));
            hashmap.put("description", res.getString(res.getColumnIndex(description)));
            hashmap.put("image", res.getString(res.getColumnIndex(image)));


            array_list.add(hashmap);
            res.moveToNext();
        }
        return array_list;
    }
}

Aucun commentaire:

Enregistrer un commentaire