当我一次又一次滚动ListView时,应用程序崩溃

时间:2014-08-04 09:49:25

标签: android listview simplecursoradapter android-cursoradapter android-cursorloader

我使用了一个列表视图,并且有超过65,000条记录,滚动没有快速平滑地完成,而且如果我一次又一次地滚动它,应用程序崩溃,没有任何错误或任何ANR对话框,任何人都可以帮助我来解决这个问题?

我正在使用SimpleCursorLoaderCursorAdapter,以下是我的代码:

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

}

3 个答案:

答案 0 :(得分:1)

可以在这里找到Endless Adapter最好的例子之一

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

答案 1 :(得分:1)

试试这个。滚动你必须回收视图
注意:我没有测试过,它可能有用......

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

答案 2 :(得分:0)

我会在25个记录之后使用加载更多按钮执行此操作,每次listview将加载另外25个记录。示例here是此类实现的最佳示例之一。

您也可以使用滑动刷新GitHub上的api。