Reputation: 103
I have a ListView in my android application, and it is very laggy. I have some images in the ListView and they are quite large, so that might just be the problem. But i need them to be large, so i was wondering if there is anything I could do to keep them large, without getting all the lag? Hope You understand. Here is my code.
package com.xxx.xxx;
import java.util.List;
import java.util.Vector;
import com.xxx.xxx.R;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
public static final String[] title = new String[] {
"IMPROSCENE",
"RASMUS WALTER",
"VIL DU STUDERE I UDLANDET?",
"INDIANS",
"BAZAR’O’RAMA",
"ON AIR FESTIVAL",
"DEN SORTE SKOLE",
"THE FLOOR IS MADE OF LAVA",
"The Punch Comedy Club",
"HIGHASAKITE (N)",
"SLIM CESSNA'S AUTO CLUB (US)",
"MARIE KEY",
"WOA Metal Battle Tour",
"FRITZ KALKBRENNER",
"BARBARA MOLEKO",
"WHEN SAINTS GO MACHINE",};
public static final String[] detail = new String[] {
"Tir 09/04 KL. 20:00 \nGratis Adgang",
"Tor 11/04 KL. 21:00 \nEntré: Kr. 135 + evt. gebyr",
"Tor 11/04 KL. 13:00 \nGratis Adgang",
"Fre 12/04 KL. 21:00 \nEntré: Kr. 80 + evt. gebyr",
"Søn 14/04 KL. 11:00 \nGratis Adgang",
"Lør 20/04 KL. 15:00 \nGratis Adgang",
"Tor 25/04 KL. 21:00 \nEntré: Kr. 130 + evt. gebyr",
"Lør 27/04 KL. 21:00 \nEntré: Kr. 140 + evt. gebyr",
"Tir 30/04 KL. 20:00 \nGratis Adgang",
"Tor 02/05 KL. 21:00 \nEntré: Kr. 80 + evt. gebyr",
"Fre 03/05 KL. 22:00 \nEntré: Kr. 70 + evt. gebyr",
"Lør 04/05 KL. 21:00 \nEntré: Kr. 130 + evt. gebyr",
"Fre 10/05 KL. 21:00 \nEntré: Kr. 75 + evt. gebyr",
"Lør 25/05 KL. 22:00 \nEntré: Kr. 100 + evt. gebyr",
"Fre 13/09 KL. 21:00 \nEntré: Kr. 110 + evt. gebyr",
"Lør 12/10 KL. 21:00 \nEntré: Kr. 140 + evt. gebyr",};
public static Integer[] imgid = {
R.drawable.1, R.drawable.2,
R.drawable.3, R.drawable.4,
R.drawable.5, R.drawable.6,
R.drawable.7, R.drawable.8,
R.drawable.9, R.drawable.10,
R.drawable.11, R.drawable.12,
R.drawable.13, R.drawable.14,
R.drawable.15, R.drawable.16,
R.drawable.17, R.drawable.18,
R.drawable.19, R.drawable.20,
};
public static final String[] footer = new String[] {
"\nTEXT",
"\nTEXT",
"\nTEXT",
"\nTEXT,
"\nTEXT",
"\nTEXT",
"\nTEXT",
"\nTEXT,
"\nTEXT",
"\nTEXT",
"\nTEXT",
"\nTEXT,
"\nTEXT",
"\nTEXT",
"\nTEXT",
"\nTEXT,
};
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for (int i = 0; i < title.length; i++) {
try {
rd = new RowData(i, title[i], detail[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.list_item, R.id.title, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("position", position);
startActivity(intent);
}
private class RowData {
protected int mId;
protected String mTitle;
protected String mDetail;
RowData(int id, String title, String detail) {
mId = id;
mTitle = title;
mDetail = detail;
}
@Override
public String toString() {
return mId + " " + mTitle + " " + mDetail;
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource,
int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
TextView detail = null;
ImageView i11 = null;
RowData rowData = getItem(position);
if (null == convertView) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
title.setText(rowData.mTitle);
detail = holder.getdetail();
detail.setText(rowData.mDetail);
i11 = holder.getImage();
i11.setImageResource(imgid[rowData.mId]);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private TextView detail = null;
private ImageView i11 = null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if (null == title) {
title = (TextView) mRow.findViewById(R.id.title);
}
return title;
}
public TextView getdetail() {
if (null == detail) {
detail = (TextView) mRow.findViewById(R.id.detail);
}
return detail;
}
public ImageView getImage() {
if (null == i11) {
i11 = (ImageView) mRow.findViewById(R.id.img);
}
return i11;
}
}
}
}
Upvotes: 0
Views: 742
Reputation: 3530
There can be several reasons (large images, number of elements...) that could impact general performances of listview.
You should probably caching all your images data (on sdcard) and loading image with a loader (here is an example).
May i suggest you this videos, there is some other tips to otpimize listview with android.
Upvotes: 1
Reputation: 2464
you can universal image loader. so it will handle all your burden.
Upvotes: 0