mercredi 27 mai 2015

Why is the List View not showing up?

I have 2 problems here. First I'm no sure why the logcat is telling me that I have no 'calories_how_long'(as in the logcat right below). Besides, I also would like to know why my list view isn't displaying my calories burn history after clicking "More".

05-28 12:42:11.769: D/wf(4365): fastWalkBurnCal= 404.96695
05-28 12:42:16.319: D/dbBurn(4365): date burn 1432788136327
05-28 12:42:16.329: D/dbBurn(4365): db BURNed calories= 404.96695
05-28 12:42:16.389: D/dbBurn(4365): db quick BURNed calories= 555.0
05-28 12:42:16.449: E/SQLiteLog(4365): (1) table burn has no column named calories_how_long
05-28 12:42:16.449: E/SQLiteDatabase(4365): Error inserting pace=0 calories_burn_quick=555.0 calories_how_long=0 calories_out_date=1432788136327 calories_burn=404.96695
05-28 12:42:16.449: E/SQLiteDatabase(4365): android.database.sqlite.SQLiteException: table burn has no column named calories_how_long (code 1): , while compiling: INSERT INTO burn(pace,calories_burn_quick,calories_how_long,calories_out_date,calories_burn) VALUES (?,?,?,?,?)
05-28 12:42:16.449: E/SQLiteDatabase(4365):     at com.fcsit.leazy.BurnDatabaseHelper.insertBurnedCalories(BurnDatabaseHelper.java:73)

Here's my code:

BurnDatabaseHelper.java:

public class BurnDatabaseHelper extends SQLiteOpenHelper {

    // Calories Burned
    public static final String KEY_CALORIES_BURN_ID = "_id";
    public static final String KEY_CALORIES_OUT_DATE = "calories_out_date";
    public static final String KEY_CALORIES_HOW_LONG = "calories_how_long";
    public static final String KEY_PACE = "pace";
    public static final String KEY_CALORIES_BURN = "calories_burn";
    public static final String KEY_CALORIES_BURN_QUICK = "calories_burn_quick";

    private static final String DATABASE_NAME = "leazy_calIntake.db";
    private static final String TABLE_CALORIES_BURN = "burn";
    private static final int DATABASE_VERSION = 1;

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

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

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String DATABASE_CREATE_CALORIES_BURN = "CREATE TABLE "
                + TABLE_CALORIES_BURN + " (" //
                + KEY_CALORIES_BURN_ID + " integer primary key autoincrement,"//
                + KEY_CALORIES_OUT_DATE + " long"//
                + KEY_CALORIES_HOW_LONG + " integer,"//
                + KEY_PACE + " integer,"//
                + KEY_CALORIES_BURN + " integer," //
                + KEY_CALORIES_BURN_QUICK + " integer" //
                + " );";

        db.execSQL(DATABASE_CREATE_CALORIES_BURN);

    }

    public void insertBurnedCalories(BurnedCalData burnData) {

        ContentValues values = new ContentValues();

        values.put(KEY_CALORIES_OUT_DATE, burnData.getCalOutDate());
        Log.d("dbBurn", "date burn " + burnData.getCalOutDate());
        values.put(KEY_CALORIES_HOW_LONG, burnData.getHowLong());
        Log.d("dbBurn", "db how long= " + burnData.getHowLong());
        values.put(KEY_PACE, burnData.getPace());
        Log.d("dbBurn", "db pace= " + burnData.getPace());
        values.put(KEY_CALORIES_BURN, burnData.getCaloriesBurn());
        Log.d("dbBurn", "db BURNed calories= " + burnData.getCaloriesBurn());
        values.put(KEY_CALORIES_BURN_QUICK, burnData.getCaloriesQuickBurn());
        Log.d("dbBurn", "db quick BURNed calories= " + burnData.getCaloriesQuickBurn());


        SQLiteDatabase db = this.getWritableDatabase();
        db.insert(TABLE_CALORIES_BURN, null, values);
        db.close();
    }

    public Cursor getAllBurnCaloriesDatas() {
        // List<Data> historyDataList= new ArrayList<Data>();
        String selectQuery = "SELECT * FROM " + TABLE_CALORIES_BURN;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        String[] columnName = cursor.getColumnNames();
        for (String string : columnName) {
            Log.i("column", string);
        }

        return cursor;
    }


}

