Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中显示错误值的游标适配器_Android_Android Cursoradapter - Fatal编程技术网

在android中显示错误值的游标适配器

在android中显示错误值的游标适配器,android,android-cursoradapter,Android,Android Cursoradapter,我正在尝试实现一个列表,该列表从我的ORM数据库中获取聊天记录并将其显示在列表中。聊天记录的获取和显示非常整齐,但当我打开键盘键入消息时,列表中的聊天记录值会变得混乱。我已按时间戳的升序获取聊天记录值。 这是我的适配器 public class ChatsAdapter extends CursorAdapter { private Context mContext; private LayoutInflater mInflater; private ContactIn

我正在尝试实现一个列表,该列表从我的ORM数据库中获取聊天记录并将其显示在列表中。聊天记录的获取和显示非常整齐,但当我打开键盘键入消息时,列表中的聊天记录值会变得混乱。我已按时间戳的升序获取聊天记录值。 这是我的适配器

public class ChatsAdapter extends CursorAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private ContactInfo contact;
    private Chat item;
    private Cursor c;
    public ChatsAdapter(Context context, Cursor c, boolean autoRequery, ContactInfo contact) {
        super(context, c, false);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mContext = context;
        this.contact = contact;
        this.c = c;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        View v = null;
        if (cursor.getInt(cursor.getColumnIndex(Chat.COLMN_IS_ME)) > 0) {
            v = mInflater.inflate(R.layout.incoming_message, parent, false);
            holder._name = (TextView) v
                    .findViewById(R.id.profile_name);
            holder._text = (TextView) v
                    .findViewById(R.id.text);
            holder._time = (TextView) v
                    .findViewById(R.id.time);
            String name = SharedPerefsUtil.getSharedPreferencesString(context, Constants.NAME, "");
            holder._name.setText(name);
            holder._time.setText(Util.getTime(cursor.getLong(cursor.getColumnIndex(Chat.COLMN_TIME))));
            holder._text.setText(cursor.getString(cursor.getColumnIndex(Chat.COLMN_TEXT)));
        } else {
            v = mInflater.inflate(R.layout.outgoing_msg, parent, false);
            holder._name = (TextView) v
                    .findViewById(R.id.profile_name);
            holder._text = (TextView) v
                    .findViewById(R.id.text);
            holder._time = (TextView) v
                    .findViewById(R.id.time);

            holder._name.setText(contact.getName());

            holder._text.setText(cursor.getString(cursor.getColumnIndex(Chat.COLMN_TEXT)));
            holder._time.setText(Util.getTime(cursor.getLong(cursor.getColumnIndex(Chat.COLMN_TIME))));
        }
        v.setTag(holder);
        return v;
    }

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

    }


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

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





    private int getItemViewType(Cursor cursor) {
        boolean type = cursor.getInt(cursor.getColumnIndex(Chat.COLMN_FROM_ME)) > 0;
        if (type) {
            return 0;
        } else {
            return 1;
        }
    }


    public class ViewHolder {
        public TextView _name;
        public TextView _time;
        public TextView _text;
        public ImageView _image;
    }
} 

感谢您的帮助..提前感谢..

您应该在
bindView
方法中绑定您的视图(调用cursor.getString()),而不是在'newView`@pskink中,但是每次列表向右滚动时都会调用bindView?。在bindView中设置值真的能解决我的问题吗?只有更改代码,您才会知道(在newView和bindView中添加Log.d语句,您将看到)@pskink好的…我将其设置到newView中,因为我在我的中膨胀了两个不同的行布局..这在设置值时造成了一些问题..仍将尝试您的解决方案..@pskink是..它成功了..谢谢。。