Reputation: 305
Hi I need to manage the large set of data in Listview in android app. The list items may be around 250 or more than that. Now my problem is when I scroll the listview it crashes the app due more items. But the listview will working fine when the items are less than 100.
Please help me to solve this.
This is my Error Log.
02-13 06:43:07.320: ERROR/AndroidRuntime(1776): Caused by: java.lang.OutOfMemoryError
android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:577)
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:738)
android.content.res.Resources.loadDrawable(Resources.java:1869)
android.content.res.Resources.getDrawable(Resources.java:659)
android.widget.ScrollView.setOverScrollMode(ScrollView.java:1487)
android.view.ViewGroup.<init>(ViewGroup.java:365)
android.widget.FrameLayout.<init>(FrameLayout.java:94)
android.widget.ScrollView.<init>(ScrollView.java:155)
android.widget.ScrollView.<init>(ScrollView.java:151)
And my getView()
is
@Override
public View getView(final int position, View view, ViewGroup parent) {
final int pos = position;
views = null;
LayoutInflater layoutInflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
views = (RelativeLayout) layoutInflator.inflate(
R.layout.guest_list_item, null);
final CheckBox chk = (CheckBox) views.getChildAt(0);
// Log.d("", "CheckBox Pos "+position);
chk.setId(position);
TextView txtView = (TextView) views.getChildAt(1);
TextView txtView2 = (TextView) views.getChildAt(2);
TextView txtView3 = (TextView) views.getChildAt(3);
final TextView txtView4 = (TextView) views.getChildAt(4);
TextView txtView5 = (TextView) views.getChildAt(6);
txtView5.setId(position);
// Log.d("", "Button Pos "+position);
txtView4.setId(position);
txtView4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, InfoScreen.class);
intent.putExtra("IDVALUE", arTempId.get(txtView4.getId()));
intent.putExtra("IDVALUE", arrayListId.get(position));
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(intent);
// System.out.println(v + "##########" + " " + v.getId());
// System.out.println(v + "##########" + " " +
// arTempId.get(txtView4.getId()));
// System.out.println(v + "##########" + " " +
// chickinlist.get(txtView4.getId()));
}
});
Log.e("", "****************************************************** ");
// Log.v("", "Adapter arr pos " + pos);
// Log.v("", "Adapter arr position " + position);
Log.v("", "Adapter arr size " + arrayListFirstName.size());
Log.v("", "Passsing arr size " + chickinlist.size());
for (int dd = 0; dd < arrayListFirstName.size(); dd++) {
if (position == dd) {
// Log.d("", "Passsing arr size " + chickinlist.size());
Boolean result = chickinlist.get(position);
// Log.d("", "After " + result);
if (result == true) {
chk.setChecked(true);
arrCheckedItems.add(position);
views.setBackgroundResource(R.drawable.list_item_checked);
} else {
chk.setChecked(false);
arrUnCheckedItems.add(position);
views.setBackgroundResource(R.drawable.list_item_unchecked);
}
txtView.setText(arrayListFirstName.get(position));
txtView2.setText(arrayListLastName.get(position));
txtView3.setText("(" + arrayListGuests.get(position) + ")");
if (arrayCustomOne.get(position).equalsIgnoreCase("0")
|| arrayCustomOne.get(position).equalsIgnoreCase(null)
|| arrayCustomOne.get(position).equalsIgnoreCase(
"(null)")) {
txtView5.setText("");
} else {
txtView5.setText(arrayCustomOne.get(position));
}
}
}
chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mySqliteAdapter.openToWrite();
sId = arrayListId.get(chk.getId());
// Log.d("Adapter",
// "Checked Temp Id "+arTempId.get(chk.getId()));
Log.d("",
"Position " + chk.getId() + "tempid "
+ arTempId.get(chk.getId()));
if (isChecked) {
chk.setChecked(true);
arrCheckedItems.add(position);
strAlertValue = "1";
sCheckIn = "1";
strExe = "update ticket_details set checkin_status=1 where tempid="
+ arTempId.get(chk.getId());
mySqliteAdapter.executeCheckQurey(strExe);
if (Util.getInstance(context).isTabletDevice()) {
buttonView
.setBackgroundResource(R.drawable.tablet_list_item_check);
} else {
buttonView
.setBackgroundResource(R.drawable.list_item_checked);
}
new CheckInUpdate().execute();
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.create();
builder.setMessage(arrayListFirstName.get(chk.getId())
+ " " + arrayListLastName.get(chk.getId())
+ " has been checked in.");
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// myGuestList = new GuestListScreen();
//
// // myGuestList.ListUpdate();
//Intent intentNavGuestList = new Intent(
//context, GuestListScreen.class);
//intentNavGuestList
// .addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
//context.startActivity(intentNavGuestList);
dialog.dismiss();
}
}).show();
// Log.d("", "ID = " + buttonView.getId());
}
if (!isChecked) {
sCheckIn = "0";
strAlertValue = "0";
// views.setBackgroundResource(R.drawable.list_item_unchecked);
strExe = "update ticket_details set checkin_status=0 where tempid="
+ arTempId.get(chk.getId());
mySqliteAdapter.executeCheckQurey(strExe);
if (Util.getInstance(context).isTabletDevice()) {
buttonView
.setBackgroundResource(R.drawable.tablet_list_item_uncheck);
} else {
buttonView
.setBackgroundResource(R.drawable.list_item_unchecked);
}
new CheckInUpdate().execute();
Intent intentNavGuestList = new Intent(context,
GuestListScreen.class);
intentNavGuestList
.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(intentNavGuestList);
// Toast.makeText(context, "check release",
// Toast.LENGTH_SHORT)
// .show();
// new CheckInUpdate().execute();
}
}
});
return views;
}
Upvotes: 1
Views: 2350
Reputation: 560
Holder pattern is a right answer.It will be better if you implement "Pagination" pattern for listview. Show next items when listview scrolled to end. Also avoid to do long running task in Adapter. Put data and adapter separate. Please see use notifyDataSetChanged () method.
Upvotes: 1
Reputation: 5391
Guess - You are not re-using views .
Try using the View Holder -Here are some links -
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
http://vsvydenko.blogspot.in/2011/06/android-use-viewholder.html
http://www.jmanzano.es/blog/?p=166
Suggestion -
Make your adapter implement OnClickListener and OnCheckedChangeListener .
public class YourAdapter implements OnCheckedChangeListener, OnClickListener....
And then handle these events separately by -
@Override
public void onClick(View v) {
// Do your stuff here
}
Like wise for OnCheckedChangeListener.
This way the code will have better readibility and easy to debug.
Upvotes: 1