Here, for the second problem I would like to ask for help is that I have my Activity which calls the MoreBurnFragment.java:

MoreBurnActivity.java:

public class MoreBurnActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(false);

        if (savedInstanceState == null) {

            MoreBurnFragment moreburnfragment = new MoreBurnFragment();
            Bundle extra = getIntent().getExtras();
            moreburnfragment.setArguments(extra);

            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction().add(
                    R.id.fragment_container, moreburnfragment);

            ft.commit();

        }

    }

}

MoreBurnFragment.java:

public class MoreBurnFragment extends Fragment{

    private MoreBurnAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_more_burn, container, false);

        ((ActionBarActivity)getActivity()).getSupportActionBar();

        ListView mListView = (ListView) v.findViewById(R.id.listView_more_burn);

        BurnDatabaseHelper db = new BurnDatabaseHelper(getActivity());
        Cursor cursor = db.getAllBurnCaloriesDatas();

        if (cursor != null) {
            // adapter context
            adapter = new MoreBurnAdapter(getActivity(), cursor);

        }
//      
        mListView.setAdapter(adapter);




        return v;
    }

}

And last my adapter, MoreBurnAdapter.java:

public class MoreBurnAdapter extends CursorAdapter {

    public MoreBurnAdapter(Context context, Cursor cursor) {
        super(context, cursor, true);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // TODO Auto-generated method stub
         long date = cursor.getLong(cursor.getColumnIndexOrThrow(BurnDatabaseHelper.KEY_CALORIES_OUT_DATE));
         Log.d("burnMore","date="+ date);
         int duration = cursor.getInt(cursor.getColumnIndexOrThrow(BurnDatabaseHelper.KEY_CALORIES_HOW_LONG));
         int pace = cursor.getInt(cursor.getColumnIndexOrThrow(BurnDatabaseHelper.KEY_PACE));
         int calBurn = cursor.getInt(cursor.getColumnIndexOrThrow(BurnDatabaseHelper.KEY_CALORIES_BURN));
         int quickBurn = cursor.getInt(cursor.getColumnIndexOrThrow(BurnDatabaseHelper.KEY_CALORIES_BURN_QUICK));
         int total = calBurn + quickBurn;

         ViewHolder history_holder = (ViewHolder) view.getTag();

         Calendar rightNow = Calendar.getInstance();//returns a calendar, is an object/instance created with a Calendar class
         rightNow.setTimeInMillis(date); //accessing rightNow class, set it to date(long).

         SimpleDateFormat format = new SimpleDateFormat("d/MM/yyyy   h:mm a", Locale.getDefault());

         String calBurnDate = format.format(rightNow.getTime());//return a date in string
         history_holder.datetime.setText(String.valueOf(calBurnDate));
         history_holder.duration.setText("Walking Duration : "+duration);
         history_holder.pace.setText("Pace: "+pace);
         history_holder.burn.setText("Calories burned: "+calBurn);
         history_holder.quick_burn.setText("Quick Burned: "+quickBurn);
         history_holder.total.setText("Total: "+total);

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.cal_out_history_row, parent, false);

        ViewHolder holder = new ViewHolder();

        holder.datetime = (TextView) view.findViewById(R.id.textView_datetime);
        holder.duration = (TextView) view.findViewById(R.id.textView_walkhowlong);
        holder.pace = (TextView) view.findViewById(R.id.textView_pace);
        holder.burn = (TextView) view.findViewById(R.id.textView_burn);
        holder.quick_burn = (TextView) view.findViewById(R.id.textView_quick_burn);
        holder.total = (TextView) view.findViewById(R.id.textView_totalburn);


        return view;
    }

    private class ViewHolder {
        TextView datetime, duration, pace, burn, quick_burn, total ;
    }

}

Please help for 2 of my problems. The first for the column 'calories_how_long' and the second for data not showing up and not list view is shown. Sorry if too many codes. Million thanks in advance.

Aucun commentaire:

Enregistrer un commentaire