【问题标题】:How to create listview with more than 100 images and texts in android?如何在 android 中创建包含 100 多个图像和文本的列表视图?
【发布时间】:2015-03-28 14:55:12
【问题描述】:

我收到 OutofMemory 错误和更多异常 ** 不从可绘制文件夹中加载所有数据..plz 建议我任何解决方案..

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);

  }
    }

这是 arrayadapter 类,我该如何解决这个问题 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>

singlelist.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>

【问题讨论】:

标签: android listview android-arrayadapter


【解决方案1】:

将您的自定义适配器更改为:

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;
    }
}

【讨论】:

  • 如何在listview上使用onscrollListener
【解决方案2】:

首先,当您生成列表视图时,您的代码效率不高。 this answer 可以帮你解决这个问题。

其次,您一次要处理大量资源,您可以避免在弱设备上出现内存不足,以防止应用崩溃和关闭,只需添加一个 try catch:

catch(OutOfMemoryError e){}

最后,很多库可以optimize your listview constructionlazy load imageviews

希望对你有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多