androidlistviewandroid-cursorandroid-cursoradapter

Listview with CustomCursorAdapter scrolling is not smooth


I am having a ListView with CustomCursorAdapter. The Listview scrolls slowly. Here is the code which I used.

public class CustomCursorAdapter extends CursorAdapter {

private ViewHolder holder=null;
private Activity mContext=null;
private ArrayList<Boolean> mCheckedList=null;
private Button mCancel=null;
private Cursor mCursor = null;
private DatabaseHandler mDbHandler;
private int dStatus;
private LayoutInflater inflater;

public CustomCursorAdapter(Activity context) {
    super(context, cursor);
    this.mCursor = cursor;
    this.mContext = context;
    this.mDbHandler = new DatabaseHandler(context);
    this.inflater = LayoutInflater.from(context);
}


@Override
public void bindView(View view, Context arg1, Cursor c) {
    holder=(ViewHolder) view.getTag();
    long duration = c.getLong(c.getColumnIndex(DatabaseHandler.DURATION));

    holder.checkToSend.setTag(c.getPosition());
    holder.editDitn.setTag(c.getPosition());
    holder.pendingLayout.setTag(c.getPosition());
    holder.name.setText(c.getString(c.getColumnIndex(DatabaseHandler.NAME)));

    holder.worktype.setText(c.getString(c.getColumnIndex(DatabaseHandler.WORKTYPE)));
    holder.comment.setText(c.getString(c.getColumnIndex(DatabaseHandler.COMMENT)));
    int prio = c.getInt(c.getColumnIndex(DatabaseHandler.PRIORITY));
    if(prio == 1)
        holder.priority.setVisibility(View.VISIBLE);
    else if(prio == 0)
        holder.priority.setVisibility(View.INVISIBLE);
    boolean check = mCheckedList.get(c.getPosition());
    holder.checkToSend.setChecked(check);
    onEnableOrDisable(true);
    int dictStat = c.getInt(c.getColumnIndex(DatabaseHandler.STATUS));
    if(c.getInt(c.getColumnIndex(DatabaseHandler.ISACTIVE))==1 || ((dictStat == 2 || dictStat == 3)&&!DMApplication.isEditMode()))
        holder.checkToSend.setVisibility(View.GONE);
    else
        holder.checkToSend.setVisibility(View.VISIBLE);

    switch (dictStat) {
    case 1:
        holder.status.setText("");
        if(DMApplication.isOnEditState()&&DMApplication.getTabPos()==1)
            holder.checkToSend.setVisibility(View.GONE);
        else
            holder.checkToSend.setVisibility(View.VISIBLE);
        onEnableOrDisable(true);
        break;
    case 111:
        holder.status.setTextColor(mContext.getResources().getColor(R.color.status_blue));
        onEnableOrDisable(false);
        holder.status.setText(mContext.getResources().getString(R.string.Property_sending));
        break;
    case 222:
        holder.status.setTextColor(mContext.getResources().getColor(R.color.status_red));
        holder.status.setText(mContext.getResources().getString(R.string.Property_Timeout));
        onEnableOrDisable(true);
        break;
    case 333:
        holder.status.setTextColor(mContext.getResources().getColor(R.color.status_blue));
        holder.status.setText(mContext.getResources().getString(R.string.Property_Retrying));
        onEnableOrDisable(false);
        break;
    case 444:
        holder.status.setTextColor(mContext.getResources().getColor(R.color.status_red));
        holder.status.setText(mContext.getResources().getString(R.string.Property_Conversion_Failed));
        onEnableOrDisable(true);
        break;
    case 555:
        holder.status.setTextColor(mContext.getResources().getColor(R.color.status_blue));
        onEnableOrDisable(false);
        holder.status.setText(mContext.getResources().getString(R.string.Property_Waiting_Send));
        break;
    case 20:
        holder.status.setTextColor(mContext.getResources().getColor(R.color.status_red));
        holder.status.setText(mContext.getResources().getString(R.string.property_sending_failed));
        onEnableOrDisable(true);
        break;
    case 3:
        holder.status.setText(mContext.getResources().getString(R.string.Property_Via_Email));
        onEnableOrDisable(true);
        break;

    default:
        holder.status.setText("");
        onEnableOrDisable(true);
        break;
    }
    int isflashair = c.getInt(c.getColumnIndex(DatabaseHandler.ISFLASHAIR));
    String formattedDur = Utilities.getDurationInTimerFormat(duration);
    if(isflashair == 0){
        holder.imgflash.setVisibility(View.GONE);
        holder.editDitn.setVisibility(View.VISIBLE);
        holder.pendingLayout.setClickable(true);
        if(dictStat == 2 || dictStat == 3){
            holder.date.setText(DMApplication.getLocalizedDateAndTime(c.getString(c.getColumnIndex(DatabaseHandler.SENT_DATE)))+"  "
                +formattedDur);
        }else{
            holder.date.setText(DMApplication.getLocalizedDateAndTime(c.getString(c.getColumnIndex(DatabaseHandler.REC_END_DATE)))+"  "
                    +formattedDur);
        }
    }else{
        holder.imgflash.setVisibility(View.VISIBLE);
        holder.editDitn.setVisibility(View.GONE);
        holder.pendingLayout.setClickable(false);
        if(dictStat == 2 || dictStat == 3){
            holder.date.setText(DMApplication.getLocalizedDateAndTime(c.getString(c.getColumnIndex(DatabaseHandler.SENT_DATE))));
        }else{

            holder.date.setText(DMApplication.getLocalizedDateAndTime(c.getString(c.getColumnIndex(DatabaseHandler.REC_END_DATE))));
        }

    }

    notifyDataSetChanged();
}
private void  onEnableOrDisable(boolean enable)
{
    if(DMApplication.getTabPos()==1)
    {
        if(enable)
        {
            holder.checkToSend.setEnabled(true);
            holder.pendingLayout.setEnabled(true);
            holder.editDitn.setEnabled(true);
        }
        else
        {
            holder.checkToSend.setVisibility(View.GONE);
            holder.checkToSend.setEnabled(false);
            holder.pendingLayout.setEnabled(false);
            holder.editDitn.setEnabled(false);
        }
    }
}

@Override
public View newView(Context arg0,final Cursor cursor, ViewGroup parent) {
    View retView = inflater.inflate(R.layout.tab_list_layout, parent, false);
    holder = new ViewHolder();
    holder.name = (TextView) retView.findViewById(R.id.text_tab_list_dictation_name);
    holder.worktype = (TextView) retView.findViewById(R.id.text_tab_list_worktype);
    holder.comment = (TextView) retView.findViewById(R.id.text_tab_list_comment);
    holder.date = (TextView) retView.findViewById(R.id.text_tab_list_dictation_date);
    holder.status = (TextView) retView.findViewById(R.id.text_tab_list_dictation_status);
    holder.checkToSend = (CheckBox) retView.findViewById(R.id.chkToSend);
    holder.priority = (ImageView) retView.findViewById(R.id.img_tab_list_priority);
    holder.editDitn = (ImageButton) retView.findViewById(R.id.img_tab_list_edit_dictation);
    holder.imgflash = (ImageView) retView.findViewById(R.id.img_is_flashair);
    holder.pendingLayout=(RelativeLayout)retView.findViewById(R.id.relativeTabDictateDetails);
    holder.pendingLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (Integer) v.getTag();             
            Cursor c = (Cursor) getItem(pos);
            // some code

        }
    });
    holder.checkToSend.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (Integer) v.getTag();
            CheckBox chk = (CheckBox) v;
            // some code
        }
    });
    holder.editDitn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int pos = (Integer) v.getTag();
            Cursor c = (Cursor) getItem(pos);
            // some code
        }
    });
    retView.setTag(holder);
    return retView;
}

