0

I have an application that displays some local images in a listview. When I run the app the logcat says

Skipped 43 frames! The application may be doing too much work on its main thread.

Any suggestions will be helpful, Thanks!

Here is the code

MainActivity.java

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

    ListView list;
    String[] web = {
            "Google Plus",
            "Twitter",
            "Tall",
    };

    Integer[] imageId = {
            R.drawable.gggggggggg,
            R.drawable.hhhhhhhhhh,
            R.drawable.ttttttttttt
    };

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

        CustomList adapter = new
                CustomList(MainActivity.this, web, imageId);
        list=(ListView)findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();

            }
        });

    }
}

Here is CustomList.java

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class CustomList extends ArrayAdapter<String>{

    private final Activity context;
    private final Integer[] imageId;
    public CustomList(Activity context,
                      String[] web, Integer[] imageId) {
        super(context, R.layout.list_single, web);
        this.context = context;
        this.imageId = imageId;

    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {

        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.list_single, null, true);


        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        imageView.setImageResource(imageId[position]);

        return rowView;

    }
}

3 Answers3

1

You may try to use the ViewHolder pattern to optimize the performance of your ListView.

See here: https://dzone.com/articles/optimizing-your-listview

Ugurcan Yildirim
  • 5,973
  • 3
  • 42
  • 73
1

1) use viewHolder to save all your .findViewById - coz this is heavy operation for android for now it always call this while you are scrolling.

2) for load images better use ImageLoader from Volley library it has cache for images and you will save lot of ram.

3) for clicks on items organize interface in adaptor and then implement it in your activity so on clicks you will be able to call your interfaces and action for them in your activity.

Stepan Maksymov
  • 2,618
  • 19
  • 31
0

Use AsyncTask and do all your stuff in AsyncTask. AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

https://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/

Dipali Shah
  • 3,742
  • 32
  • 47