给我适合的代码行是:
this.databaseHandler = new DatabaseHandler(MainActivity.
我在项目中有这个模块,这行来自我想要合并的另一个项目。我相信我需要这一行,并且无法理解上下文参数,因为它在这里使用。 是的,这条线是不完整的,因为我无法完成它。 我的整个结构或想法可能是错的吗?
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.AsyncTask;
import com.Table.TableMainLayout;
import com.example.tablefreezepane.DatabaseHandler;
public class MainActivity extends Activity {
final String TAG = "MainActivity.java";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Loads next module */
setContentView(new TableMainLayout(this));
}
}
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.
//(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.");
}
}
提前谢谢。
答案 0 :(得分:1)
如果这是异步,则无法按照您的方式从MainActivity访问上下文。为此,请使用上下文参数添加构造函数,然后将MainActivity.this
替换为context