static class ViewHolder{
    TextView name = null;
    TextView date = null;
    TextView worktype = null;
    TextView comment = null;
    TextView status = null;
    CheckBox checkToSend = null;
    ImageView priority = null;
    RelativeLayout pendingLayout=null;
    ImageButton editDitn = null;
    ImageView imgflash = null;
}
}

Please let me know whether anything wrong in my code.


Solution

  • yes you can use the getView in place of newView.as it provides you with a recycled View as a counterview.

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
        ViewHolder holder;
        View retView = convertView;
    
        if (convertView == null) {
            retView = inflater.inflate(R.layout.tab_list_layout, parent, false);
            holder = new ViewHolder();
            holder.name = (TextView) retView.findViewById(R.id.text_tab_list_dictation_name);
            holder.worktype = (TextView) retView.findViewById(R.id.text_tab_list_worktype);
            holder.comment = (TextView) retView.findViewById(R.id.text_tab_list_comment);
            holder.date = (TextView) retView.findViewById(R.id.text_tab_list_dictation_date);
            holder.status = (TextView) retView.findViewById(R.id.text_tab_list_dictation_status);
            holder.checkToSend = (CheckBox) retView.findViewById(R.id.chkToSend);
            holder.priority = (ImageView) retView.findViewById(R.id.img_tab_list_priority);
            holder.editDitn = (ImageButton) retView.findViewById(R.id.img_tab_list_edit_dictation);
            holder.imgflash = (ImageView) retView.findViewById(R.id.img_is_flashair);
            holder.pendingLayout=(RelativeLayout)retView.findViewById(R.id.relativeTabDictateDetails);
            holder.pendingLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = (Integer) v.getTag();             
                    Cursor c = (Cursor) getItem(pos);
                    // some code
    
                }
            });
            holder.checkToSend.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = (Integer) v.getTag();
                    CheckBox chk = (CheckBox) v;
                    // some code
                }
            });
            holder.editDitn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    int pos = (Integer) v.getTag();
                    Cursor c = (Cursor) getItem(pos);
                    // some code
                }
            });
            retView.setTag(holder);
           }
    
         else {
    
        holder = (ViewHolder) retView.getTag();
                }
        return retView ;
        }