android中的自定义ListView适配器

时间:2014-08-13 09:33:41

标签: java android listview android-listview

我有一个Android应用程序,我想显示一些消息。消息分为两类,图像和文本。文本消息有两种类型,发送和接收。我已经创建了一个自定义列表视图适配器来显示消息listview。当调用适配器时,我传递了13个对象的arraylist。问题并不是arraylist中的所有消息都显示。只显示前五条消息。消息每次显示三次。当列表视图中的消息项时单击,多个消息高亮。可能是什么问题。 这是listview适配器:

public class CustomMessageListAdapter extends BaseAdapter {
private ArrayList<?> listData;

private LayoutInflater layoutInflater;

public CustomMessageListAdapter(Context context, ArrayList<?> listData) {
    this.listData = listData;
    layoutInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return listData.size();
}

@Override
public Object getItem(int position) {
    return listData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.listview_message,
                null);
        viewHolder = new ViewHolder();

        viewHolder.incoming_message = (RelativeLayout) convertView
                .findViewById(R.id.received_layout);
        viewHolder.outgoing_message = (RelativeLayout) convertView
                .findViewById(R.id.sent_layout);

        viewHolder.sent_message = (TextView) convertView
                .findViewById(R.id.sent_messages_textView_message);
        viewHolder.sent_time = (TextView) convertView
                .findViewById(R.id.sent_messages_textview_time);

        viewHolder.sent_image = (ImageView) convertView
                .findViewById(R.id.sent_image_imageview);
        viewHolder.received_image = (ImageView) convertView
                .findViewById(R.id.received_image_imageview);

        viewHolder.sent_image_layout = (RelativeLayout) convertView
                .findViewById(R.id.sent_image_layout);
        viewHolder.received_image_layout = (RelativeLayout) convertView
                .findViewById(R.id.received_image_layout);

        viewHolder.received_message = (TextView) convertView
                .findViewById(R.id.messages_textView_message);
        viewHolder.received_time = (TextView) convertView
                .findViewById(R.id.messages_textview_time);

        viewHolder.date = (TextView) convertView
                .findViewById(R.id.messages_textView_date);

        viewHolder.sent_hepasnap = (RelativeLayout) convertView
                .findViewById(R.id.chat_layout_sent_hepasnap);
        viewHolder.recieved_hepasnap = (RelativeLayout) convertView
                .findViewById(R.id.chat_layout_received_hepasnap);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    MessageItem messageItem = (MessageItem) listData.get(position);
    Log.i("msg", "" + position);
    if (messageItem.getCategory().equalsIgnoreCase("text")) {
        if (messageItem.getType().equalsIgnoreCase("sent")) {
            viewHolder.outgoing_message.setVisibility(View.VISIBLE);
            viewHolder.sent_message.setText(messageItem.getMessage());
            viewHolder.sent_time.setText(messageItem.getTime());
        } else {
            viewHolder.incoming_message.setVisibility(View.VISIBLE);
            viewHolder.received_message.setText(messageItem.getMessage());
            viewHolder.received_time.setText(messageItem.getTime());
        }
    }

    return convertView;
}

public class ViewHolder {
    TextView sent_message;
    TextView sent_time;
    TextView received_message;
    TextView received_time;
    RelativeLayout outgoing_message;
    RelativeLayout incoming_message;
    RelativeLayout sent_hepasnap;
    RelativeLayout recieved_hepasnap;
    TextView date;
    ImageView sent_image;
    ImageView received_image;
    RelativeLayout sent_image_layout;
    RelativeLayout received_image_layout;
}

}

点击消息时:

enter image description here

2 个答案:

答案 0 :(得分:0)

添加viewHolder.incoming_message.setVisibility(View.GONE);如果条件

(messageItem.getType()equalsIgnoreCase(&#34;发送&#34))

和viewHolder.outgoing_message.setVisibility(View.GONE);否则条件。

如果您的问题没有解决,请告诉我。

答案 1 :(得分:0)

我认为消息会多次显示,因为只要转换视图为非空,它们就会带有较旧的值。

由于您的传入和传出消息都被填满,因此会产生多项被选中的错觉。

因此,您应该将未使用的视图的可见性更改为View.GONE。