Android:无法使用SQL Server数据填充ListView

时间:2019-10-02 19:47:50

标签: java android gradle

我正在尝试使用查询中的SQL Server数据填充Listview,但是我无法使用异步任务来完成此操作,但是在调试时,我可以看到查询工作正常。 这将是我的第一个Android APK。

我正在使用com.android.tools.build:gradle:3.5.0和android 10

代码:

package com.example.deltafx;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.List;
import java.util.ArrayList;
import static java.lang.Class.forName;
import android.widget.ArrayAdapter;



public class MainActivity extends Activity {

    private Button verFX;
    private TextView txtMonto;
    private MiTareaAsincronaDialog tarea2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        verFX = (Button)findViewById(R.id.btnpidetc);
        verFX.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                tarea2 = new MiTareaAsincronaDialog();
                tarea2.execute();
            }
        });
    }

    //Tarea Asincronona para sacar los puestos del ranking.
    private class MiTareaAsincronaDialog extends AsyncTask<Void, Void, Integer> {

        @Override
        protected void onPreExecute() {}


        List<String> infolist = new ArrayList<String>();;
        ListView lstOpes = (ListView) findViewById(R.id.lstOpes);

        protected void onPostExecute(boolean result) {
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_list_item_1, infolist);
            lstOpes.setAdapter(adapter);

        }


        @Override
        protected Integer doInBackground(Void... params) {
            try {
                forName("net.sourceforge.jtds.jdbc.Driver");
                Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://xx.xx.xx.xx:1433/xxxxxxxx", "usr_equilibra", "xxxxxxx");
                //Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433;IntegratedSecurity=true;DatabaseName=AnagraInternacional");
                //En el stsql se puede agregar cualquier consulta SQL deseada.
                String stsql = " select top 10 motivo+usuario as campo from tbl_motivo_cambio_fecha";
                // String stsql = "SELECT @@VERSION";
                Statement st = conn.createStatement();
                ResultSet rs = st.executeQuery(stsql);

                while (rs.next()) {
                    String datoCliente = new String();
                    datoCliente= rs.getString("campo");
                    infolist.add(datoCliente);
                }

                conn.close();

                String[] strarray = new String[infolist.size()];
                infolist.toArray(strarray);

                final ArrayList<String> list = new ArrayList<String>();
                for (int i = 0; i < strarray.length; ++i) {
                    list.add(strarray[i]);
                }

               // publishProgress(100);
                return 1;

            } catch (SQLException se) {
                System.out.println("oops! No se puede conectar. Error: " + se.toString());
            } catch (ClassNotFoundException e) {
                System.out.println("oops! No se encuentra la clase. Error: " + e.getMessage());
            }
            return 0;
        }
    }
}

0 个答案:

没有答案