Программное добавление представлений через адаптер массива

Я показываю список с помощью адаптера массива. для каждого элемента списка я надуваю один и тот же файл xml. Я могу отображать список в порядке. Но теперь у меня есть другое требование. Я хочу программно добавить еще несколько представлений в тот же XML-файл, который я раздуваю для каждого элемента списка. но когда я пытаюсь это сделать, я не получаю никаких ошибок. но каким-то образом вид, который я добавил программно, не отображается... может ли кто-нибудь помочь мне. ниже приведен код макета xml и адаптера массива

<?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>

This is the code for 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;
}

}


person vivek    schedule 11.08.2012    source источник


Ответы (1)


Это выглядит правильно для меня. Возможно вставка удалась, но места на экране не занимает? Можете ли вы убедиться, что заполнитель занимает часть видимого пространства на экране, установив для него ярко-розовый цвет фона или что-то в этом роде?

person Nick    schedule 11.08.2012
comment
я добавил еще один linearlayout как прямой дочерний элемент для заполнителя (linearlayout). и я присвоил ему вес макета 0,3, а затем добавил к нему текстовое представление, но оно все равно не отображается :(:( - person vivek; 11.08.2012
comment
Я думаю, проблема в вашем макете xml. пока не уверен, в чем именно проблема. - person Nick; 11.08.2012
comment
я также проверил в инструменте просмотра иерархии ... текстового просмотра тоже нет. поэтому я думаю, что самого встраивания не произошло. но почему ? - person vivek; 11.08.2012
comment
Это слишком долго, но попробуйте добавить rowview.invalidate прямо перед возвратом rowview. - person Nick; 11.08.2012
comment
Кроме того, учитывая, что размеры добавленного элемента соответствуют родительскому элементу, я думаю, что как только он начнет работать, он затенит все остальное. - person Nick; 11.08.2012
comment
ранее я поместил размеры этих добавленных элементов в wrapcontent ... но я изменил их на matchparent просто для эксперимента. и, кстати, rowview.invalidate не работает. - person vivek; 11.08.2012