0

I am unsure how to get alternating rows inflated for a listview with a custom adapter in a sort of conversation style, that is user1 gets row_style1 and user2 gets row_style2 for their messages respectively. I have the data for both users in the same JSON object. I was looking for help as to how best I can get this accomplished. I am not merely trying to get alternating views, but rather for every message user1 sends it always uses row_style1 and for every message user2 has row_style2. If user1 sends 3 messages row_style1 is used for each of those, etc...

public class MsgAdapter extends ArrayAdapter<String> {
private ArrayList<String> memName,memAvatar,msgBody,msgTime;
private ArrayList<Integer>  memId;
private LayoutInflater inflater;
private ImageLoader imageLoader;



public MsgAdapter(Context context, int resId, ArrayList<String> body, ArrayList<String> avatar, ArrayList<String> name,ArrayList<Integer> memId, ArrayList<String> msgTime, LayoutInflater inflater){
    super(context,resId, body);
    this.msgBody = body;
    this.memName = name;
    this.memAvatar = avatar;
    this.msgTime = msgTime;
    this.memId = memId;
    this.inflater = inflater;
    this.imageLoader = new ImageLoader(context);
}

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

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

    if(convertView == null){

            v = inflater.inflate(R.layout.msg_thread_row_sender, parent, false);

    }else
        v = convertView;

    TextView msg         = (TextView)v.findViewById(R.id.msgBody);
    TextView dateInfo    = (TextView)v.findViewById(R.id.msgTime);
    ImageView memImg = (ImageView) v.findViewById(R.id.usrImg);
    imageLoader.DisplayImage(memAvatar.get(position), memImg);

    msg.setText(msgBody.get(position));
    dateInfo.setText(msgTime.get(position));

    return v;
}
}
4

2 回答 2

2

在您的自定义适配器中,您必须覆盖

@Override
public int getViewTypeCount() {

}

@Override
public int getItemViewType(int position) {

}

getViewTypeCount必须返回要显示的不同视图的数量(在您的情况下为 2),getItemViewType应返回视图的类型(例如 user1 为 0,user2 为 1)。这样,您 getView 将收到两个不同的 convertView ,您可以根据位置选择如何充气

于 2013-06-13T14:14:45.207 回答
0

你可以这样做:在你的 get view 方法中

View getView (int position, View convertView, ViewGroup parent){
//even row
if(position%2==0)
//inflate row_style_1
else
//odd row
//inflate row_style_2
}

希望这可以帮助

于 2013-06-13T14:28:09.563 回答