Konrad Uciechowski
Konrad Uciechowski

Reputation: 486

Loading listview Activity takes long and show black screen before appear

I created app that takes JSON with AsyncTask from server. When User click a button app starts new Activity and download data from server and show it as a items in ListView. The Problem is when I open new Activity it takes too long to load. When button is pressed app freeze on about one or two seconds and then show black screen for another 2/3 seconds. After that activity is displayed but it is very slow. It freeze every time user is scrolling or pressing button to display more options of custom adapter. Is there any way to make app more smooth? Json data that is downloaded is just simple JSONArray with JSONObjects that has 2 string values and one HTML format. This 3 values is display to user.

Part of Custom Adapter class

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        SuggestionList suggestionList = getItem(position);
        int actualPosition = 0;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.sugestion_list, parent, false);
        }
        final Button suggestionsButton = (Button) convertView.findViewById(R.id.suggestionsMore);
        final TextView suggestionNumber = (TextView) convertView.findViewById(R.id.sugestionNumber);
        final TextView suggestionDescription = (TextView) convertView.findViewById(R.id.suggestionDescription);
        final ImageView bio = convertView.findViewById(R.id.sugestionBio);
        final ImageView block = convertView.findViewById(R.id.sugestionBlock);
        final ImageView call = convertView.findViewById(R.id.sugestionCall);
...
        final Animation slideUp = AnimationUtils.loadAnimation(getContext(), R.anim.slideup);
        final Animation slideDown = AnimationUtils.loadAnimation(getContext(), R.anim.slidedown);
        final Handler handler = new Handler();
        suggestionsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bioSuggestions.getVisibility() == View.GONE) {
                    bio.setVisibility(View.VISIBLE);
                    block.setVisibility(View.VISIBLE);
                    call.setVisibility(View.VISIBLE);
                    bioSuggestions.startAnimation(slideUp);
                    blockSuggestions.startAnimation(slideUp);
                    callSuggestions.startAnimation(slideUp);
                } else if (bioSuggestions.getVisibility() == View.VISIBLE) {
                    bioSuggestions.startAnimation(slideDown);
                    blockSuggestions.startAnimation(slideDown);
                    callSuggestions.startAnimation(slideDown);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            bio.setVisibility(View.GONE);
                            block.setVisibility(View.GONE);
                            call.setVisibility(View.GONE);
                        }
                    }, 300);
                }
            }
        });

        if (actualPosition != position) {
            if (bio.getVisibility() == View.VISIBLE) {
                bio.setVisibility(View.GONE);
                block.setVisibility(View.GONE);
                call.setVisibility(View.GONE);
            }

            actualPosition = position;

        }

        JSONObject jsonValSuggestions = new getSugestions().sugestionsDetails(position, "suggestions");

        try {
            final String name = jsonValSuggestions.getString("client_name");
            final String num = jsonValSuggestions.getString("client_number");
            final String description = jsonValSuggestions.getString("client_description");

            bio.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent suggestionsDetails = new Intent(view.getContext(), SuggestionsDetails.class);

                    suggestionsDetails.putExtra("client_number", num);
                    suggestionsDetails.putExtra("client_name", name);
                    suggestionsDetails.putExtra("client_description", description);
                    activity.startActivityForResult(suggestionsDetails, position);


                }
            });

            block.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent suggestionBlock = new Intent(view.getContext(), BlockSuggestionsActivity.class);

                    activity.startActivity(suggestionBlock);

                }

            });

            call.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent suggestionCall = new Intent(view.getContext(), CallSuggestionActivity.class);

                    suggestionCall.putExtra("client_number", num);
                    suggestionCall.putExtra("client_name", name);


                    activity.startActivity(suggestionCall);

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (suggestionList.suggestionName.equals("null") || suggestionList.suggestionName.equals("")) {
                suggestionNumber.setText(suggestionList.suggestionNumber);
            } else {
                suggestionNumber.setText(suggestionList.suggestionName);
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                suggestionDescription.setText(Html.fromHtml(suggestionList.suggestionDescription, Html.FROM_HTML_MODE_LEGACY));
            } else {
                suggestionDescription.setText(Html.fromHtml(suggestionList.suggestionDescription));
            }
        } catch (Exception e) {
            Log.i("exception", e.getMessage());
        }

        return convertView;
    }

