我是Android开发的新手。
最近我想创建一个应用程序,First Activity计算一个数字并通过意图将其发送给第二个活动。
到目前为止一切顺利。
但在这个阶段,我面临一些问题
也就是说,当我通过getIntent()
收到意图时,就可以了
但我不知道如何在SQLite数据库中保存getIntent()
值?
希望你们能帮助我解决这个问题。
这是我的第一个活动
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class DrinkActivity extends AppCompatActivity {
// Remove the below line after defining your own ad unit ID.
//private static final String TOAST_TEXT = "Test ads are being shown. "
// + "To show live ads, replace the ad unit ID in res/values/strings.xml with your own ad unit ID.";
//this is how many count
int a = 0;
//this is hoq much
int b = 0;
//this is button who is send information and go next page
Button B1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
// Load an ad into the AdMob banner view.
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
// Toasts the test ad message on the screen. Remove this after defining your own ad unit ID.
//Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show();
B1 = (Button) findViewById(R.id.gonextPage);
B1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(DrinkActivity.this,ScrollingActivity.class);
int x = a ;
int y = b;
int z = a * b;
intent.putExtra("drinkhowmany",x);
intent.putExtra("drinkhowmuch",y);
intent.putExtra("drinktotal",z);
startActivity(intent);
}
});
}
//this method count down how many drink plus calculation
public void howmanyPlusButton(View view){
if (a == 100){
return;
}
a = a + 1;
displayHowMany(a);
}
//this method count down how many drink minus calculation
public void howmanyMinusButton(View view){
if (a == -0){
return;
}
a = a - 1;
displayHowMany(a);
}
//this method count down how much price drink plus calculation
public void howmuchPlusButton(View view){
if (b == 100){
return;
}
b = b +1;
displayHowMuch(b);
}
//this method count down how much price minus drink calculation
public void howmuchMinusButton(View view){
if (b == -0){
return;
}
b = b -1;
displayHowMuch(b);
}
//this text show user how many digit
private void displayHowMany(int i){
TextView textView = (TextView) findViewById(R.id.howMany);
textView.setText(""+i);
}
//this text show user how much price digit
private void displayHowMuch(int i){
TextView textView = (TextView) findViewById(R.id.howMuch);
textView.setText("$"+i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_drink, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的第二个活动
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ScrollingActivity extends AppCompatActivity {
//drink text working hear
TextView drinkhowmany,drinkhowmuch,drinktotal;
//data save button and data delete button
Button save,delete;
//database
SQLiteDataBase sqLiteDataBase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
Intent intent = new Intent(ScrollingActivity.this,MenuActivity.class);
startActivity(intent);
}
});
//main text working and shoq user preview hear
//database call hear
sqLiteDataBase = new SQLiteDataBase(this);
//this is drink section
//this is show user hoq many drink user order
drinkhowmany = (TextView) findViewById(R.id.drinkHowMany);
drinkhowmany.setText(""+getIntent().getIntExtra("drinkhowmany",0));
//thisis show user how much drink price
drinkhowmuch = (TextView) findViewById(R.id.drinkHowMuch);
drinkhowmuch.setText("$"+getIntent().getIntExtra("drinkhowmuch",0));
//this is show user total drink price
final int drinkTotalInt = +getIntent().getIntExtra("drinktotal",0);
drinktotal = (TextView) findViewById(R.id.drinkTotal);
drinktotal.setText("$"+drinkTotalInt);
//buttton with database
save = (Button) findViewById(R.id.savedataButton);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean chaker = saveDataFile.addtoTable(`what i need to write hear also ?` );
if (chaker == true){
Toast.makeText(getBaseContext(),"SAVE YOUR DATA",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getBaseContext(),"Your Data Not Save",Toast.LENGTH_SHORT).show();
}
}
});
}
}
SQLITE ACTIVITY
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by AIB Nihan on 10/4/2016.
*/
public class SQLiteDataBase extends SQLiteOpenHelper {
//this is the like box of information this is the box of which user input ther value
private static final String DATABASE_NAME = "mydatabase.db";
private static final String TABLE_NAME = "mytable";
private static final String DRINK_PRICE = "drink";
public SQLiteDataBase(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query;
query = "CREATE TABLE " +TABLE_NAME+ "(" +CIG_PRICE+ " INTEGER, "+ ")";
sqLiteDatabase.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(" DROP FILE IF EXITS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
//int coffee,,int drink,int food,int other
public boolean addtotable(int cig){
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues contentValues = new ContentValues();
//contentValues.put(DRINK_PRICE,drink);
long chaker = sqLiteDatabase.insert(TABLE_NAME,null,contentValues);
if (chaker == 1){
return false;
}else {
return true;
}
}
public Cursor display(){
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
Cursor res = sqLiteDatabase.rawQuery("SELECT * FORM" + TABLE_NAME,null);
return res;
}
}
答案 0 :(得分:1)
根据您的代码,您可以通过传递drinkTotalInt或某个整数值将整数传递给 addtotable()。
e.g
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { boolean chaker =
saveDataFile.addtoTable(drinkTotalIn); if (chaker == true){
Toast.makeText(getBaseContext(),"SAVE YOUR
DATA",Toast.LENGTH_SHORT).show(); }else {
Toast.makeText(getBaseContext(),"Your Data Not
Save",Toast.LENGTH_SHORT).show();
}
}
});
并在addtable()中插入传递值,如下所示,
contentValues.put(DRINK_PRICE, [insert data]);
如果要将intent插入sqlite,请将sqlite schema更改为:
query = "CREATE TABLE " +TABLE_NAME+ "(" +CIG_PRICE+ " **TEXT**, "+ ")";
然后,将意图作为参数传递给addtable()
。
例如,
addtable(getIntent());
在addtable()
:
public boolean addtotable(Intent intent){
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues contentValues = new ContentValues();
String intentDescription = intent.toUri(0);
contentValues.put(DRINK_PRICE,intentDescription);
long chaker = sqLiteDatabase.insert(TABLE_NAME,null,contentValues);
if (chaker == 1){
return false;
}else {
return true;
}
}
从sqlite获取意图:
String intentDescription = cursor.getString(intentIndex);
Intent intent = Intent.parseUri(intentDescription, 0);
现在您可以解析意图以获取所有值。
修改强> 从sqlite获取更多细节的意图:
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
Cursor cursor = sqLiteDatabase .rawQuery("select * from [table-name] ");
if (cursor.moveToFirst()) {
do {
String intentDescription = cursor.getString(0); // getting intent from sqlite at index 0
Intent intent = Intent.parseUri(intentDescription, 0);
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
if(db!=null)
{
db.close();
}
答案 1 :(得分:0)
您需要使用ContentValues。 我建议使用本教程: https://www.tutorialspoint.com/android/android_sqlite_database.htm