通过数组适配器以编程方式添加视图

时间:2012-08-11 05:38:49

标签: android android-layout android-arrayadapter

我正在使用arrayadapter显示列表视图。对于每个listitem我膨胀相同的xml文件。我能够显示listview罚款。但现在我有另一个要求。我想以编程方式将更多视图添加到我为每个列表项充气的相同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;
}

}

1 个答案:

答案 0 :(得分:0)

这对我来说是正确的。也许嵌入是成功的,但屏幕上不占用空间?您是否可以通过将占位符设置为粉红色或其他东西来验证占位符占用屏幕上的一些可见空间?