如何创建listview以使用baseadapter显示发送方接收方消息?

时间:2012-03-28 05:02:43

标签: android

当我们向下滚动或向上滚动getview问题? getview没有被正确调用? 在背景图像从相应的网址加载? 发送者和接收者的布局不同..

公共类NewReadMessageAdapter扩展BaseAdapter {     私有上下文上下文;

private LayoutInflater vi;
public ImageLoader imageLoader;
private Typeface tf;

private class Image {
    String pic_url = new String();
    Bitmap thumb;
    String messege = new String();
    int id;
    String time = new String();
    String sender_name = new String();
    public boolean sender_user;

}


private Image[] images;
ArrayList<Messages> messages = new ArrayList<Messages>();

private TextView txt_msg;

private TextView txt_name;

private TextView txt_time;
Button btn_loc;
private ImageView img;

 private ArrayList<Messages> mData = new ArrayList<Messages>();
 private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
 private static final int TYPE_RECEIVER = 0;
 private static final int TYPE_SENDER = 1;
public NewReadMessageAdapter(Context context,
        ReadMessagePostParser messageParser, String sender_id,
        String reciever_id) {

    this.context = context;

    tf = Typeface.createFromAsset(context.getAssets(), "fonts/tahoma.ttf");
    messages = messageParser.getMessages();
    imageLoader = new ImageLoader(context.getApplicationContext());
    images = new Image[messages.size()];
    for (int i = 0, j = messages.size(); i < j; i++) {
        images[i] = new Image();
        images[i].messege = messages.get(i).getMessage_text();

        images[i].time = messages.get(i).getTime();
        if (messages.get(i).getMessage_from().equals("YES")) {
            images[i].sender_name = messageParser.sender_name;
            images[i].pic_url = messageParser.sender_pic_url;
            images[i].sender_user = true;
            images[i].id = Integer.parseInt(sender_id);


        } else {
            images[i].sender_user = false;
            images[i].id = Integer.parseInt(reciever_id);
            images[i].sender_name = messageParser.reciever_name;
            images[i].pic_url = messageParser.receiver_pic_url;

        }
    }

    for (int i = 0; i < images.length; i++) {
        System.out.println(images[i].pic_url + " " + images[i].sender_name
                + " " + images[i].messege);
    }
    vi = LayoutInflater.from(context);
    imageLoader = new ImageLoader(context.getApplicationContext());

}
public void addItem(final Messages item) {
    mData.add(item);
    notifyDataSetChanged();
}

public void addSeparatorItem(final Messages item) {
    mData.add(item);

    mSeparatorsSet.add(mData.size() - 1);
    notifyDataSetChanged();
}
@Override

public int getCount() {

    return mData.size();
}

@Override

public Object getItem(int position) {

    return mData.get(position);
}

@Override

public long getItemId(int position) {
    return position;
}
 @Override
 public int getItemViewType(int position) {
     return mSeparatorsSet.contains(position) ? TYPE_SENDER : TYPE_RECEIVER;
 }

 @Override
    public View getView( int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        int type = getItemViewType(position);

        if (convertView == null) {

            switch (type) {
                case TYPE_SENDER:

                    System.out.println("Sender");
                    holder = new ViewHolder();
                    convertView = vi.inflate(R.layout.list_row_layout_sender, null);
                    holder.txt_msg = (TextView) convertView
                            .findViewById(R.id.txt_msg);
                    holder.txt_name = (TextView) convertView
                            .findViewById(R.id.txt_name);
                    holder.txt_time = (TextView) convertView
                            .findViewById(R.id.txt_time);
                    holder.img = (ImageView) convertView
                            .findViewById(R.id.user_img);
                    holder.btn_loc = (Button) convertView
                            .findViewById(R.id.btn_loc);
                    convertView.setTag(holder);
                    break;
                case TYPE_RECEIVER:
                    System.out.println("Receiver");
                    convertView = vi.inflate(R.layout.list_row_layout_receiver, null);
                    holder = new ViewHolder();
                    holder.txt_msg = (TextView) convertView
                            .findViewById(R.id.txt_msg);
                    holder.txt_name = (TextView) convertView
                            .findViewById(R.id.txt_name);
                    holder.txt_time = (TextView) convertView
                            .findViewById(R.id.txt_time);
                    holder.img = (ImageView) convertView
                            .findViewById(R.id.user_img);
                    holder.btn_loc = (Button) convertView
                            .findViewById(R.id.btn_loc);
                    convertView.setTag(holder);
                    break;
            }

        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.btn_loc.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                System.out
                        .println("-------------- btn_loc ------------------- Sender");
                Developer.callIntent_userlocation(context, "18.50929796 ",
                        "73.79045371");
            }
        });
        holder.txt_name.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                System.out.println("----Name----");

            }
        });
        holder.img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                System.out.println("----Image----");

            }
        });
        holder.txt_name.setText(mData.get(position).message_from);
        holder.txt_time.setText(mData.get(position).time);
        holder.txt_msg.setText(mData.get(position).message_text);
        if (position != 0) {
            cached = images[position - 1];
            if (cached.sender_user == flag) {
                holder.img.setVisibility(ImageView.INVISIBLE);
                holder.txt_name.setVisibility(TextView.INVISIBLE);
            } else
                imageLoader.DisplayImage(images[position].pic_url.trim()
                        .replace(" ", "%20"), ReadMessageActivityNew.activity,
                        holder.img);
        } else
            imageLoader.DisplayImage(
                    images[position].pic_url.trim().replace(" ", "%20"),
                ReadMessageActivityNew.activity, holder.img);

        return convertView;
    }
static class ViewHolder {
    TextView txt_msg;
    TextView txt_name;
    TextView txt_time;
    ImageView img;
    Button btn_loc;
}

}

1 个答案:

答案 0 :(得分:0)

最好只在两个布局相同时重用convertView。由于此处的布局不同,当您重复使用发送方用于接收方的convertView时,视图可能与预期不符。