我有一个自定义列表视图,其中包含一些动态部分和一些预定义部分。当我填充它。静态部分出现在列表视图中的正确位置,但动态部分覆盖列表视图的!st,将布局中其余部分的布局部分留空。为什么会这样?
这是我的自定义列表视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll_list_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/txtvw_ch_item_label_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/default_name"
android:textSize="17sp" />
<TextView
android:id="@+id/txtvw_ch_item_label_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+1956xxxxxxx" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#03a3d9"
android:orientation="vertical" >
</LinearLayout>
这是代码的getView部分:
private class CustomListAdapter extends ArrayAdapter<String> {
Context context;
LayoutInflater inflater = null;
List<String> list_of_numbers = new ArrayList<String>();
public CustomListAdapter(Context ctx, int res_id, List<String> objects) {
super(ctx, res_id, objects);
context = ctx;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < (objects.size()); i++) {
list_of_numbers.add(objects.get(i));
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (vi == null) {
vi = inflater.inflate(R.layout.labels_listvw_item, null);
}
// lv_wrapper = (LinearLayout) findViewById(R.id.ll_list_items);
LinearLayout lv_wrapper = (LinearLayout) findViewById(R.id.ll_list_items);
TextView txt_number = (TextView) vi
.findViewById(R.id.txtvw_ch_item_label_number);
TextView txt_name = (TextView) vi
.findViewById(R.id.txtvw_ch_item_label_name);
if (list_of_numbers.get(position).toString() == null) {
list_of_numbers.get(position).replace(null, "");
}
List<String> list_of_other_columns = Utils.checkForOtherColumns(
context, list_of_numbers.get(position).toString());
Log.d("", "logger name=" + txt_name.getText());
for (int i = 0; i < list_of_other_columns.size(); i++) {
Log.d("", "logger posn is" + position);
// LinearLayout lv_wrapper = (LinearLayout)
// findViewById(R.id.ll_list_items);
TextView label_txt = new TextView(context);
// //
LinearLayout.LayoutParams label_text_params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// //
label_txt.setLayoutParams(label_text_params);
label_txt.setText(list_of_other_columns.get(i));
Log.d("", "logger label for" + txt_name.getText() + "="
+ label_txt.getText());
// // Log.d("", "logger lv_wrapper=" + lv_wrapper);
// lv_wrapper.addView(label_txt);+
if (lv_wrapper != null) {
lv_wrapper.addView(label_txt);
}
}
txt_number.setText(list_of_numbers.get(position).toString());
txt_name.setText(Utils.getContactName(context,
list_of_numbers.get(position).toString()));
return vi;
}
}