vendredi 25 septembre 2015

try to put image for sqlite

Im trying to put image(String) from database with SqlHelper class.But I have some errors when I do this.(Errors below).Im try to many way,but see the same errors.Maybe I cant to put http link to database? 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();
        }

ForItemClick();

    }








    @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();
            }
                if(isNetworkConnected()==true) {
                    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);
                }else
                {
                    displaysavedlv();
                }


        }catch (final IllegalArgumentException e){e.printStackTrace();}
        }
        protected Boolean doInBackground(String... args) {
            dbHelper = new SqlHelper(MainActivity.this);
           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();
                }
            }
bdList = dbHelper.getAllData();
            return null;
        }
    }
private void displaysavedlv(){

CustomListAdapter adapter1 = new CustomListAdapter(MainActivity.this,bdList,R.id.list_item,new String[]{TITLE,DESCRIPTION},new int[]{R.id.title,R.id.description});
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;
    }
    public void ForItemClick(){
        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);


                intent.putExtra("title", title1);
                intent.putExtra("description", description1);
                intent.putExtra("url", url1);
                startActivity(intent);


    }
});
    };
}
SqlHelper:




public class SqlHelper {
String title;
    String description;
    String image;
    private static final String TEXT_TYPE = " TEXT";
    private static final String COMMA_SEP = ",";
    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 static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + DATABASE_TABLE + " (" +
                    KEY_ROWID + " INTEGER PRIMARY KEY," +
                    KEY_TITLE + TEXT_TYPE + COMMA_SEP +
                    KEY_DESCRIPTION + TEXT_TYPE + COMMA_SEP + KEY_IMAGE + TEXT_TYPE +

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

    public SqlHelper(Context c) throws SQLException {
        ourContext = c;
        try {
            open();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    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_IMAGE + " TEXT NOT NULL," + KEY_DESCRIPTION + " 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 image,String description) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_TITLE, title);
        cv.put(KEY_IMAGE, image);
        cv.put(KEY_DESCRIPTION,description);

        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;
    }
}
Errors:
    11910-11929/com.example.vnvbnv.myapplication E/SQLiteLog﹕ (1) table categoryTable has no column named image
09-25 15:56:46.467  11910-11929/com.example.vnvbnv.myapplication E/SQLiteDatabase﹕ Error inserting image=http://ift.tt/1Kz9BEN title=Случайный хомяк 27 description=Случайное фото случайного хомячка. Он тут — для массовости!
    android.database.sqlite.SQLiteException: table categoryTable has no column named image (code 1): , while compiling: INSERT INTO categoryTable(image,title,description) VALUES (?,?,?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:909)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:520)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1523)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1395)
            at com.example.vnvbnv.myapplication.SqlHelper.createEntry(SqlHelper.java:93)
            at com.example.vnvbnv.myapplication.MainActivity$ProgressTask.doInBackground(MainActivity.java:159)
            at com.example.vnvbnv.myapplication.MainActivity$ProgressTask.doInBackground(MainActivity.java:113)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:864)

Aucun commentaire:

Enregistrer un commentaire