我可以在Android中扩展AppCompactDialogFragment的类中调用另一个类构造函数吗?
我想在stepsDone中调用stespRecord构造函数,我应该将构造函数调用放在哪里,比如StepsRecord rec=newstepsrecord(this)强>我可以在Android中扩展AppCompactDialogFragment的类中调用另一个类构造函数吗?,android,Android,我想在stepsDone中调用stespRecord构造函数,我应该将构造函数调用放在哪里,比如StepsRecord rec=newstepsrecord(this) public class stepsDone extends AppCompatDialogFragment { public Padomet data; public StepsRecord rec; private static final String TAG = "stepsDone&q
public class stepsDone extends AppCompatDialogFragment {
public Padomet data;
public StepsRecord rec;
private static final String TAG = "stepsDone";
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.activity_steps_done, null);
rec= new StepsRecord(this);
builder.setView(view)
.setNegativeButton("Do more", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("Add to the List", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
float g =data.get();
int d = data.getc();
Log.d(TAG, "HELLLOOOO1111"+g+" uuuuuuuuuuuuuuu "+d);
AddData(g,d);
}
});
ImageView imageView = view.findViewById(R.id.imageView);
Glide.with(this).load(R.drawable.tick2).into(imageView);
return builder.create();
}
public void AddData(float arg1,int arg2) {
rec.addData(arg1, arg2);
}
}
这是我要调用其构造函数的类
public class StepsRecord extends SQLiteOpenHelper {
private static final String TABLE_NAME = "PreviousRecord";
private static final String COL1 = "Goal";
private static final String COL2= "stepsTaken";
public StepsRecord(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + "("+ COL1 + " TEXT,"
+ COL2 + " TEXT" + ")";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL(String.format("DROP IF TABLE EXISTS %s", TABLE_NAME));
onCreate(db);
}
public void addData(float goal, int steps) {
SQLiteDatabase db = this.getWritableDatabase();
String gooals = String.valueOf(goal);
String stepstaken = String.valueOf(steps);
ContentValues contentValues = new ContentValues();
//contentValues.put(COL1, strDate);
contentValues.put(COL1, gooals);
contentValues.put(COL2, stepstaken);
db.insert(TABLE_NAME, null, contentValues);
}
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
}
您的帮助将不胜感激。。。我在这方面搜索了很多,但没有找到任何解决方案,可能这是不可能的,也可能是可能的。。如果这是可能的,那么请告诉我或任何其他解决方案。现在
步骤记录的构造函数需要一个上下文作为参数,您正在传递此
,它是指片段的子对象stepsDone
对象
现在,Fragment
类不扩展上下文
,但是活动
类扩展了上下文
记住这一点,您可以在stepsDone
类中重写onAttach()
方法,您将获得与stepsDone
类关联的活动的引用:
public class stepsDone extends AppCompatDialogFragment {
public Padomet data;
public StepsRecord rec;
private static final String TAG = "stepsDone";
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
.......
.......
}
//here override the onAttach() and initialize the StepsRecord
@Override
public void onAttach(Context context){
super.onAttach(context);
//initialize here
rec= new StepsRecord(context);
}
.........
.........
}
现在StepsRecord
的构造函数需要一个上下文作为参数,您正在传递this
,它引用的是stepsDone
对象,它是片段的子对象
现在,Fragment
类不扩展上下文
,但是活动
类扩展了上下文
记住这一点,您可以在stepsDone
类中重写onAttach()
方法,您将获得与stepsDone
类关联的活动的引用:
public class stepsDone extends AppCompatDialogFragment {
public Padomet data;
public StepsRecord rec;
private static final String TAG = "stepsDone";
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
.......
.......
}
//here override the onAttach() and initialize the StepsRecord
@Override
public void onAttach(Context context){
super.onAttach(context);
//initialize here
rec= new StepsRecord(context);
}
.........
.........
}
StepsRecord
的参数是Context
,而不是Fragment
,其作用类似于rec=newstepsrecord(this.getContext())代码>工作?或rec=新步骤记录(requireContext())代码>谢谢。。成功了!StepsRecord
的参数是Context
,而不是Fragment
,其作用类似于rec=newstepsrecord(this.getContext())代码>工作?或rec=新步骤记录(requireContext())代码>谢谢。。成功了!