1

嗨,我需要在 Android 应用程序的 Listview 中管理大量数据。列表项可能在 250 左右或更多。现在我的问题是,当我滚动列表视图时,由于更多项目,它会使应用程序崩溃。但是当项目少于 100 时,列表视图将正常工作。

请帮我解决这个问题。

这是我的错误日志。

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)

而我getView()的是

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

}
4

2 回答 2

1

猜猜-您没有重复使用视图。
尝试使用 View Holder -这里有一些链接 -
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

建议 - 让您的适配器实现OnClickListenerOnCheckedChangeListener

public class YourAdapter  implements OnCheckedChangeListener, OnClickListener....

然后分别处理这些事件 -

@Override
public void onClick(View v) {
    // Do your stuff here

}

就像OnCheckedChangeListener一样明智。

这样代码将具有更好的可读性和易于调试。

于 2013-02-13T06:36:04.727 回答
1

持有人模式是一个正确的答案。如果您为列表视图实现“分页”模式会更好。当列表视图滚动到结束时显示下一个项目。还要避免在适配器中执行长时间运行的任务。将数据和适配器分开。请参阅使用 notifyDataSetChanged() 方法。

于 2013-02-13T06:48:13.340 回答