gpt4 book ai didi

java - 如何在另一个类中使用来自 'MainActivity class' 的 Intent 传递数据,而不扩展任何类

转载 作者:行者123 更新时间:2023-12-01 22:11:47 25 4
gpt4 key购买 nike

我试图使用下面 MainActivity 类中的 Intent 传递数据到 DBManager 类,但我现在失败了。

所以请告诉我如何使用另一个类的 Intent 数据我会很感激的。

public class MainActivity extends Activity{
private ViewPager mPager;
private DBManager dbManager;
private DatePicker datePicker;
String selectedDate;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pager);

dbManager = new DBManager(getApplicationContext(),"Calorie_test1.db",null,1);

mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(new PagerAdapterClass(getApplicationContext()));
mPager.setCurrentItem(1);
mPager.setPageTransformer(false, new ViewPager.PageTransformer()
{


@Override
public void transformPage(View page, float position)
{
// TODO Auto-generated method stub


float normalizedposition = Math.abs(1 - Math.abs(position));

page.setAlpha(normalizedposition);
page.setScaleX(normalizedposition / 2 + 0.5f);
page.setScaleY(normalizedposition / 2 + 0.5f);
page.setRotationY(position * 80);
}
});
}

/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}*/

private void setCurrentInflateItem(int type){
if(type==0){
mPager.setCurrentItem(0);
}else if(type==1){
mPager.setCurrentItem(1);
}else{
mPager.setCurrentItem(2);
}
}


/**
* PagerAdapter
*/
private class PagerAdapterClass extends PagerAdapter
{

private LayoutInflater mInflater;

public PagerAdapterClass(Context c){
super();
mInflater = LayoutInflater.from(c);
}

@Override
public int getCount() {
return 3;
}

@Override
public Object instantiateItem(View pager, int position) {
View v = null;
ProgressBar progressExercise;
ProgressBar progressFood;
int exerciseCalorie;
int calorie;

if(position==0){
v = mInflater.inflate(R.layout.side_menu, null);

}
else if(position==1){
v = mInflater.inflate(R.layout.activity_main, null);

datePicker = (DatePicker)v.findViewById(R.id.datepicker);
datePicker.init(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
new DatePicker.OnDateChangedListener()
{
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
*selectedDate* = String.format("%d%d%d",year,monthOfYear+1,dayOfMonth);
//Intent intent = new Intent(MainActivity.this,DBManager.class);
//intent.putExtra("selectedDate",uri.toString());
//uri = intent.getParcelableExtra("uri");
Intent intent = new Intent(MainActivity.this,DBManager.class);
intent.putExtra("selectedDate",selectedDate);
}
});

//Intent intent = getIntent();

//int calorieExercise = intent.getIntExtra("calorieExercise",0);

progressExercise = (ProgressBar)v.findViewById(R.id.progressExercise);
progressExercise.setMax(500);
//progressExercise.setProgress(calorieExercise);
progressExercise.setProgress(dbManager.getExercise_Calorie());

//int calorieFood = intent.getIntExtra("calorieFood",0);

progressFood = (ProgressBar)v.findViewById(R.id.progressCalorie);
progressFood.setMax(2400);
progressFood.setProgress(dbManager.getFood_Calorie());

}else if(position== 2){
v = mInflater.inflate(R.layout.program_list, null);

v.findViewById(R.id.btn_food).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, FoodsearchActivity.class);
startActivity(intent);
finish();

}
});
v.findViewById(R.id.btn_exercise).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,ExerciseActivity.class);
startActivity(intent);
finish();
}
});
v.findViewById(R.id.btn_fermeted).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,FermentedActivity.class);
startActivity(intent);
finish();
}
});

}

((ViewPager)pager).addView(v, 0);

return v;
}

@Override
public void destroyItem(View pager, int position, Object view) {
((ViewPager)pager).removeView((View)view);
}

@Override
public boolean isViewFromObject(View pager, Object obj) {
return pager == obj;
}

@Override public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override public Parcelable saveState() { return null; }
@Override public void startUpdate(View arg0) {}
@Override public void finishUpdate(View arg0) {}

}
}


///

public class DBManager extends SQLiteOpenHelper{
public DBManager(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){

super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE test_table(_id INTEGER PRIMARY KEY AUTOINCREMENT, calorie_exercise INTEGER, calorie_food INTEGER, date_ TEXT)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{

}

public void insert(String _query)
{
SQLiteDatabase db = getWritableDatabase();
db.execSQL(_query);
db.close();
}

public int getExercise_Calorie()
{
SQLiteDatabase db = getReadableDatabase();
int calorie = 0;
//Intent selectedDate = Intent.getIntent("selectedDate");
//String date = selectedDate.getStringExtra("selectedDate");
Test aa = new Test();
String date = aa.getSelectedDate();
Log.d("log.d",String.valueOf(date));

Cursor cursor = db.rawQuery("select * from test_table where date_ = '" + date + "';", null);
//Cursor cursor = db.rawQuery("select * from test_table",null);
while(cursor.moveToNext())
{
calorie += cursor.getInt(1);
}
return calorie;
}

public int getFood_Calorie()
{
SQLiteDatabase db = getReadableDatabase();
int calorie = 0;

Cursor cursor = db.rawQuery("select * from test_table",null);
while(cursor.moveToNext())
{
calorie += cursor.getInt(2);
}
return calorie;
}

}

最佳答案

将数据放入 Intent 后尚未开始下一个 Activity。

Intent intent = new Intent(MainActivity.this,DBManager.class);
intent.putExtra("selectedDate",selectedDate);
startActivity(intent);

关于java - 如何在另一个类中使用来自 'MainActivity class' 的 Intent 传递数据,而不扩展任何类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31696464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com