Part of AsyncTask class

 public static final String REQUEST_METHOD = "GET";

    public static final int READ_TIMEOUT = 15000;
    public static final int CONNECTION_TIMEOUT = 15000;

    @Override
    protected String doInBackground(String... params) {

        String clientUrl = params[0];

        String result;

        String inputLine;
        JSONObject obj;
        String data;
        String message;

        try {
            URL myUrl = new URL(clientUrl);
            HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection();

            connection.setRequestMethod(REQUEST_METHOD);
            connection.setReadTimeout(READ_TIMEOUT);
            connection.setConnectTimeout(CONNECTION_TIMEOUT);

            connection.connect();

            InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
            BufferedReader reader = new BufferedReader(streamReader);
            StringBuilder stringBuilder = new StringBuilder();

            while ((inputLine = reader.readLine()) != null) {
                stringBuilder.append(inputLine);

            }
            reader.close();
            streamReader.close();
            result = stringBuilder.toString();


        } catch (IOException e) {
            e.printStackTrace();
            result = null;
        }
        return result;
    }

public String[] getSuggestionsList() {

    String[] suggestionList = new String[5];
    String result;
    String status;
    JSONObject listObj;
    String suggestionsData;
    JSONObject suggestionsDataObj;
    JSONArray suggestionsDataArr;

    String ClientsSugestionsUrl = "https://example.com/token=" + authToken;

    getApiClientSugestions getSugestionsFromApi = new getApiClientSugestions();

    try {
        result = getSugestionsFromApi.execute(ClientsSugestionsUrl).get();

        try {
            listObj = new JSONObject(result);
            status = listObj.getString("result");
            suggestionsData = listObj.getString("suggestions");
            suggestionsDataArr = new JSONArray(suggestionsData);
        } catch (Exception e) {
            e.printStackTrace();
            suggestionsDataArr = null;
            status = null;
        }

        suggestionList[3] = status;
        suggestionList[4] = suggestionsDataArr.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return suggestionList;
}

Activity

public class CallsSuggestionsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calls_suggestions);
        Slidr.attach(this);
        getSupportActionBar().setTitle("Skontaktuj się");
    }

    @Override
    protected void onResume() {
        super.onResume();
        CallsSuggestionList();
    }

    public void CallsSuggestionList() {

        final ListView suggestionList = findViewById(R.id.sugestionList);

        final ArrayList<SuggestionList> suggestionArray = new ArrayList<SuggestionList>();
        SuggestionListAdapter suggestionListAdapter = new SuggestionListAdapter(getContext(), suggestionArray, this);
        String[] suggestionListArray = new getSugestions().getSuggestionsList();

        String suggStat = suggestionListArray[3];
        String arrayList = suggestionListArray[4];

        String clientName;
        String clientNumber;
        String clientDescription;

        try {
            JSONArray jsonArray = new JSONArray(arrayList);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject explrObject = jsonArray.getJSONObject(i);
                clientName = explrObject.getString("client_name");
                clientNumber = explrObject.getString("client_number");
                clientDescription = explrObject.getString("client_description");

                if (suggStat.equals("true")) {

                    SuggestionList suggestionList1 = new SuggestionList(clientName, clientDescription, clientNumber);

                    suggestionListAdapter.addAll(suggestionList1);
                    suggestionListAdapter.notifyDataSetChanged();
                    suggestionList.setAdapter(suggestionListAdapter);

                }
            }
        } catch (Exception e) {
            Log.i("exception", e.getMessage());
            e.printStackTrace();
            clientName = null;
            clientDescription = null;
            clientNumber = null;
        }


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

SuggestionList

public class SuggestionList {

    public String suggestionNumber;

    public String suggestionDescription;

    public String suggestionCallType;

    public String suggestionName;

    public SuggestionList(
//            String suggestionCallType,
            String suggestionName, String suggestionDescription, String suggestionNumber) {
        this.suggestionNumber = suggestionNumber;
//        this.suggestionCallType = suggestionCallType;
        this.suggestionName = suggestionName;
        this.suggestionDescription = suggestionDescription;
    }

}

Adapter are custom with custom view displayed to user. I use similar custom adapter to show content from sqlite that is on phone and there app isn't so slow. But when I open this activity it slow down dramatically. Also I noticed when I press back button it take very long to back to previous screen.

Upvotes: 1

Views: 173

Answers (1)

hasbi
hasbi

Reputation: 520

The problem is in the getSuggestionsList function. in this function, you are calling getSugestionsFromApi.execute(ClientsSugestionsUrl).get(); which make your code sync again. I mean your code is waiting this code to be executed.

One way (not right way, but easy way): you can call new getSugestions().getSuggestionsList(); in a new thread.

Second way, call getSugestionsFromApi.execute(ClientsSugestionsUrl) without get() function. But to get result of the code, you need to give an interface. To get right usage: https://xelsoft.wordpress.com/2014/11/28/asynctask-implementation-using-callback-interface/

Upvotes: 1

Related Questions