我在android上做了一个代码,我想从stackoverflow人那里得到一些意见。这是关于我使用 AsyncTask 类的代码......但我不知道它是否编码好......
这是代码:
package com.example.basicmaponline;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.mlab.android.basicoverlays.PostgreSQL;
import com.mlab.android.basicoverlays.SQLloja;
public class Intro extends Activity{
//SQLlistLoja listaLoja;
HashMap<String, SQLloja> listaLoja;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.intro);
//Intent openMainActivity = new Intent("com.example.basicmaponline.MAINACTIVITY"); //isto vem do ficheiro AndroidManifest.xml o "com.example....."
//startActivity(openMainActivity);
try {
listaLoja = new loadDatabase().execute().get();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(listaLoja !=null){
for(SQLloja loja : listaLoja.values()){
Log.d("ASYNC25", loja.getNome() );
}
}
else{
Log.d("ASYNC25", "NAO GUARDOU!" );
}
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}finally{
Intent openMenu = new Intent("com.example.basicmaponline.MENU"); //isto vem do ficheiro AndroidManifest.xml o "com.example....."
openMenu.putExtra("listaLoja",listaLoja);
startActivity(openMenu);
}
}
};
timer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//ourSong.release(); //release the music and we are done with you
finish();
}
//@SuppressWarnings("rawtypes")
public class loadDatabase extends AsyncTask<Void, Void, HashMap<String,SQLloja>>{
@Override
protected HashMap<String,SQLloja> doInBackground(Void... params) {
// TODO Auto-generated method stub
HashMap<String,SQLloja>listaLojas = new HashMap<String, SQLloja>();
try{
PostgreSQL pSQL = new PostgreSQL(host,db, username, password);
String sql = pSQL.getLojasCidadao();
Statement st = pSQL.getConnection().createStatement();
ResultSet rs = st.executeQuery(sql);
Log.d("ASYNC2","Entrei na thread ASYNCTASK");
while(rs.next()){
int lcId = Integer.parseInt(rs.getString(1));
String lcNome=rs.getString(2);
String lcCP = rs.getString(3);
int lcDistrito = Integer.parseInt(rs.getString(4));
int lcConselho = Integer.parseInt(rs.getString(5));
double lcAltitude = Double.parseDouble(rs.getString(6));
double lcLongitude = Double.parseDouble(rs.getString(7));
String lcTelefone = rs.getString(8);
boolean lcEstado = Boolean.parseBoolean(rs.getString(9));
String lcRua = rs.getString(10);
SQLloja loja = new SQLloja(lcId,lcNome,lcCP,lcDistrito,lcConselho,lcAltitude,lcLongitude,lcTelefone,lcEstado,lcRua);
listaLojas.put(loja.getNome(),loja.clone());
String informacoesLoja = "Rua : "+lcRua+"\nC.P. : "+lcCP+"\nTel. : "+lcTelefone;
Log.d("ASYNC",lcNome+" Altitude = "+lcAltitude+" Longitude = "+lcLongitude+" Rua = "+lcRua);
}
//listaLoja = new SQLlistLoja(listaLojas);
rs.close();
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if(listaLojas!=null){
Log.d("ASYNC","entrei no if da listaLojas");
for(SQLloja loja : listaLojas.values()){
Log.d("ASYNC", loja.getNome() );
}
}
else Log.d("ASYNC","listaLoja nula ! PQP !");
return listaLojas;
}
@Override
protected void onPostExecute(HashMap<String,SQLloja> listaLojas){
listaLoja = listaLojas;
for(SQLloja loja : listaLoja.values()){
Log.d("ASYNC24", loja.getNome() );
}
}
}
}
我不确定,但我认为 AsyncTask 的返回值不是以正确的方式生成的,尽管它在我的 android 应用程序上工作。一些意见将受到欢迎。谢谢 !:)