package com.zhenxin.taskmanage;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import java.util.ArrayList;
public class cx extends AppCompatActivity {
private AutoCompleteTextView autot=null;
public ArrayList<String> mcj=null;
private MySQLiteHelper myhelper;
final String LOG_TAG = “MY_LOG_TAG”;
//String[] mcj=new String[]{“kuaijiekagu”,”pvcsantong”,”shengliaodai”,”dianqijiaodai”,”pvcfalan”,”jiluben”};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cx);
/*mcj=new ArrayList<>();
mcj.add(“kuaijiekagu”);
mcj.add(“pvcfalan”);
mcj.add(“pvcsantong”);
mcj.add(“shengliaodai”);*/
Log.e(LOG_TAG, “–初始化的时候–“);
autot=(AutoCompleteTextView)this.findViewById(R.id.autot);
myhelper = new MySQLiteHelper(this, “my.db”, null, 1);
new cxjgtask().execute(myhelper);
}
public class cxjgtask extends AsyncTask<MySQLiteHelper,Void,ArrayList<String>>{
protected ArrayList<String> doInBackground(MySQLiteHelper… myhelper){
Log.i(LOG_TAG, “–doinbackground–“);
ArrayList<sccs> sccss=new ArrayList<>();
SQLiteDatabase db = myhelper[0].getReadableDatabase();
Cursor cursor = db.query(“rkjl”, null, null, null, null, null, null);
ArrayList<String> mcj1 = new ArrayList<>();
Log.i(LOG_TAG, “–“);
for (cursor.moveToFirst();!(cursor.isAfterLast());cursor.moveToNext()) {
sccs sccs= new sccs();
sccs.setname(cursor.getString(1));
sccs.setguige(cursor.getString(2));
sccs.setdanwei(cursor.getString(3));
sccs.setshuliang(cursor.getDouble(4));
sccs.setjiage(cursor.getDouble(5));
sccss.add(sccs);
mcj1.add(cursor.getString(1));
Log.i(LOG_TAG, cursor.getString(1) + “–“);
}
return mcj1;
}
protected void onPostExecute(ArrayList<String> result){
//super.onPostExecute(mcj1);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(cx.this,android.R.layout.simple_dropdown_item_1line,result);
autot.setAdapter(adapter);
autot.setThreshold(2);
}
}
}
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
import java.util.ArrayList;
public class cx extends AppCompatActivity {
private AutoCompleteTextView autot=null;
public ArrayList<String> mcj=null;
private MySQLiteHelper myhelper;
final String LOG_TAG = “MY_LOG_TAG”;
//String[] mcj=new String[]{“kuaijiekagu”,”pvcsantong”,”shengliaodai”,”dianqijiaodai”,”pvcfalan”,”jiluben”};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cx);
/*mcj=new ArrayList<>();
mcj.add(“kuaijiekagu”);
mcj.add(“pvcfalan”);
mcj.add(“pvcsantong”);
mcj.add(“shengliaodai”);*/
Log.e(LOG_TAG, “–初始化的时候–“);
autot=(AutoCompleteTextView)this.findViewById(R.id.autot);
myhelper = new MySQLiteHelper(this, “my.db”, null, 1);
new cxjgtask().execute(myhelper);
}
public class cxjgtask extends AsyncTask<MySQLiteHelper,Void,ArrayList<String>>{
protected ArrayList<String> doInBackground(MySQLiteHelper… myhelper){
Log.i(LOG_TAG, “–doinbackground–“);
ArrayList<sccs> sccss=new ArrayList<>();
SQLiteDatabase db = myhelper[0].getReadableDatabase();
Cursor cursor = db.query(“rkjl”, null, null, null, null, null, null);
ArrayList<String> mcj1 = new ArrayList<>();
Log.i(LOG_TAG, “–“);
for (cursor.moveToFirst();!(cursor.isAfterLast());cursor.moveToNext()) {
sccs sccs= new sccs();
sccs.setname(cursor.getString(1));
sccs.setguige(cursor.getString(2));
sccs.setdanwei(cursor.getString(3));
sccs.setshuliang(cursor.getDouble(4));
sccs.setjiage(cursor.getDouble(5));
sccss.add(sccs);
mcj1.add(cursor.getString(1));
Log.i(LOG_TAG, cursor.getString(1) + “–“);
}
return mcj1;
}
protected void onPostExecute(ArrayList<String> result){
//super.onPostExecute(mcj1);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(cx.this,android.R.layout.simple_dropdown_item_1line,result);
autot.setAdapter(adapter);
autot.setThreshold(2);
}
}
}
解决方案
40
只是简单的数据库查询没必要用asynctask,你直接新建子线程,通过handler来更新ui就行。从代码来看,你的asynctask使用好像没有问题