3

I am getting error OutofMemory and more exceptions ** Does not load all data from drawable folder ..plz suggest me any solution..

Main.java

public class Main extends Activity{

 ListView list;

 String[] pool = { "Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool A", "Pool B","Pool A", "Pool B","Pool A", "Pool B","Pool A"  };

  String[] date1 = {
    "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
      "01-02-2014",
  } ;

  String[] venue = { "Australia ", "ustralia " , "Australia ", "ustralia " ,"Australia ", "ustralia " , "Australia ", "Australia ", "ustralia " , "Australia ", "ustralia " ,"Australia ", "ustralia " , "Australia ", "Australia ", "ustralia " , "Australia ", "ustralia " ,"Australia ", "ustralia fgasfhdgsdj" , "Australia ", "Australia ", "ustralia " , "Australia ", "ustralia " ,"Australia ", "ustralia " , "Australia ", "Australia ", "ustralia fgasfhdgsdj" , "Australia ", "ustralia " ,"Australia ", "ustralia " , "Australia "};

  String[] time = { "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm", "09:30 pm" };

  String[] country1 = {
            "India",
              "New Zealand",
              "West Indies",
              "West Indies",
              "Australia",
              "Australia",
              "Australia",
              "India",
              "New Zealand",
              "West Indies",
              "West Indies",
              "Australia",
              "Australia",
              "Australia",
              "India",
              "New Zealand",
              "West Indies",
              "West Indies",
              "Australia",
              "Australia",
              "Australia",
              "India",
              "New Zealand",
              "West Indies",
              "West Indies",
              "Australia",
              "Australia",
              "Australia",
              "India",
              "New Zealand",
              "West Indies",
              "West Indies",
              "Australia",
              "Australia",
              "Australia"
          } ;

  String[] country2 = {
            "India",
              "New Zealand",
              "West Indies",
              "West Indies",
              "Australia",
              "Australia",
              "Australia",
              "India",
              "New Zealand",
              "West Indies",
              "West Indies",
              "Australia",
              "Australia",
              "Australia",
              "India",
              "New Zealand",
              "West Indies",
              "West Indies",
              "Australia",
              "Australia",
              "Australia",
              "India",
              "New Zealand",
              "West Indies",
              "West Indies",
              "Australia",
              "Australia",
              "Australia",
              "India",
              "New Zealand",
              "West Indies",
              "West Indies",
              "Australia",
              "Australia",
              "Australia"
          } ;

  String[] vs = { "Vs", "Vs","Vs","Vs","Vs","Vs","Vs", "Vs", "Vs","Vs","Vs","Vs","Vs","Vs", "Vs", "Vs","Vs","Vs","Vs","Vs","Vs", "Vs", "Vs","Vs","Vs","Vs","Vs","Vs", "Vs", "Vs","Vs","Vs","Vs","Vs","Vs" };

  Integer[] imageId = {
      R.drawable.afghanistan,
      R.drawable.australia,
      R.drawable.bangladesh,
      R.drawable.england,
      R.drawable.newzealand,
      R.drawable.scotland,
      R.drawable.srilanka,
      R.drawable.afghanistan,
      R.drawable.australia,
      R.drawable.bangladesh,
      R.drawable.england,
      R.drawable.newzealand,
      R.drawable.scotland,
      R.drawable.srilanka,
      R.drawable.afghanistan,
      R.drawable.australia,
      R.drawable.bangladesh,
      R.drawable.england,
      R.drawable.newzealand,
      R.drawable.scotland,
      R.drawable.srilanka,
      R.drawable.afghanistan,
      R.drawable.australia,
      R.drawable.bangladesh,
      R.drawable.england,
      R.drawable.newzealand,
      R.drawable.scotland,
      R.drawable.srilanka,
      R.drawable.afghanistan,
      R.drawable.australia,
      R.drawable.bangladesh,
      R.drawable.england,
      R.drawable.newzealand,
      R.drawable.scotland,
      R.drawable.srilanka
  };
  Integer[] imageId2 = {
          R.drawable.afghanistan,
          R.drawable.australia,
          R.drawable.bangladesh,
          R.drawable.england,
          R.drawable.newzealand,
          R.drawable.scotland,
          R.drawable.srilanka,
          R.drawable.afghanistan,
          R.drawable.australia,
          R.drawable.bangladesh,
          R.drawable.england,
          R.drawable.newzealand,
          R.drawable.scotland,
          R.drawable.srilanka,
          R.drawable.afghanistan,
          R.drawable.australia,
          R.drawable.bangladesh,
          R.drawable.england,
          R.drawable.newzealand,
          R.drawable.scotland,
          R.drawable.srilanka,
          R.drawable.afghanistan,
          R.drawable.australia,
          R.drawable.bangladesh,
          R.drawable.england,
          R.drawable.newzealand,
          R.drawable.scotland,
          R.drawable.srilanka,
          R.drawable.afghanistan,
          R.drawable.australia,
          R.drawable.bangladesh,
          R.drawable.england,
          R.drawable.newzealand,
          R.drawable.scotland,
          R.drawable.srilanka,

      };
  @Override
  protected void onCreate(Bundle savedInstanceState) throws OutOfMemoryError {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main1);
    try
    {
    Custom_List1 adapter = new Custom_List1(Main.this, pool, date1, venue, time, country1, country2, vs, imageId, imageId2);
    list=(ListView)findViewById(R.id.list);
    list.setAdapter(adapter);

  }
    }

