0

I'm developing an android application, when I select a fragment, I put those code in "OnActivityCreated" such as:

 @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    btnAdd = (ImageButton) getActivity().findViewById(R.id.imageButtonTime);
    txtTime = (TextView) getActivity().findViewById(R.id.textViewTime);
    txtTime.setText("");
    listView = (ListView) getActivity().findViewById(R.id.listView);
    time_control = new Time_Control(LoginActivity.db.getConnection());
    accountID = LoginActivity.Account_Id;
    calendar = Calendar.getInstance();
    mYear = calendar.get(Calendar.YEAR); // current year
    mMonth = calendar.get(Calendar.MONTH); // current month
    mDay = calendar.get(Calendar.DAY_OF_MONTH); // current day
    txtTime.setText(mDay + "/" + (mMonth + 1) + "/" + mYear);
    sDate = mYear + "-" + (mMonth + 1) + "-" + mDay;
    LoadList();

LoadList() is used to get data from database and put it on listview. My application load very slow when I select fragment. Is there anyway i could do to increase the performance, like putting above code to other location.

Appreciate your help!

Edit: LoadList() code:

 public ArrayList<Time> LoadTime(int accId, String date) {
    ArrayList<Time> arrayList = new ArrayList<Time>();
    try {
        PreparedStatement query = connection.prepareStatement("exec sp_excutedtask_load ?,?");
        query.setInt(1, accId);
        query.setString(2, date);
        ResultSet resultSet = query.executeQuery();
        while (resultSet.next()) {
            int id = resultSet.getInt("ID");
            String tagName = resultSet.getString("TagName");
            String taskName = resultSet.getString("TaskName");
            int esHrs = resultSet.getInt("EsHour");
            int esMin = resultSet.getInt("EsMinute");
            int acHrs = resultSet.getInt("AcHour");
            int acMin = resultSet.getInt("AcMinute");
            String start, end;
            if (resultSet.getTime("Start") == null) {
                start = "";
            } else {
                start = resultSet.getTime("Start").toString();
            }
            if (resultSet.getTime("End") == null) {
                end = "";
            } else {
                end = resultSet.getTime("End").toString();
            }
            int run = resultSet.getInt("Running");
            int done = resultSet.getInt("Done");
            Time time = new Time(id, tagName, taskName, esHrs, esMin, acHrs, acMin, start, end, run, done);
            arrayList.add(time);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return arrayList;
}
Huy
  • 179
  • 1
  • 1
  • 9
  • ...we'd need to know the code for LoadList() to answer this question – EpicPandaForce Jul 27 '16 at 08:58
  • the loading time depends on the logic you used in the LoadList(). Since you are getting it from db, optimize the query you used to get data and keep your SQLiteOpenHelper class as singleton – LvN Jul 27 '16 at 09:03

1 Answers1

-2

You can use the AsyncTask to load data. https://developer.android.com/reference/android/os/AsyncTask.html

Hadi
  • 159
  • 1
  • 9