更改位置ListView中的聊天消息?

时间:2014-10-02 15:45:59

标签: android listview android-arrayadapter android-adapter

我为我的聊天页面创建了一个自定义适配器,它在listView中为firstUser显示消息,在{}}}右侧打开一个显示消息的框,并为secondUser打开一个框左侧显示消息,但填满页面并scroll页面。改变消息的位置?? 我的代码很大但是我的Custom Adapter

public class AdapterConversation extends ArrayAdapter<StructConversation>{
    public static String co;
    public AdapterConversation(ArrayList<StructConversation> array){
        super(G.context, MClear().equals("pasokh") ? R.layout.out : R.layout.in_out_conversation , array);
        MClear();
    }
    private static class ViewHolder{
        public TextView NameTitle;
        public TextView Conver;
        public TextView dateTime;

        public ViewHolder(View view){
            NameTitle = (TextView) view.findViewById(R.id.NameTitle);
            Conver = (TextView) view.findViewById(R.id.Conver);
            dateTime = (TextView) view.findViewById(R.id.dateTime);
        }
        public void fill(ArrayAdapter<StructConversation> adapter,final StructConversation item, int position){
            NameTitle.setText(item.NameTitle);
            Conver.setText(item.Coversations);
            dateTime.setText(item.dateTime);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        StructConversation item = getItem(position);
        if(convertView == null){
            convertView = G.inflater.inflate(MClear().equals("pasokh") ? R.layout.out : R.layout.in_out_conversation, parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.fill(this, item, position);
        return convertView;
    }

    public static String MClear() {
        SharedPreferences sharedPrefs = G.context.getSharedPreferences("CONVER", 0);
        co = (sharedPrefs.getString("PSA", "not"));
        sharedPrefs.edit().remove("PSA").commit();
        return co;
    }

}

1 个答案:

答案 0 :(得分:1)

你的viewHolder看起来应该是这样的:

private static class ViewHolder{
    public TextView NameTitle;
    public TextView Conver;
    public TextView dateTime;
}

创建持有者时只设置一次成员

if(convertView == null){
        convertView = G.inflater.inflate(MClear().equals("pasokh") ? R.layout.out : R.layout.in_out_conversation, parent,false);
        holder = new ViewHolder(convertView);
        // Use findViewById HERE!!
        convertView.setTag(holder);
    }