我想要使用自定义ListView
在adapter
中显示的项目列表,但是如果daat包含更多值,则列表中只显示一个项目。
设置适配器的代码:
if (app.familyMemebers != null) {
Log.d("family", app.familyMemebers.size() + "");
FamilyMemeberAdapter fad = new FamilyMemeberAdapter(
getApplicationContext(), R.layout.family_row,
app.familyMemebers);
lvFamilyMemebr.setAdapter(fad);
Log.d("count >>>>", fad.getCount() + "");
}
自定义适配器的代码是:
package com.example.importtoolapp;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class FamilyMemeberAdapter extends ArrayAdapter<FamilyMemeber> {
private List<FamilyMemeber> values;
private Context context;
public FamilyMemeberAdapter(Context context, int textViewResourceId,
List<FamilyMemeber> values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
// Keeps reference to avoid future findViewById()
valuesViewHolder viewHolder;
final FamilyMemeber obj = values.get(position);
if (v == null) {
LayoutInflater li = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.family_row, parent, false);
viewHolder = new valuesViewHolder();
viewHolder.tvID = (TextView) v.findViewById(R.id.tvID);
viewHolder.tvName = (TextView) v.findViewById(R.id.tvName);
viewHolder.tvIndrc = (TextView) v.findViewById(R.id.tvIndrc);
viewHolder.tvIdnumber = (TextView) v.findViewById(R.id.tvIdnumber);
viewHolder.tvRelation = (TextView) v.findViewById(R.id.tvRelation);
viewHolder.tvBDate = (TextView) v.findViewById(R.id.tvBDate);
v.setTag(viewHolder);
} else {
viewHolder = (valuesViewHolder) v.getTag();
}
if (obj != null) {
viewHolder.tvID.setText(obj.familyid);
viewHolder.tvName.setText(obj.fname);
viewHolder.tvIndrc.setText(obj.indrc);
viewHolder.tvIdnumber.setText(obj.idnumber);
viewHolder.tvRelation.setText(obj.hofrelation);
viewHolder.tvBDate.setText(obj.bdate);
}
return v;
}
static class valuesViewHolder {
TextView tvName;
TextView tvID;
TextView tvIndrc;
TextView tvIdnumber;
TextView tvRelation;
TextView tvBDate;
}
}
答案 0 :(得分:1)
首先不要在scrollview中使用listview。这是错的。
如果必须,您应手动计算listview高度并以编程方式设置它,如下面的代码:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
但请记住,您始终可以在列表视图中添加页眉或页脚。