This is arrayadapter class and how can i solve this problem Custom_List1.java

public class Custom_List1 extends ArrayAdapter<String>{

private final Activity context;

private final String[] pool;
private final String[] date1;
private final String[] venue;
private final String[] time;

private final String[] country1;  //web
private final String[] country2;
private final String[] vs;
private final Integer[] imageId;
private final Integer[] imageId2;
public Custom_List1(Activity context, String[] pool, String[] date1, String[] venue, String[] time, String[] country1, String[] country2,String[] vs, Integer[] imageId, Integer[] imageId2) 
{
 super(context, R.layout.list_single1, country1);
 this.context = context;
 this.pool = pool;
 this.date1 = date1;
 this.venue = venue;
 this.time = time;
 this.country1 = country1;
 this.country2 = country2;
 this.vs = vs;
 this.imageId = imageId;
 this.imageId2 = imageId2;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single1, null, true);

TextView poolT = (TextView) rowView.findViewById(R.id.txtPool);
TextView date1T = (TextView) rowView.findViewById(R.id.txtDate);
TextView venueT = (TextView) rowView.findViewById(R.id.txtVenue1);
TextView timeT = (TextView) rowView.findViewById(R.id.txtTime);
TextView country1T = (TextView) rowView.findViewById(R.id.txtCountry1);
TextView country2T = (TextView) rowView.findViewById(R.id.txtCountry2);
TextView vsT = (TextView) rowView.findViewById(R.id.vs);

ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
ImageView imageView1 = (ImageView) rowView.findViewById(R.id.img2);

poolT.setText(pool[position]);
date1T.setText(date1[position]);
venueT.setText(venue[position]);
timeT.setText(time[position]);
country1T.setText(country1[position]);
country2T.setText(country2[position]);
vsT.setText(vs[position]);

imageView.setImageResource(imageId[position]);
imageView1.setImageResource(imageId[position]);
return rowView;
}

}

list.xml

<ListView
   android:id="@+id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_marginTop="20sp"
    android:dividerHeight="10.0sp">
</ListView>

sinlglelist.xml

   <TableRow>
       <ImageView
        android:id="@+id/img"
        android:layout_width="50dp"
        android:layout_height="50dp"/>
      <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="50dp" 
        android:layout_marginLeft="20sp"/>
       <ImageView 
        android:id="@+id/img2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="10sp"/>

 </TableRow>

2 Answers2

0

Change Your custom Adapter to this:

public class Custom_List1 extends ArrayAdapter<String>{

...

@Override
    public View getView(final int position, View rowView, ViewGroup parent) {
        ViewHolder holder;

            if (rowView == null) {
                rowView = LayoutInflater.from(context).inflate(
                        R.layout.list_single1, null);
                holder = new ViewHolder();

                holder.poolT = (TextView) rowView.findViewById(R.id.txtPool);
                holder.date1T = (TextView) rowView.findViewById(R.id.txtDate);
                holder.venueT = (TextView) rowView.findViewById(R.id.txtVenue1);
                holder.timeT = (TextView) rowView.findViewById(R.id.txtTime);
                holder.country1T = (TextView) rowView.findViewById(R.id.txtCountry1);
                holder.country2T = (TextView) rowView.findViewById(R.id.txtCountry2);
                holder.vsT = (TextView) rowView.findViewById(R.id.vs);

                holder.imageView = (ImageView) rowView.findViewById(R.id.img);
                holder.imageView1 = (ImageView) rowView.findViewById(R.id.img2);

                rowView.setTag(holder);
            } else {
                holder = (ViewHolder) rowView.getTag();
            }

            holder.poolT.setText(pool[position]);
            holder.date1T.setText(date1[position]);
            holder.venueT.setText(venue[position]);
            holder.timeT.setText(time[position]);
            holder.country1T.setText(country1[position]);
            holder.country2T.setText(country2[position]);
            holder.vsT.setText(vs[position]);

            holder.imageView.setImageResource(imageId[position]);
            holder.imageView1.setImageResource(imageId[position]);

            return rowView;

}

public static class ViewHolder {
        TextView poolT;
        TextView date1T;
        TextView venueT;
        TextView timeT;
        TextView country1T;
        TextView country2T;
        TextView vsT;

        ImageView imageView ;
        ImageView imageView1;
    }
}
arpit
  • 1,462
  • 1
  • 12
  • 12
0

First of all, your code is not efficient when you generate your listview. this answer can help you fix that.

Second, your are handling a lot of resources at a time and you can avoid out of memory on weak devices to prevent the app to crash and shut down just adding a try catch:

catch(OutOfMemoryError e){}

finally, a lot of libraries can optimize your listview construction and lazy load imageviews

Hope to be helpful

Community
  • 1
  • 1
PedroHawk
  • 622
  • 5
  • 19