在Android中启动应用程序时初始化SQL

时间:2018-07-20 16:25:30

标签: java android arrays azure azure-sql-database

我有一个名为getdata的Java类,其中包含一些SQL代码,该代码在另一个Java类上显示为listview,我的问题是每次我尝试查看冻结了一点的数据时一会儿,然后显示数据,因为我是从云数据库中获取数据的。无论如何,当我在用户忙于登录的应用程序中启动我的应用程序并将其保持在ram上时,是否有初始化此SQL代码的请求,以便在我尝试查看产品时应用程序不会滞后

这是我的getdata.java

public class GetData {
Connection connect;
String ConnectionResult = "";
Boolean isSuccess = false;




public List<Map<String,String>> doInBackground() {

    List<Map<String, String>> data = null;
    data = new ArrayList<Map<String, String>>();
    try
    {
        ConnectionHelper conStr=new ConnectionHelper();
        connect =conStr.connectionclass();        // Connect to database
        if (connect == null)
        {
            ConnectionResult = "Check Your Internet Access!";
        }
        else
        {
            // Change below query according to your own database.
            String query = "select * from cc_rail";
            Statement stmt = connect.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()){
                Map<String,String> datanum=new HashMap<String,String>();
                datanum.put("NAME",rs.getString("RAIL_NAME"));

                datanum.put("PRICE",rs.getString("RAIL_UNIT_PRICE"));

                datanum.put("RANGE",rs.getString("RAIL_RANGE"));

                datanum.put("SUPPLIER",rs.getString("RAIL_SUPPLIER"));

                datanum.put("SIZE",rs.getString("RAIL_SIZE"));
                data.add(datanum);
            }


            ConnectionResult = " successful";
            isSuccess=true;
            connect.close();
        }
    }
    catch (Exception ex)
    {
        isSuccess = false;
        ConnectionResult = ex.getMessage();
    }

    return data;
}

这是我的.java文件,我在其中查看数据并发生滞后:

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_valance, container, false);

    list = (ListView)rootView.findViewById(R.id.raill);
    add = (Button)rootView.findViewById(R.id.btn_add);
    progressBar = (ProgressBar)rootView.findViewById(R.id.PB_Getting);
    progressBar.setVisibility(View.GONE);

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getActivity(),Add_valance.class);
            startActivity(i);
        }
    });


          List<Map<String,String>> MyData = null;
            GetValence mydata =new GetValence();
            MyData= mydata.doInBackground();
            String[] fromwhere = { "NAME","PRICE","SIZE" ,"RANGE","SUPPLIER"};

            int[] viewswhere = {R.id.Name_txtView , R.id.price_txtView,R.id.size_txtView};

            ADAhere = new SimpleAdapter(getActivity(), MyData,R.layout.list_valence, fromwhere, viewswhere);

            list.setAdapter(ADAhere);

1 个答案:

答案 0 :(得分:1)

这就是我在评论中所说的。

GetData.java

public class GetData extends AsyncTask<String, Integer, Map<String, String>> {
    @Override
    protected Map<String, String> doInBackground(String... params) {
        //do your thing here
        return resultMap;
    }
    @Override
    protected void onPostExecute(Map<String, String> result) {
        StorageClass.setArrayData(result);
    }
}

StorageClass.java

public class StorageClass {
    private static Map<String,String> arrayData = new HashMap<>();

    public static void setArrayData(Map<String,String> results){
        arrayData = results;
    }

    public static Map<String, String> getArrayData(){
        return arrayData;
    }
}