我有一些代码,它与db一起工作。我尝试在asynctask中使用它,但是然后SetListadapter不起作用,并且无法显示我的list.How我能在asynctask中正确地执行它吗?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new LoadViewTask().execute();
final String[] from = {COLUMN_NAME, COLUMN_PHOTO};
final int[] to = {R.id.name, R.id.photo};
ScriptDBAdapter adapter = new ScriptDBAdapter(this, R.layout.list_item, mCursor, from, to);
LayoutAnimationController controller = AnimationUtils
.loadLayoutAnimation(this, R.anim.list_layout_controller);
getListView().setLayoutAnimation(controller);
OnItemClickListener itemListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub
String s =(String) ((TextView) v.findViewById(R.id.name)).getText();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("name_of_brend", s);
startActivity(intent);
}
};
getListView().setOnItemClickListener(itemListener);
} ///end OnOpen
private void tryReleaseCursor() {
if (mCursor != null) {
stopManagingCursor(mCursor);
// mCursor.close();
//mCursor = null;
}
}
private class LoadViewTask extends AsyncTask<Void, Integer, Cursor>
{
//Before running code in separate thread
@Override
protected void onPreExecute()
{
//Create a new progress dialog
progressDialog = new ProgressDialog(MainActivity.this);
//Set the progress dialog to display a horizontal progress bar
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//Set the dialog title to 'Loading...'
progressDialog.setTitle("Loading...");
//Set the dialog message to 'Loading application View, please wait...'
progressDialog.setMessage("Loading application View, please wait...");
//This dialog can't be canceled by pressing the back key
progressDialog.setCancelable(false);
//This dialog isn't indeterminate
progressDialog.setIndeterminate(false);
//The maximum number of items is 100
progressDialog.setMax(100);
//Set the current progress to zero
progressDialog.setProgress(0);
//Display the progress dialog
progressDialog.show();
}
//The code to be executed in a background thread.
@Override
protected Cursor doInBackground(Void... params)
{
tryReleaseCursor();
try {
SqliteScript script = new SqliteScript(getAssets().open("init2.sql"));
SqliteOpenHelper dbHelper = new SqliteOpenHelper(getApplicationContext(), script);
SQLiteDatabase database = dbHelper.getWritableDatabase();
mCursor = database.query(TABLE_BRANDS, null, null, null, "name", null, null);
startManagingCursor(mCursor);
} catch (IOException e) {
e.printStackTrace();
}
return mCursor;
}
//Update the progress
@Override
protected void onProgressUpdate(Integer... values)
{
progressDialog.setProgress(values[0]);
}
//after executing the code in the thread
protected void onPostExecute(Cursor mcCursor)
{
//close the progress dialog
progressDialog.dismiss();
//initialize the View
setListAdapter(adapter);
adapter.notifyDataSetChanged();
setContentView(R.layout.activity_main);
}
}
我试图这样做,但没有任何作用。我无法理解如何填充适配器。请帮助我!