我正在使用 arrayadapter 显示列表视图。对于每个列表项,我都会膨胀相同的 xml 文件。我能够很好地显示列表视图。但现在我有另一个要求。我想以编程方式将更多视图添加到我为每个列表项膨胀的同一个 xml 文件中。但是当我尝试这样做时,我没有收到任何错误。但不知何故,我以编程方式添加的视图没有显示出来......谁能帮助我。下面是 xml 布局和 arrayadapter 的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/listitemcolor"
android:padding="10dip">
<LinearLayout
android:layout_weight="1.0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:textColor="@android:color/black"
android:id="@+id/locationitem_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textSize="16dp"
android:textStyle="bold" />
<LinearLayout
android:layout_marginTop="5dip"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:drawableLeft="@drawable/like"
android:textColor="#008000"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Likes"
android:textSize="12dp"
android:textStyle="bold" />
<TextView
android:textColor="#008000"
android:layout_marginLeft="5dip"
android:id="@+id/number_of_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:drawableLeft="@drawable/dislike"
android:textColor="#FF0000"
android:layout_marginLeft="10dip"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dislikes"
android:textSize="12dp"
android:textStyle="bold" />
<TextView
android:textColor="#FF0000"
android:layout_marginLeft="5dip"
android:id="@+id/number_of_dislikes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
这是arrayadapter的代码:
public class LocationItemAdapter extends ArrayAdapter<LocationItem> {
private Context context;
private ArrayList<LocationItem> items;
public LocationItemAdapter(Context context,
List<LocationItem> objects) {
super(context, R.layout.locationitem_listview_element, objects);
this.context = context;
items = (ArrayList<LocationItem>) objects;
}
/* (non-Javadoc)
* @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowview = null;
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowview = inflater.inflate(R.layout.locationitem_listview_element, null);
}
else
{
rowview = convertView;
}
((TextView)rowview.findViewById(R.id.locationitem_name)).setText(items.get(position).getName());
((TextView)rowview.findViewById(R.id.number_of_likes)).setText(String.valueOf(items.get(position).getLikes()));
((TextView)rowview.findViewById(R.id.number_of_dislikes)).setText(String.valueOf(items.get(position).getDislikes()));
LayoutInflater inflater1 = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView tv = new TextView(context);
//TextView tv = (TextView)inflater1.inflate(R.layout.sample, null);
tv.setText("My name is blah");
tv.setTextColor(R.color.black);
tv.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
//tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
LinearLayout ll = (LinearLayout)rowview.findViewById(R.id.placeholder);
//((LinearLayout)rowview.findViewById(R.id.placeholder)).addView(tv);
ll.addView(tv);
rowview.setTag(items.get(position));
return rowview;
}
}