我对要在列表视图中显示的数据使用了线性布局(如下所示)。我怎样才能做到这一点?通过使用以下代码,我得到空白列表视图,不知道为什么!以下是列表视图项目
这是我正在使用的代码
public class ListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context context;
private ArrayList<Data> friendListData;
private class ViewHolder {
public TextView name;
public ImageView pro_image;
public TextView comment;
}
public ListAdapter(Context context, ArrayList<Data> friendDataList) {
// TODO Auto-generated constructor stub
super();
this.friendListData = friendDataList;
this.context = context;
}
// @Override
public int getCount() {
return friendListData.size();
}
// @Override
public Object getItem(int position) {
return position;
}
// @Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.conversation_item, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.comment = (TextView) convertView.findViewById(R.id.comment);
holder.pro_image = (ImageView) convertView.findViewById(R.id.avatarPic);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Data data = (Data) getItem(position);
holder.name.setText(data.getName());
holder.comment.setText(data.getComment());
holder.pro_image.setImageResource(data.getPro_image());
return convertView;
}
public class Data {
String name;
String comment;
int pro_image;
}
}
在 MainActivity.java 中,我正在使用这样的适配器
lv = (ListView) findViewById(R.id.ListView);
friendsDataList = new ArrayList<Data>();
adapter = new ListAdapter(MainActivity.this, friendsDataList);
lv.setAdapter(adapter);