0

I am using CursorAdapter and ViewHolder to upload data from database to listView. But now I need dynamically add some custom item to bottom of the listView without using cursorAdapter(not from database) and after that - update listView.

here is my cursorAdapter:

public class MyCustomCursorAdapter extends CursorAdapter {

    private static final int VIEW_TYPE_COUNT = 2;
    public static final int VIEW_TYPE_INPUT = 1;
    public static final int VIEW_TYPE_OUTPUT = 2;
public static class ViewHolder {
        public final CircularImageView mAvatar;
        public final ImageView mLanStatus;
        public final TextView mInputMessage;
        public final TextView mDate;

        public ViewHolder(View view){
            mAvatar = (CircularImageView) view.findViewById(R.id.circularImageView_itemDialog_userAvatar);
            mLanStatus = (ImageView)view.findViewById(R.id.imageView_itemDialog_lanStatus);
            mInputMessage = (TextView) view.findViewById(R.id.textView_itemDialog_message);
            mDate = (TextView) view.findViewById(R.id.textView_itemDialog_time);
        }
    }

public DialogAdapter(Activity activity, Cursor c, int flag) {
        super(activity, c, flag);
        mActivity = activity;
    }

@Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        int layoutId;
        if (mViewType == VIEW_TYPE_MESSAGE_OUTPUT){
            layoutId = R.layout.list_item_dialog_message_output;
        } else {
            layoutId = R.layout.list_item_dialog_message_input;
        }

        View rootView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        mViewHolder = new ViewHolder(rootView);
        rootView.setTag(mViewHolder);

        return rootView;
    }

@Override
    public void bindView(View view, Context context, Cursor cursor) {

        mViewHolder = (ViewHolder) view.getTag();

        mMessageId = cursor.getString(DialogActivityFragment.COL_MESSAGE_ID);
String message = cursor.getString(DialogActivityFragment.COL_BODY);
String messageDate = cursor.getString(DialogActivityFragment.COL_DATE);
String avatarUrl = cursor.getString(DialogActivityFragment.COL_AVATAR_URL);
        int onlineStatus = cursor.getInt(DialogActivityFragment.COL_ONLINE_STATUS);
uploadUserAvatar(avatarUrl, onlineStatus);
        setUserMessageData(message, messageDate);
    }

@Override
    public int getViewTypeCount(){
        return VIEW_TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        Cursor cursor = (Cursor)getItem(position);
        return getItemViewType(cursor);
    }

    private int getItemViewType(Cursor cursor) {
        int mainUserId = SharedPrefsClass.getUserId(mActivity);
        int opponentUserId = cursor.getInt(DialogActivityFragment.COL_USER_ID);

        if (mainUserId == opponentUserId){
            mViewType = VIEW_TYPE_MESSAGE_OUTPUT;
        } else {
            mViewType = VIEW_TYPE_MESSAGE_INPUT;
        }
        return mViewType;
    }

will be glad any solution! Thanks!

Stan Malcolm
  • 2,740
  • 5
  • 32
  • 53

0 Answers0