我正在尝试将我的数据库导出到外部存储上的 CSV 文件,但是当我点击“导出数据”时没有任何反应!按钮。
以下是代码:
SpsActivity 的.java
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bStart:
start.setVisibility(View.INVISIBLE);
counter.setVisibility(View.VISIBLE);
new CountDownTimer(9000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
minutes = millisUntilFinished / 60000;
seconds = (millisUntilFinished / 1000) % 60;
counter.setText("Remaining: " + minutes + ":" + seconds);
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
counter.setText("Done!");
export.setVisibility(View.VISIBLE);
}
}.start();
break;
case R.id.bExport:
ExportDatabaseCSVTask task = new ExportDatabaseCSVTask();
task.execute();
break;
}
}
private class ExportDatabaseCSVTask extends
AsyncTask<String, Void, Boolean> {
private final ProgressDialog dialog = new ProgressDialog(
SpsActivity.this);
@Override
protected void onPreExecute() {
this.dialog.setMessage("Exporting database...");
this.dialog.show();
}
protected Boolean doInBackground(final String... args) {
File dbFile = getDatabasePath("brojanje.db");
DatabaseHelper dbhelper = new DatabaseHelper(
getApplicationContext());
Log.v("SpsActivity", "Db path is: " + dbFile); // get the path of db
File exportDir = new File(
Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, "brojanje.csv");
try {
file.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
SQLiteDatabase db = dbhelper.getReadableDatabase(); // Cursor
Cursor curCSV = db.rawQuery("SELECT * FROM izlazniRezultati.db",
null);
csvWrite.writeNext(curCSV.getColumnNames());
while (curCSV.moveToNext()) {
String arrStr[] = { curCSV.getString(0),
curCSV.getString(1), curCSV.getString(2),
curCSV.getString(3) };
csvWrite.writeNext(arrStr);
}
csvWrite.close();
curCSV.close();
return true;
} catch (Exception sqlEx) {
Log.e("SpsActivity", sqlEx.getMessage(), sqlEx);
return false;
}
}
@Override
protected void onPostExecute(final Boolean success) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
if (success) {
Toast.makeText(SpsActivity.this, "Export successful!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SpsActivity.this, "Export failed!",
Toast.LENGTH_SHORT).show();
}
}
}
以下是我的DATABASE的代码: 的数据库处理器的.java
public class DatabaseHelper extends SQLiteOpenHelper{
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
public static final String KEY_ID = "_id";
public static final String KEY_SMEROVI = "Smerovi";
public static final String KEY_KATEGORIZACIJA = "Kategorizacija";
public static final String KEY_VREDNOSTI = "Vrednosti";
private static final String DATABASE_NAME = "brojanje.db";
private static final String DATABASE_TABLE = "izlazniRezultati";
private static final int DATABASE_VERSION = 1;
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_SMEROVI
+ " TEXT NOT NULL, " + KEY_KATEGORIZACIJA
+ " TEXT NOT NULL, " + KEY_VREDNOSTI + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
public void createEntry(String smer, String kat, String vrednost){
SQLiteDatabase db = this.getWritableDatabase();
//this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(KEY_SMEROVI, smer);
values.put(KEY_KATEGORIZACIJA, kat);
values.put(KEY_VREDNOSTI, vrednost);
//ask the database object to insert the new data
try {
db.insert(DATABASE_TABLE, null, values);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
}
因此,我导出了已签名的应用程序,并在我的手机上进行了测试。手机中没有导出的文件。我想这是一个明显的错误,但我在这里发疯了,所以,如果你能帮助我,请欣赏!
提前致谢。
答案 0 :(得分:0)
这里有一个额外的“.db”
Cursor curCSV = db.rawQuery("SELECT * FROM izlazniRezultati.db",
它应该是:
Cursor curCSV = db.rawQuery("SELECT * FROM izlazniRezultati",
<强> [编辑] 强>
由于这不是唯一的问题,我会告诉你我是如何做同样的事情,或多或少:
这是在我的主要活动(在你的情况下,SpsActivity?) - 在AsyncTask中调用
private final void dataExport()
{
/* Save a headerless csv file from the db, separated by ";" an terminated by "\n"). */
final Context ctx = getApplicationContext();
final String appPath = String.format
(
"%s/YourAppName", Environment.getExternalStorageDirectory()
);
final File path = new File(appPath);
if(!path.exists())
{
path.mkdir();
}
final String fileBAK = String.format("%s/backup.csv", appPath);
final String str = CLS_DB.dataExport(ctx);
final int len = str.length();
//final int rows = str.split("\n").length;
if (len > 0)
{
// Write the backup file
try
{
final FileOutputStream fos = new FileOutputStream(new File(fileBAK));
try
{
fos.write(str.getBytes());
fos.flush();
fos.close();
}
catch (final IOException e)
{
/*
System.out.println
(
"Error: " + e.getMessage() + "\n" + e.getLocalizedMessage() + "\n" + e.getCause()
);
*/
// Do something to inform the users that you couldn't export the data
}
}
catch(final FileNotFoundException e)
{
// Do something to inform the users that you couldn't export the data
}
}
}
这是在我的DBHelper类(CLS_DB)中 - 是的,方法名称是相同的,由于幻想短缺......;)
// Export all data
protected final static String dataExport(final Context ctx)
{
SQLiteDatabase db = null;
final StringBuilder sb = new StringBuilder();
try
{
db = ctx.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
final Cursor cur = db.rawQuery
(
"SELECT * FROM izlazniRezultati ORDER BY Smerovi ASC", null
);
if (cur != null)
{
if (cur.moveToFirst())
{
do
{
sb.append
(
String.format
(
"%s;%s\n",
cur.getString(cur.getColumnIndex("Smerovi")),
cur.getString(cur.getColumnIndex("Kategorizacija")),
cur.getString(cur.getColumnIndex("Vrednosti")),
);
}
while (cur.moveToNext());
}
}
cur.close();
}
catch (final SQLiteException se)
{
System.out.println
(
ctx.getClass().getSimpleName() + "\n" + "Could not read from the database"
);
se.printStackTrace();
}
finally
{
db.close();
}
return sb.toString();
}
它的作用,简而言之:
它有点长,但很容易理解。
请注意,我没有在导出中包含_id字段(我没有它,我认为你在Excel中也不需要它),但你可以轻松地将它添加到StringBuilder中,同时准备导出字符串。
我忘了提及......你问题的直接答案是:
使用此代码,csv文件将导出/mnt/sdcard/YourAppName/backup.csv
(或任何已安装/链接的预定义存储空间)
[编辑2]
我理所当然地认为您在Manifest文件中设置了此权限:
<!-- To write and read to and from SDCard -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果没有,请设置它 - 需要读/写你的SD卡。
答案 1 :(得分:0)
以下是 MainActivity.java :
package com.mmilosanovic.trafficcounting;
import java.io.File;
import java.io.FileWriter;
import android.app.Activity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import au.com.bytecode.opencsv.CSVWriter;
public class MainActivity extends Activity implements OnCheckedChangeListener,
OnClickListener {
private GestureDetector gestureDetector;
CheckBox TV, BUS, AV, MOT;
Button start, export;
TextView counter;
float startX = 0;
float startY = 0;
float finishX = 0;
float finishY = 0;
int pomocZaSve = 0;
int pomocZaCekiranje = 0;
long seconds, minutes;
ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.bStart);
export = (Button) findViewById(R.id.bExport);
export.setVisibility(View.INVISIBLE);
counter = (TextView) findViewById(R.id.tvCounter);
counter.setVisibility(View.INVISIBLE);
start.setOnClickListener(this);
TV = (CheckBox) findViewById(R.id.cbTV);
BUS = (CheckBox) findViewById(R.id.cbBUS);
AV = (CheckBox) findViewById(R.id.cbAV);
MOT = (CheckBox) findViewById(R.id.cbMOT);
TV.setOnCheckedChangeListener(this);
BUS.setOnCheckedChangeListener(this);
AV.setOnCheckedChangeListener(this);
MOT.setOnCheckedChangeListener(this);
gestureDetector = new GestureDetector(this, new SwipeGestureDetector());
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bStart:
start.setVisibility(View.INVISIBLE);
counter.setVisibility(View.VISIBLE);
new CountDownTimer(9000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
minutes = millisUntilFinished / 60000;
seconds = (millisUntilFinished / 1000) % 60;
counter.setText("Remaining: " + minutes + ":" + seconds);
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
counter.setText("Done!");
export.setVisibility(View.VISIBLE);
}
}.start();
break;
case R.id.bExport:
ExportDatabaseCSVTask task = new ExportDatabaseCSVTask();
task.execute();
break;
}
}
private class ExportDatabaseCSVTask extends
AsyncTask<String, Void, Boolean> {
private final ProgressDialog dialog = new ProgressDialog(
MainActivity.this);
@Override
protected void onPreExecute() {
this.dialog.setMessage("Exporting database...");
this.dialog.show();
}
protected Boolean doInBackground(final String... args) {
File dbFile = getDatabasePath("brojanje");
DatabaseHelper dbhelper = new DatabaseHelper(
getApplicationContext());
Log.v("SpsActivity", "Db path is: " + dbFile); // get the path of db
File exportDir = new File(
Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, "brojanje.csv");
try {
file.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
SQLiteDatabase db = dbhelper.getReadableDatabase(); // Cursor
Cursor curCSV = db.rawQuery("SELECT * FROM izlazniRezultati",
null);
csvWrite.writeNext(curCSV.getColumnNames());
while (curCSV.moveToNext()) {
String arrStr[] = { curCSV.getString(0),
curCSV.getString(1), curCSV.getString(2),
curCSV.getString(3) };
csvWrite.writeNext(arrStr);
}
csvWrite.close();
curCSV.close();
return true;
} catch (Exception sqlEx) {
Log.e("SpsActivity", sqlEx.getMessage(), sqlEx);
return false;
}
}
@Override
protected void onPostExecute(final Boolean success) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
if (success) {
Toast.makeText(MainActivity.this, "Export successful!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Export failed!",
Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
float height = metrics.heightPixels;
float width = metrics.widthPixels;
height = metrics.heightPixels;
width = metrics.widthPixels;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getRawX();
startY = event.getRawY();
break;
case MotionEvent.ACTION_UP:
finishX = event.getRawX();
finishY = event.getRawY();
float X1zbirX2 = startX + finishX;
float Y1zbirY2 = startY + finishY;
float X1aritmetickaX2 = X1zbirX2 / 2;
float Y1aritmetickaY2 = Y1zbirY2 / 2;
// drugi kvartal (koordinate sredine povucene linije/dijagonale)
if (X1aritmetickaX2 > (width / 2) && Y1aritmetickaY2 < (height / 2)) {
pomocZaSve = 1;
// cetvrti kvartal
} else if (X1aritmetickaX2 < (width / 2)
&& Y1aritmetickaY2 > (height / 2)) {
pomocZaSve = 2;
// treci kvartal
} else if (X1aritmetickaX2 > (width / 2)
&& Y1aritmetickaY2 > (height / 2)) {
pomocZaSve = 3;
// prvi kvartal
} else if (X1aritmetickaX2 < (width / 2)
&& Y1aritmetickaY2 < (height / 2)) {
pomocZaSve = 4;
}
break;
}
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return super.onTouchEvent(event);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
switch (buttonView.getId()) {
case R.id.cbTV:
if (buttonView.isChecked()) {
pomocZaCekiranje = 1;
} else {
pomocZaCekiranje = 5;
}
break;
case R.id.cbBUS:
if (buttonView.isChecked()) {
pomocZaCekiranje = 2;
} else {
pomocZaCekiranje = 6;
}
break;
case R.id.cbAV:
if (buttonView.isChecked()) {
pomocZaCekiranje = 3;
} else {
pomocZaCekiranje = 7;
}
break;
case R.id.cbMOT:
if (buttonView.isChecked()) {
pomocZaCekiranje = 4;
} else {
pomocZaCekiranje = 8;
}
break;
}
}
private void onSwipeLeft() {
progressDialog = ProgressDialog.show(this, "", "DESNO LEVO");
new Thread() {
public void run() {
if (pomocZaCekiranje == 1) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-4", "TV", 1);
entry.close();
TV.toggle();
} else if (pomocZaCekiranje == 2) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-4", "BUS", 1);
entry.close();
BUS.toggle();
} else if (pomocZaCekiranje == 3) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-4", "AV", 1);
entry.close();
AV.toggle();
} else if (pomocZaCekiranje == 4) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-4", "MOT", 1);
entry.close();
MOT.toggle();
} else {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-4", "PA", 1);
entry.close();
}
}
}.start();
}
private void onSwipeRight() {
progressDialog = ProgressDialog.show(this, "", "LEVO DESNO");
new Thread() {
public void run() {
if (pomocZaCekiranje == 1) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-2", "TV", 1);
entry.close();
TV.toggle();
} else if (pomocZaCekiranje == 2) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-2", "BUS", 1);
entry.close();
BUS.toggle();
} else if (pomocZaCekiranje == 3) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-2", "AV", 1);
entry.close();
AV.toggle();
} else if (pomocZaCekiranje == 4) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-2", "MOT", 1);
entry.close();
MOT.toggle();
} else {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-2", "PA", 1);
entry.close();
}
}
}.start();
}
private void onSwipeTop() {
progressDialog = ProgressDialog.show(this, "", "DOLE GORE");
new Thread() {
public void run() {
try {
if (pomocZaCekiranje == 1) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-1", "TV", 1);
entry.close();
TV.toggle();
} else if (pomocZaCekiranje == 2) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-1", "BUS", 1);
entry.close();
BUS.toggle();
} else if (pomocZaCekiranje == 3) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-1", "AV", 1);
entry.close();
AV.toggle();
} else if (pomocZaCekiranje == 4) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-1", "MOT", 1);
entry.close();
MOT.toggle();
} else {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-1", "PA", 1);
entry.close();
}
} catch (Exception e) {
Log.e("MainActivity", "Error");
}
progressDialog.dismiss();
}
}.start();
}
private void onSwipeBottom() {
progressDialog = ProgressDialog.show(this, "", "GORE DOLE");
new Thread() {
public void run() {
if (pomocZaCekiranje == 1) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-3", "TV", 1);
entry.close();
TV.toggle();
} else if (pomocZaCekiranje == 2) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-3", "BUS", 1);
entry.close();
BUS.toggle();
} else if (pomocZaCekiranje == 3) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-3", "AV", 1);
entry.close();
AV.toggle();
} else if (pomocZaCekiranje == 4) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-3", "MOT", 1);
entry.close();
MOT.toggle();
} else {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-3", "PA", 1);
entry.close();
}
}
}.start();
}
private void onSwipeFromBottomToLeft() {
progressDialog = ProgressDialog.show(this, "", "DOLE LEVO");
new Thread() {
public void run() {
if (pomocZaCekiranje == 1) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-4", "TV", 1);
entry.close();
TV.toggle();
} else if (pomocZaCekiranje == 2) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-4", "BUS", 1);
entry.close();
BUS.toggle();
} else if (pomocZaCekiranje == 3) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-4", "AV", 1);
entry.close();
AV.toggle();
} else if (pomocZaCekiranje == 4) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-4", "MOT", 1);
entry.close();
MOT.toggle();
} else {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-4", "PA", 1);
entry.close();
}
}
}.start();
}
private void onSwipeFromTopToRight() {
progressDialog = ProgressDialog.show(this, "", "GORE DESNO");
new Thread() {
public void run() {
if (pomocZaCekiranje == 1) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-2", "TV", 1);
entry.close();
TV.toggle();
} else if (pomocZaCekiranje == 2) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-2", "BUS", 1);
entry.close();
BUS.toggle();
} else if (pomocZaCekiranje == 3) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-2", "AV", 1);
entry.close();
AV.toggle();
} else if (pomocZaCekiranje == 4) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-2", "MOT", 1);
entry.close();
MOT.toggle();
} else {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-2", "PA", 1);
entry.close();
}
}
}.start();
}
private void onSwipeFromTopToLeft() {
progressDialog = ProgressDialog.show(this, "", "GORE LEVO");
new Thread() {
public void run() {
if (pomocZaCekiranje == 1) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-4", "TV", 1);
entry.close();
TV.toggle();
} else if (pomocZaCekiranje == 2) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-4", "BUS", 1);
entry.close();
BUS.toggle();
} else if (pomocZaCekiranje == 3) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-4", "AV", 1);
entry.close();
AV.toggle();
} else if (pomocZaCekiranje == 4) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-4", "MOT", 1);
entry.close();
MOT.toggle();
} else {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 1-4", "PA", 1);
entry.close();
}
}
}.start();
}
private void onSwipeFromBottomToRight() {
progressDialog = ProgressDialog.show(this, "", "DOLE DESNO");
new Thread() {
public void run() {
if (pomocZaCekiranje == 1) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-2", "TV", 1);
entry.close();
TV.toggle();
} else if (pomocZaCekiranje == 2) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-2", "BUS", 1);
entry.close();
BUS.toggle();
} else if (pomocZaCekiranje == 3) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-2", "AV", 1);
entry.close();
AV.toggle();
} else if (pomocZaCekiranje == 4) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-2", "MOT", 1);
entry.close();
MOT.toggle();
} else {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 3-2", "PA", 1);
entry.close();
}
}
}.start();
}
private void onSwipeFromRightToTop() {
progressDialog = ProgressDialog.show(this, "", "DESNO GORE");
new Thread() {
public void run() {
if (pomocZaCekiranje == 1) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-1", "TV", 1);
entry.close();
TV.toggle();
} else if (pomocZaCekiranje == 2) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-1", "BUS", 1);
entry.close();
BUS.toggle();
} else if (pomocZaCekiranje == 3) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-1", "AV", 1);
entry.close();
AV.toggle();
} else if (pomocZaCekiranje == 4) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-1", "MOT", 1);
entry.close();
MOT.toggle();
} else {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-1", "PA", 1);
entry.close();
}
}
}.start();
}
private void onSwipeFromRightToBottom() {
progressDialog = ProgressDialog.show(this, "", "DESNO DOLE");
new Thread() {
public void run() {
if (pomocZaCekiranje == 1) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-3", "TV", 1);
entry.close();
TV.toggle();
} else if (pomocZaCekiranje == 2) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-3", "BUS", 1);
entry.close();
BUS.toggle();
} else if (pomocZaCekiranje == 3) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-3", "AV", 1);
entry.close();
AV.toggle();
} else if (pomocZaCekiranje == 4) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-3", "MOT", 1);
entry.close();
MOT.toggle();
} else {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 2-3", "PA", 1);
entry.close();
}
}
}.start();
}
private void onSwipeFromLeftToTop() {
progressDialog = ProgressDialog.show(this, "", "LEVO GORE");
new Thread() {
public void run() {
if (pomocZaCekiranje == 1) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-1", "TV", 1);
entry.close();
TV.toggle();
} else if (pomocZaCekiranje == 2) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-1", "BUS", 1);
entry.close();
BUS.toggle();
} else if (pomocZaCekiranje == 3) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-1", "AV", 1);
entry.close();
AV.toggle();
} else if (pomocZaCekiranje == 4) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-1", "MOT", 1);
entry.close();
MOT.toggle();
} else {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-1", "PA", 1);
entry.close();
}
}
}.start();
}
private void onSwipeFromLeftToBottom() {
progressDialog = ProgressDialog.show(this, "", "LEVO DOLE");
new Thread() {
public void run() {
if (pomocZaCekiranje == 1) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-3", "TV", 1);
entry.close();
TV.toggle();
} else if (pomocZaCekiranje == 2) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-3", "BUS", 1);
entry.close();
BUS.toggle();
} else if (pomocZaCekiranje == 3) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-3", "AV", 1);
entry.close();
AV.toggle();
} else if (pomocZaCekiranje == 4) {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-3", "MOT", 1);
entry.close();
MOT.toggle();
} else {
DatabaseHelper entry = new DatabaseHelper(MainActivity.this);
entry.createEntry("Smer 4-3", "PA", 1);
entry.close();
}
}
}.start();
}
// Private class for gestures
private class SwipeGestureDetector extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 50;
private static final int SWIPE_X = 30;
private static final int SWIPE_Y = 50;
private static final int SWIPE_DIJAGONALA_X = 80;
private static final int SWIPE_DIJAGONALA_Y = 80;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)
&& Math.abs(diffY) < SWIPE_X) {
if (Math.abs(diffX) > SWIPE_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else if (Math.abs(diffY) > Math.abs(diffX)
&& Math.abs(diffX) < SWIPE_Y) {
if (Math.abs(diffY) > SWIPE_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
// iz donjeg kraka u levi krak
} else if (pomocZaSve == 2
&& Math.abs(diffX) > SWIPE_DIJAGONALA_X
&& Math.abs(diffY) > SWIPE_DIJAGONALA_Y
&& startX > finishX && startY > finishY) {
onSwipeFromBottomToLeft();
// iz gornjeg kraka u desni krak
} else if (pomocZaSve == 1
&& Math.abs(diffX) > SWIPE_DIJAGONALA_X
&& Math.abs(diffY) > SWIPE_DIJAGONALA_Y
&& startX < finishX && startY < finishY) {
onSwipeFromTopToRight();
// iz gornjeg kraka u levi krak
} else if (pomocZaSve == 4
&& Math.abs(diffX) > SWIPE_DIJAGONALA_X
&& Math.abs(diffY) > SWIPE_DIJAGONALA_Y
&& startX > finishX && startY < finishY) {
onSwipeFromTopToLeft();
// iz donjeg kraka u desni krak
} else if (pomocZaSve == 3
&& Math.abs(diffX) > SWIPE_DIJAGONALA_X
&& Math.abs(diffY) > SWIPE_DIJAGONALA_Y
&& startX < finishX && startY > finishY) {
onSwipeFromBottomToRight();
// iz desnog kraka u gornji
} else if (pomocZaSve == 1
&& Math.abs(diffX) > SWIPE_DIJAGONALA_X
&& Math.abs(diffY) > SWIPE_DIJAGONALA_Y
&& startX > finishX && startY > finishY) {
onSwipeFromRightToTop();
// iz desnog kraka u donji krak
} else if (pomocZaSve == 3
&& Math.abs(diffX) > SWIPE_DIJAGONALA_X
&& Math.abs(diffY) > SWIPE_DIJAGONALA_Y
&& startX > finishX && startY < finishY) {
onSwipeFromRightToBottom();
// iz levog kraka u gornji
} else if (pomocZaSve == 4
&& Math.abs(diffX) > SWIPE_DIJAGONALA_X
&& Math.abs(diffY) > SWIPE_DIJAGONALA_Y
&& startX < finishX && startY > finishY) {
onSwipeFromLeftToTop();
// iz levog kraka u donji
} else if (pomocZaSve == 2
&& Math.abs(diffX) > SWIPE_DIJAGONALA_X
&& Math.abs(diffY) > SWIPE_DIJAGONALA_Y
&& startX < finishX && startY < finishY) {
onSwipeFromLeftToBottom();
}
} catch (Exception e) {
Log.e("MainActivity", "Error on gestures");
}
return false;
}
}
}
这是 DatabaseHelper.java :
package com.mmilosanovic.trafficcounting;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper{
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
public static final String KEY_ID = "_id";
public static final String KEY_SMEROVI = "Smerovi";
public static final String KEY_KATEGORIZACIJA = "Kategorizacija";
public static final String KEY_VREDNOSTI = "Vrednosti";
private static final String DATABASE_NAME = "brojanje";
private static final String DATABASE_TABLE = "izlazniRezultati";
private static final int DATABASE_VERSION = 1;
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_SMEROVI
+ " TEXT NOT NULL, " + KEY_KATEGORIZACIJA
+ " TEXT NOT NULL, " + KEY_VREDNOSTI + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
public void createEntry(String smer, String kat, int vrednost){
SQLiteDatabase db = this.getWritableDatabase();
//this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
values.put(KEY_SMEROVI, smer);
values.put(KEY_KATEGORIZACIJA, kat);
values.put(KEY_VREDNOSTI, vrednost);
//ask the database object to insert the new data
try {
db.insert(DATABASE_TABLE, null, values);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
}