在ListView中切割日期标题,其中包含由CursorAdapter扩展的适配器

时间:2015-07-22 12:38:48

标签: android android-listview cursor android-cursoradapter

我正在尝试构建一个演示聊天App.I想要显示带有节标题的消息,如“今天”,“昨天”,“2015年5月21日”等日期。我已经成功实现了这一目标但是新的每当我滚动列表时都会调用View方法。标题和消息会混淆。

为简单起见,我已将标题保留在布局中,并在日期更改时更改其可见性(已消失且可见)。

你可以帮我解决这个问题吗?如果有人需要在问题中发布更多信息,请告诉我。

public class ChatssAdapter extends CursorAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private Cursor mCursor;
    private String mMyName, mMyColor, mMyImage, mMyPhone;
    // private List<Contact> mContactsList;
    private FragmentActivity mActivity;
    private boolean mIsGroupChat;

    public ChatssAdapter(Context context, Cursor c, boolean groupChat) {
        super(context, c, false);
        mContext = context;
        mMyColor = Constants.getMyColor(context);
        mMyName = Constants.getMyName(context);
        mMyImage = Constants.getMyImageUrl(context);
        mMyPhone = Constants.getMyPhone(context);
        mIsGroupChat = groupChat;
        mCursor = c;
        // mActivity = fragmentActivity;
        /*try {
            mContactsList = PinchDb.getHelper(mContext).getContactDao().queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }*/
    }

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


    private int getItemViewType(Cursor cursor) {
        boolean type;

        if (mIsGroupChat)
            type = cursor.getString(cursor.getColumnIndex(Chat.COLMN_CHAT_USER)).compareTo(mMyPhone) == 0;
        else type = cursor.getInt(cursor.getColumnIndex(Chat.COLMN_FROM_ME)) > 0;
        if (type) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = null;
        int itemViewType = getItemViewType(cursor);
        if (v == null) {
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (itemViewType == 0) {
                v = mInflater.inflate(R.layout.row_chat_outgoing, parent, false);
            } else {
                v = mInflater.inflate(R.layout.row_chat_incoming, parent, false);
            }
        }
        return v;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = new ViewHolder();
        View v = view;
        final Chat chat = new Chat(cursor);
        boolean fromMe = mIsGroupChat ? chat.getUser().compareTo(mMyPhone) == 0 : chat.isFrom_me();

        if (fromMe) {

            // LOGGED IN USER'S DATA SETTING....
            holder.chat_name = (StyleableTextView) v
                    .findViewById(R.id.chat_user_name);

            holder.chat_time = (StyleableTextView) v
                    .findViewById(R.id.chat_time);

            holder.chat_tag = (StyleableTextView) v
                    .findViewById(R.id.chat_tag);

            int color = Color.parseColor("#FFFFFF");
            v.setBackgroundColor(color);

            holder.chat_name.setText("#You");
            holder.chat_time.setText(AppUtil.getEventTime(chat.getTimestampLong()));

            // header text setting and process..
            holder.chat_header_text = (TextView) v.findViewById(R.id.header_text);


            String str_date = AppUtil.covertToDate(chat.getTimestampLong());
            String pref_date = SharePreferencesUtil.getSharedPreferencesString(mContext, Constants.CHAT_TIMESTAMP, "");

            if (!str_date.equalsIgnoreCase(pref_date)) {
                holder.chat_header_text.setVisibility(View.VISIBLE);
                SharePreferencesUtil.putSharedPreferencesString(mContext, Constants.CHAT_TIMESTAMP, str_date);
                holder.chat_header_text.setText(str_date);
            } else {
                holder.chat_header_text.setVisibility(View.GONE);
            }



            String firstWord, theRest;
            String mystring = chat.getText();
            String arr[] = mystring.split(" ", 2);

            if (arr.length > 1) {
                firstWord = arr[0]; // the word with hash..
                theRest = arr[1];   // rest of the body..

                holder.chat_tag.setText(Html.fromHtml("<font color=\"#999999\"><b>" + firstWord + "</b></font>" + "  " + "<font color=\"#000000\">" + theRest + "</font>"));
                // holder.chat_text.setText(theRest);
                // holder.chat_text.setClickable(false);
            } else {
                String msg = arr[0]; // the word with hash..
                holder.chat_tag.setText(Html.fromHtml("<font color=\"#999999\"><b>" + msg + "</b></font>"));
                //holder.chat_text.setText("");
            }

            updateTimeTextColorAsPerStatus(holder.chat_time, chat.getStatus());
            v.setTag(holder);

        } else {

            // OTHER USER'S DATA SETTING....

            holder.chat_name = (StyleableTextView) v
                    .findViewById(R.id.chat_user_name);

            holder.chat_time = (StyleableTextView) v
                    .findViewById(R.id.chat_time);


            holder.chat_tag = (StyleableTextView) v
                    .findViewById(R.id.chat_tag);


            holder.chat_image = (ImageView) v
                    .findViewById(R.id.chat_profile_image);

            String image = cursor.getString(cursor.getColumnIndex("image"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String color = cursor.getString(cursor.getColumnIndex("color"));

            // set the values...
            if (holder.chat_image != null) {
                MImageLoader.displayImage(context, image, holder.chat_image, R.drawable.round_user_place_holder);
            }

            int back_color = Color.parseColor("#FFFFFF");
            v.setBackgroundColor(back_color);

            holder.chat_name.setText(name);
            holder.chat_time.setText(AppUtil.getEventTime(chat.getTimestampLong()));

            // header text setting and process..
            holder.chat_header_text = (TextView) v.findViewById(R.id.header_text);

            String str_date = AppUtil.covertToDate(chat.getTimestampLong());
            String pref_date = SharePreferencesUtil.getSharedPreferencesString(mContext, Constants.CHAT_TIMESTAMP, "");
            Log.d("eywa", "str date is ::::: " + str_date + "  pref date is :::::: " + pref_date);
            /*if (!TextUtils.isEmpty(pref_date)) {
                if (!pref_date.contains(str_date)) {
                    holder.chat_header_text.setVisibility(View.VISIBLE);
                    SharePreferencesUtil.putSharedPreferencesString(mContext, Constants.CHAT_TIMESTAMP, pref_date + str_date);
                    holder.chat_header_text.setText(str_date);
                } else {
                    holder.chat_header_text.setVisibility(View.GONE);
                }
            } else {
                holder.chat_header_text.setVisibility(View.VISIBLE);
                SharePreferencesUtil.putSharedPreferencesString(mContext, Constants.CHAT_TIMESTAMP, pref_date + str_date);
                holder.chat_header_text.setText(str_date);

            }*/
            if (!str_date.equalsIgnoreCase(pref_date)) {
                holder.chat_header_text.setVisibility(View.VISIBLE);
                SharePreferencesUtil.putSharedPreferencesString(mContext, Constants.CHAT_TIMESTAMP, str_date);
                holder.chat_header_text.setText(str_date);
            } else {
                holder.chat_header_text.setVisibility(View.GONE);
            }


            String firstWord, theRest;
            String mystring = chat.getText();

            String arr[] = mystring.split(" ", 2);
            if (arr.length > 1) {
                firstWord = arr[0]; // the word with hash..
                theRest = arr[1];   // rest of the body..

                holder.chat_tag.setText(Html.fromHtml("<font color=\"#999999\"><b>" + firstWord + "</b></font>" + "  " + "<font color=\"#000000\">" + theRest + "</font>"));
                // holder.chat_text.setClickable(false);
            } else {
                String msg = arr[0]; // the word with hash..
                holder.chat_tag.setText(Html.fromHtml("<font color=\"#999999\"><b>" + msg + "</b></font>"));
                // holder.chat_text.setText("");
            }

            String phone = cursor.getString(cursor.getColumnIndex("user"));
            final Contact contact = new Contact(name, phone, "", color, image);

            if (holder.chat_image != null) {
                holder.chat_image.setTag(contact);

//            holder.chat_name.setTag(contact);
                holder.chat_image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Contact con = (Contact) v.getTag();
                        Intent intent = new Intent(mContext, OtherProfileActivity.class);
                        intent.putExtra(Constants.EXTRA_CONTACT, con);
                        mContext.startActivity(intent);
                    }
                });
            }

            v.setTag(holder);
        }


        /*else
        {
            view=
        }*/
    }


    private void updateTimeTextColorAsPerStatus(TextView chat_time, int status) {
        if (status == 0) chat_time.setVisibility(View.INVISIBLE);
        else {
            chat_time.setVisibility(View.VISIBLE);
           /* if (status == 1)
                chat_time.setTextColor(mContext.getResources().getColor(android.R.color.white));*/
            if (status == 2)
                chat_time.setTextColor(mContext.getResources().getColor(android.R.color.darker_gray));
            else if (status == 3)
                chat_time.setTextColor(mContext.getResources().getColor(android.R.color.black));
        }
    }


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

    public class ViewHolder {
        public StyleableTextView chat_name;
        public StyleableTextView chat_time;
        public StyleableTextView chat_tag;
        public ImageView chat_image;
        public TextView chat_header_text;
    }


    @Override
    public int getCount() {

        if (getCursor() == null) {
            return 0;
        } else {
            return getCursor().getCount();
        }
    }
}

0 个答案:

没有答案