我正在开发一个Android应用程序,其中我创建了一个活动,在该活动中,我一直显示在行下面的两个文本视图//在Activity.java中的select_category页面的会话:
以前我在这个textview上设置了日期和时间,但是现在我从getMandaliName()方法获取mandali名称的数据库中的值并尝试将其保留在此textview上,然后它仍然显示日期以及我之前设置的时间。我不知道为什么它在textview上显示先前设置的数据而不是在textview上显示获取的数据。 我的Activity.java在这里:
package com.bis.localsale;
import java.security.PublicKey;
import com.bis.localsale.util.CommonUtil;
import com.bis.localsale.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class SelectCategory extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_category);
DBHelper db = new DBHelper(this);
Button btnMandaliName = (Button) findViewById(R.id.mandali_name);
final String mandali = db.getMandaliName();
System.out.println(mandali);
Toast.makeText(getApplicationContext(),mandali,
Toast.LENGTH_SHORT).show();
btnMandaliName.setText(mandali);
//Session For select_category page
TextView txtSession = (TextView) findViewById(R.id.txtSession);
txtSession.setText(mandali);
TextView textTime = (TextView) findViewById(R.id.txtTime);
textTime.setText(mandali);
DisplayMetrics dmAdmin =new DisplayMetrics();
WindowManager windowManager = (WindowManager) this.getSystemService(this.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
final EditText editTextLitre = (EditText) findViewById(R.id.edit_text_litre);
editTextLitre.setInputType(InputType.TYPE_CLASS_NUMBER);
final TextView tvwAmount = (TextView)findViewById(R.id.amount);
editTextLitre.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if(s.length()>0){
if(s.length()<5){
String text = String.valueOf(Float.valueOf(s.toString())*10);
tvwAmount.setText(text);
Toast.makeText(getApplicationContext(),
mandali,
Toast.LENGTH_SHORT).show();
}
else{
InputFilter[] FilterArray = new InputFilter[0];
FilterArray[0] = new InputFilter.LengthFilter(4);
editTextLitre.setFilters(FilterArray);
/*editTextLitre.setText(s.subSequence(0, 4));*/
}
}
else{
tvwAmount.setText("0");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Button btnSaveDetails = (Button) findViewById(R.id.btnSave);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.select_category_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.admin:
Intent intent1 = new Intent("com.example.localsale.Admin");
startActivity(intent1);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
我的数据库助手类显示如下代码 我的DBHelper.java在这里:
package com.bis.localsale;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper
{
private SQLiteDatabase db;
public static final String KEY_ROWID = "_id";
public static final String KEY_CREATED_USER = "createdUser";
public static final String KEY_CREATED_DATE = "createdDate";
public static final String KEY_LAST_MODIFIED_USER= "lastModifiedUser";
public static final String KEY_LAST_MODIFIED_DATE= "lastModifiedDate";
public static final String KEY_RECORD_STATUS = "recordStatus";
public static final String KEY_NAMEOFMANDALI = "nameOfMandali";
public static final String KEY_RATEOFCOW = "rateOfCow";
public static final String KEY_RATEOFBUFFALO = "rateOfBuffalo";
DBHelper DB = null;
private static final String DATABASE_NAME = "localsale.db";
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_TABLE_NAME = "organizationSetup";
private static final String DATABASE_TABLE_CREATE = "CREATE TABLE "
+ DATABASE_TABLE_NAME
+ "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "nameOfMandali TEXT NOT NULL, rateOfCow TEXT NOT NULL, rateOfBuffalo TEXT NOT NULL, createdUser TEXT NOT NULL, createdDate TEXT NOT NULL, lastModifiedUser TEXT NOT NULL, lastModifiedDate TEXT NOT NULL, recordStatus TEXT NOT NULL);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_TABLE_CREATE);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_CREATE);
// Create tables again
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
public Cursor rawQuery(String string, String[] strings) {
// TODO Auto-generated method stub
return null;
}
public SQLiteDatabase open() {
SQLiteDatabase db = this.getWritableDatabase();
return db;
}
public Cursor getDetails(String text) throws SQLException {
Cursor mCursor = db.query(DATABASE_TABLE_NAME, new String[] {
KEY_ROWID, KEY_CREATED_USER, KEY_CREATED_DATE, KEY_LAST_MODIFIED_USER, KEY_LAST_MODIFIED_DATE,
KEY_RECORD_STATUS }, null, null, null, null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public void insert(String DATABASE_TABLE_NAME, Object object,
ContentValues values) {
// TODO Auto-generated method stub
}
@SuppressLint("NewApi")
public String getMandaliName(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(DATABASE_TABLE_NAME, new String[]
{ KEY_NAMEOFMANDALI}, null);
if (cursor != null)
cursor.moveToLast();
String mandaliName = cursor.getString(cursor.getColumnIndex(KEY_NAMEOFMANDALI));
Log.e("test", mandaliName);
return mandaliName;
} }
我没有得到代码中的错误,它(取出的数据)没有显示在textview上。请尽快提供答案,帮助我。
答案 0 :(得分:0)
无法判断您的问题是什么,因为您的描述令人困惑,但如果您的错误是
编译organizationSetup时,android.database.sqlite.SQLiteException接近DATABASE_TABLE_NAME语法错误(代码:1)
尝试在表名后面加一个空格。您的括号触及表格名称。