Siddhpura Amit
Siddhpura Amit

Reputation: 15078

App crashes when I scroll the ListView again and again

I have used a list view and there are more than 65,000 records, scrolling is not done fast and smooth, and also if I scroll it again and again the app crashes, without any error or any ANR dialog box, can anybody help me to resolve that issue?

I am using SimpleCursorLoader and CursorAdapter and below is my code:

   private static class DotCursorLoader extends SimpleCursorLoader {

    private DbHelper mHelper;
    private CharSequence filter;

    public DotCursorLoader(Context context, DbHelper helper,
            CharSequence filter) {
        super(context);
        mHelper = helper;
        this.filter = filter;
    }

    @Override
    public Cursor loadInBackground() {

        return mHelper.getCursor(filter);
    }

}

private class DotCursorAdapter extends CursorAdapter {

    private Context mContext;
    private Typeface font;

    public class ViewHolder {
        TextView txtEng, txtGuj;
    }

    // public DotCursorAdapter(Context context, Cursor c, int flags) {
    // super(context, c, flags);
    // mContext = context;
    // }

    public DotCursorAdapter(Context context, Cursor c,
            boolean autoRequery) {
        super(context, c, autoRequery);
        mContext = context;

    }

    @Override
    public void bindView(View view, Context context,
            android.database.Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();
        if (LibConstants.isEnglishEnabled(mActivity)) {

            holder.txtEng.setTextColor(settings.getInt(
                    LibConstants.English_Pref_Color_Key,
                    Color.BLACK));
            // txtEng.setTypeface(Typeface.DEFAULT);
            holder.txtEng.setTextSize(Integer.parseInt(settings
                    .getString("TextSize", "20")));
            holder.txtEng.setText(LibConstants.getWord(cursor
                    .getString(1)));
            Utils.setEnglishFont(holder.txtEng, mActivity);

            holder.txtGuj.setTextColor(settings.getInt(
                    LibConstants.Mean_Pref_Color_Key,
                    getResources().getInteger(
                            R.color.blue)));

            holder.txtGuj.setTypeface(font);
            holder.txtGuj.setTextSize(Integer.parseInt(settings
                    .getString("TextSize", "20")));
            holder.txtGuj.setText(LibConstants.getWord(cursor
                    .getString(2)));

        } else {

            holder.txtEng.setTextColor(settings
                    .getInt(LibConstants.Mean_Pref_Color_Key,
                            getResources().getColor(
                                    R.color.blue)));
            holder.txtEng.setTypeface(font);
            holder.txtEng.setTextSize(Integer.parseInt(settings
                    .getString("TextSize", "20")));
            holder.txtEng.setText((cursor
                    .getString(2)));

            holder.txtGuj.setTextColor(settings.getInt(
                    LibConstants.English_Pref_Color_Key,
                    Color.BLACK));
            holder.txtGuj.setTypeface(Typeface.DEFAULT);
            holder.txtGuj.setTextSize(Integer.parseInt(settings
                    .getString("TextSize", "20")));
            holder.txtGuj.setText(cursor
                    .getString(1));
            Utils.setEnglishFont(holder.txtGuj, mActivity);
        }

    }

    @Override
    public View newView(Context context,
            android.database.Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.txtEng = (TextView) view
                .findViewById(R.id.txtEng);
        viewHolder.txtGuj = (TextView) view
                .findViewById(R.id.txtGuj);
        view.setTag(viewHolder);
        return view;
    }

}

Upvotes: 3

Views: 777

Answers (3)

sunil
sunil

Reputation: 701

try this.while scrolling you have to recycle the view
NOTE:i have not tested, it may work...

View con=null; 
 class ViewHolder
     {

         TextView tv,tv1;
         public ViewHolder(View v) {
            // TODO Auto-generated constructor stub

                tv=(TextView)v.findViewById(R.id.phone_number); 
                tv1=(TextView)v.findViewById(R.id.contact_name);


        }
     }
     //
      @Override
    public View newView(Context context,
            android.database.Cursor cursor, ViewGroup parent) {
            ViewHolder viewHolder ;
            if(con==null)
            {
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.list_item, parent, false);
                con=view;
        viewHolder = new ViewHolder(con);

        view.setTag(viewHolder);
         Log.i("getview","new view created");
        }

        return view;
    }

Upvotes: 1

Juned
Juned

Reputation: 6326

One of the best example of Endless Adapter can be found here

https://github.com/commonsguy/cwac-endless

Upvotes: 1

Michele La Ferla
Michele La Ferla

Reputation: 6884

I would do this using a load more button after lets say 25 records, and each time the listview would load another 25 records. The example here is one of the best for this kind of implementation.

You could also use the swipe to refresh api availabel on GitHub.

Upvotes: 0

Related Questions