简体   繁体   English

使用 sqliteDB 将数据添加到片段中的 textview

[英]Adding data to textview in fragment with sqliteDB

Hey I want to add data to my txtview in my fragment from my sqllite db.嘿,我想在我的 sqllite db 的片段中将数据添加到我的 txtview 中。 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: DB function 获取数据:

 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我已经让它在我的 WorkoutActivity 中工作,但这不是使用片段。这就是我在 onCreate 方法中转换它的方式

   //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在我的 DemoAdapter onCreateView 中,我尝试实现与上面相同的方法,但到目前为止还没有工作,我只有这个,它在 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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