کلاس ایجاد پایگاه داده و جدول آن به شرح ذیل می باشد:
کد:
package com.example.al.myapplicationop;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.Calendar;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDB.db";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE USERS_TABLE(\n" +
" PUserID INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
" PUserNAME TEXT NOT NULL,\n" +
" PUserPass TEXT NOT NULL,\n" +
" InsDateTime DATETIME NOT NULL,\n" +
" UserRanking DECIMAL NOT NULL,\n" +
" UserAverage FLOAT NOT NULL,\n" +
" UserHashCode NVARCHAR(200) NOT NULL,\n" +
" UserGuID UNIQUEIDENTIFIER NOT NULL UNIQUE\n" +
");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void UDF_InsertDataToDB(double v , float v1 , String Str_UserHashCode , String Str_UniqIden)
{
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
sqLiteDatabase.execSQL("INSERT INTO USERS_TABLE (PUserNAME , PUserPass , InsDateTime , " +
"UserRanking , UserAverage , UserHashCode , UserGuID)" +
" VALUES ('ALI' , 'ALI3' , '" + mydate + "' , " + v +" , " + v1 +" , " +
"'" + Str_UserHashCode + "' , '" + Str_UniqIden + "');");
}
}
کدهای مربوط به بخش Main_Activity برای اجرای کلاس ایجاد پایگاه داده و Progress Bar در محیط برنامه نویسی اندروید:
کد:
package com.example.al.myapplicationop;
import android.os.Build;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.UUID;
public class Main2Activity extends AppCompatActivity {
DatabaseHelper myDB;
private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
myDB = new DatabaseHelper(this);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(3000);
final ProgressBar CircProgressBar = (ProgressBar) findViewById(R.id.progressBar_cyclic);
textView = (TextView) findViewById(R.id.textView);
new Thread(new Runnable() {
public void run() {
while (progressStatus < 3000) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
String uniqueID = UUID.randomUUID().toString();
myDB.UDF_InsertDataToDB(progressStatus/7 ,(float)java.lang.Math.cos(progressStatus) ,
String.valueOf(String.valueOf(progressStatus).hashCode()),
uniqueID);
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds.
Thread.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressBar.setVisibility(View.INVISIBLE);
CircProgressBar.setVisibility(View.INVISIBLE);
}
}).start();
}
}
ساختار جدول یا table ایجاد شده در SQLite به شرح ذیل است:
کد:
CREATE TABLE USERS_TABLE(
PUserID INTEGER PRIMARY KEY AUTOINCREMENT,
PUserNAME TEXT NOT NULL,
PUserPass TEXT NOT NULL,
InsDateTime DATETIME NOT NULL,
UserRanking DECIMAL NOT NULL,
UserAverage FLOAT NOT NULL,
UserHashCode NVARCHAR(200) NOT NULL,
UserGuID UNIQUEIDENTIFIER NOT NULL UNIQUE
);
کد XML مربوط به ساختار Progressbar و فیلد TextView به شرح ذیل میباشد:
کد:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="50dp"
app:layout_constraintBottom_toBottomOf="@+id/imageView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/imageView3"
app:layout_constraintTop_toTopOf="@+id/imageView3" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="327dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:indeterminate="false"
android:max="2000"
android:minHeight="50dp"
android:minWidth="200dp"
android:progress="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="0dp" />
<ProgressBar
android:id="@+id/progressBar_cyclic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="116dp"
android:minHeight="50dp"
android:minWidth="50dp"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />