gpt4 book ai didi

java - 在哪里可以更好地全面、深入地理解Context参数的实例化?

转载 作者:行者123 更新时间:2023-12-02 04:55:49 25 4
gpt4 key购买 nike

我试图解决这段代码中Context和Classes之间的联系以及我对这个概念的理解。 MainActivity.this在这里不起作用。它带有周围的代码。

package com.Table;

import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tablefreezepane.DatabaseHandler;
import com.example.tablefreezepane.MainActivity;

import java.util.ArrayList;
import java.util.List;

public class AsyncInsertData extends AsyncTask<String, String, String> {

DatabaseHandler databaseHandler;
String type;
long timeElapsed;

protected AsyncInsertData(String type){
this.type = type;
this.databaseHandler = new DatabaseHandler(MainActivity.this);
}

// @type - can be 'normal' or 'fast'
//@Override
//protected void onPreExecute() {
// super.onPreExecute();
// tvStatus.setText("Inserting " + editTextRecordNum.getText() + " records...");
//}

@Override
protected String doInBackground(String... aurl) {

try {

// get number of records to be inserted
int insertCount = 20;

// empty the table
databaseHandler.deleteRecords();

// keep track of execution time
long lStartTime = System.nanoTime();

if (type.equals("normal")) {
databaseHandler.insertNormal(insertCount);
} else {
databaseHandler.insertFast(insertCount);
}

// execution finised
long lEndTime = System.nanoTime();

// display execution time
timeElapsed = lEndTime - lStartTime;

} catch (Exception e) {
e.printStackTrace();
}
return null;
}

protected void onPostExecute(String unused) {
Toast.makeText(getApplicationContext(),"This is an Android Toast Message", Toast.LENGTH_LONG).show();
//tvStatus.setText("Done " + choice + " inserting " + databaseHandler.countRecords() + " records into table: [" + this.databaseHandler.tableName + "]. Time elapsed: " + timeElapsed / 1000000 + " ms.");
}

}

阅读或复制代码不是问题。问题存在于this.databaseHandler = new DatabaseHandler(MainActivity.this);中Context参数的整体思维模型。陈述。必须存在该行代码才能调用 databaseHandler 中的方法。类。

package com.example.tablefreezepane;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.widget.TextView;


public class DatabaseHandler extends SQLiteOpenHelper {

// for our logs
public static final String TAG = "DatabaseHandler.java";

public TextView tvstatus;

// database version
private static final int DATABASE_VERSION = 7;

// database name
protected static final String DATABASE_NAME = "NinjaDatabase2";

// table details
public String tableName = "locations";
public String fieldObjectId = "id";
public String fieldObjectName = "name";
public String fieldObjectDescription = "description";

// constructor
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// creating table
@Override
public void onCreate(SQLiteDatabase db) {

String sql = "";
this.tvstatus.setText("Creating table");

sql += "CREATE TABLE " + tableName;
sql += " ( ";
sql += fieldObjectId + " INTEGER PRIMARY KEY AUTOINCREMENT, ";
sql += fieldObjectName + " TEXT, ";
sql += fieldObjectDescription + " TEXT ";
sql += " ) ";

db.execSQL(sql);
this.tvstatus.setText("Table created...");

// create the index for our INSERT OR REPLACE INTO statement.
// this acts as the WHERE name="name input" AND description="description input"
// if that WHERE clause is true, I mean, it finds the same name and description in the database,
// it will be REPLACEd.
// ELSE, what's in the database will remain and the input will be INSERTed (new record)
String INDEX = "CREATE UNIQUE INDEX locations_index ON "
+ tableName + " (name, description)";

db.execSQL(INDEX);
}

/*
* When upgrading the database, it will drop the current table and recreate.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

String sql = "DROP TABLE IF EXISTS " + tableName;
db.execSQL(sql);

onCreate(db);
}

// insert data using transaction and prepared statement
public void insertFast(int insertCount) {

// you can use INSERT only
String sql = "INSERT OR REPLACE INTO " + tableName + " ( name, description ) VALUES ( ?, ? )";

SQLiteDatabase db = this.getWritableDatabase();

/*
* According to the docs http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
* Writers should use beginTransactionNonExclusive() or beginTransactionWithListenerNonExclusive(SQLiteTransactionListener)
* to start a transaction. Non-exclusive mode allows database file to be in readable by other threads executing queries.
*/
//db.beginTransactionNonExclusive();
db.beginTransaction();

SQLiteStatement stmt = db.compileStatement(sql);

for(int x=1; x<=insertCount; x++){

stmt.bindString(1, "Name # " + x);
stmt.bindString(2, "Description # " + x);

stmt.execute();
stmt.clearBindings();

}

db.setTransactionSuccessful();
db.endTransaction();

}

// inserts the record without using transaction and prepare statement
public void insertNormal(int insertCount){
try{

SQLiteDatabase db = this.getWritableDatabase();

for(int x=1; x<=insertCount; x++){

ContentValues values = new ContentValues();
values.put(fieldObjectName, "Name # " + x);
values.put(fieldObjectDescription, "Description # " + x);

db.insert(tableName, null, values);

}

db.close();

}catch(Exception e){
e.printStackTrace();
}
}

// deletes all records
public void deleteRecords(){

SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("delete from "+ tableName);
db.close();
}

// count records
public int countRecords(){

SQLiteDatabase db = this.getWritableDatabase();

Cursor cursor = db.rawQuery("SELECT count(*) from " + tableName, null);
cursor.moveToFirst();

int recCount = cursor.getInt(0);

cursor.close();
db.close();

return recCount;
}

}

任何指导、图形模型链接、如何 this.databaseHandler = new DatabaseHandler(MainActivity.this);声明被构建将不胜感激。

最佳答案

行:

this.databaseHandler = new DatabaseHandler(MainActivity.this);

期望构造函数中有一个有效的ContextActivity 扩展了 ContextThemeWrapper,因此是要传递的有效对象,但该对象内的实际上下文必须有效。

看起来您已经从 Activity 复制了代码,该代码的所有代码都包含在 MainActivity 对象中,并且在创建 MainActivity 时从框架来看,上下文是有效的。

如果您将 Activity 实例化为常规类,则它将被创建,但 Context 将无效。 Android 框架必须实例化它,并在类的构造过程中为其提供有效的Context。您无法创建自己的有效Context,因为 Android 需要定义它。

这是一篇关于什么是 Context 的好文章:

What is 'Context' on Android?

我不知道有任何图形可以描述或解释它。它有点抽象,因为它涉及有关 Android 环境的许多信息,包括一些视觉方面(如屏幕信息)和非视觉方面(文件位置、安全性、权限等)。

关于java - 在哪里可以更好地全面、深入地理解Context参数的实例化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28785386/

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