简体   繁体   中英

Adding data to textview in fragment with sqliteDB

Hey I want to add data to my txtview in my fragment from my sqllite db. The user inputs a value to the db which works, but when I try to convert it over in my fragment it wont work.

DB function to get the data:

 public ArrayList<Double> getLastSquatEntry(){
    ArrayList<Double> data = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_SQUAT, new String[]{COL2},null, null, null, null, null);
    Double add = null;
    while(cursor.moveToNext()){
        if(cursor.moveToLast()){
            add=cursor.getDouble(0);
            data.add(add);
        }
    }
    cursor.close();
    return data;
}

Ive made it work inside my WorkoutActivity but this is not using fragments.This is how I convert it inside of it in onCreate method

   //getting last entry from squat
    ArrayList<Double> dataSquat = mDatabaseHelper.getLastSquatEntry();
    double []arrSquat =  new double [dataSquat.size()];
    for(int j=0; j<dataSquat.size();j++) {
        double convertSquat = dataSquat.get(j);
        arrSquat[j] = convertSquat;
    }

   //converting to double from Double
    for(double d : arrSquat){
        Log.d(TAG, ""+d);
        textViewSquat.setText("Sets left: "+Integer.toString(setsLeftSquat)+" Reps : "+Integer.toString(repsLeft) + " "+d*0.8+"KG ");

    }

In my DemoAdapter onCreateView i tried implementing the same as above but did not work so far I just have this which returns an empty array in the textview

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    DatabaseHelper db = new DatabaseHelper(getActivity());
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_demo, container, false);
    txtView = view.findViewById(R.id.txt_display);

    txtView.setText(db.getLastSquatEntry().toString());


    return view;
}

Fixed it nvm

Solution

  public double getLastSquatEntryDouble(){
    double data = 0;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_SQUAT, new String[]{COL2},null, null, null, null, null);
    Double add = null;
    while(cursor.moveToNext()){
        if(cursor.moveToLast()){

            data =cursor.getDouble(0);
        }
    }
    cursor.close();
    return data;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM