为什么getAllBurnCaloriesDatas()返回0而列表视图没有显示?

时间:2015-05-28 05:03:59

标签: android sqlite listview

我想知道为什么我的列表视图在点击"更多"后不显示我的卡路里燃烧历史记录。

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;
    }


}

在这里,我想请求帮助的第二个问题是我的Activity调用了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;
    }

}

最后我的适配器,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 ;
    }

}

请帮忙。数据未显示,未显示列表视图。百万提前感谢。

0 个答案:

没有答案