注意:本文代码做了两次测试,所以bean类和myadapter类不对称,但本文可提供语法格式,代码均亲测
package com.example.administrator.daer_end;
/**
* Created by Administrator on 2017/2/25.
*/
public class bean {
private String name;
// private int imagId;
private int type;
public static final int received=0;
public static final int send=1;
public bean(String name,int type) {
this.name = name;
// this.imagId = imagId;
this.type=type;
}
public String getName() {
return name;
}
// public int getImagId() {
// return imagId;
// }
public int getType(){
return type;
}
}
package com.example.administrator.daer_end;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
/**
* Created by Administrator on 2017/2/25.
*/
public class myadapter extends BaseAdapter {
private LayoutInflater mlayoutInflater;
private List<bean>list;
public myadapter(Context context, List<bean> list) {
this.mlayoutInflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHoder viewhoder=null;
if (convertView==null)
{
viewhoder=new viewHoder();
convertView=mlayoutInflater.inflate(R.layout.news,null);
viewhoder.leftLayout= (LinearLayout) convertView.findViewById(R.id.layout_left);
viewhoder.rightLayout= (LinearLayout) convertView.findViewById(R.id.layout_right);
viewhoder.right_news= (TextView) convertView.findViewById(R.id.right);
viewhoder.left_news= (TextView) convertView.findViewById(R.id.left);
// viewhoder.imageId= (ImageView) convertView.findViewById(R.id.image1);
convertView.setTag(viewhoder);
}
else {
viewhoder= (viewHoder) convertView.getTag();
}
bean go=list.get(position);
if (go.getType()==bean.received){
viewhoder.leftLayout.setVisibility(convertView.VISIBLE);
viewhoder.rightLayout.setVisibility(convertView.GONE);
viewhoder.left_news.setText(go.getName());
}
// viewhoder.imageId.setImageResource(go.getImagId());
else if (go.getType()==bean.send){
viewhoder.rightLayout.setVisibility(convertView.VISIBLE);
viewhoder.leftLayout.setVisibility(convertView.GONE);
viewhoder.right_news.setText(go.getName());
}
return convertView;
}
class viewHoder{
TextView left_news;
TextView right_news;
// ImageView imageId;
LinearLayout leftLayout;
LinearLayout rightLayout;
}
}
注意:本文代码做了两次测试,所以bean类和myadapter类不对称,但本文可提供语法格式,代码均亲测package com.example.administrator.daer_end;/** * Created by Administrator on 2017/2/25. */public class bean { private String name;//