为什么我的listView不可见?

时间:2013-06-28 10:58:17

标签: java android listview adapter

我已经在我的片段和我自己的适配器中创建了一个listView,为listView创建了自己的布局,但它没有显示任何内容

我的listView布局的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="fill|right" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <TextView
            android:id="@+id/textView_listViewText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView_adressList1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/bronze_pin_level" />

        <ImageView
            android:id="@+id/imageView_adressList2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/bronze_pin_level" />

        <ImageView
            android:id="@+id/imageView_adressList3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/bronze_pin_level" />

    </LinearLayout>

</LinearLayout>

我的listView的

<?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="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listView_adressList"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2.25" >
</ListView>

<ImageView
    android:id="@+id/imageView_adressList1"
    android:layout_width="match_parent"
    android:layout_height="43dp"
    android:src="@drawable/bottom_me" />

片段代码:

    public class FragList extends Fragment {

ListView listView;
View myFragmentView;
List<String> adressList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

     myFragmentView = inflater.inflate(R.layout.frag_list, container, false);

    listView = (ListView) myFragmentView
            .findViewById(R.id.listView_adressList);


    adressList = new ArrayList<String>();

    adressList.add("Marvin");
    adressList.add("Markus");
    adressList.add("Bob");


    ArrayAdapter<String> adapter = new MyAdapter(getActivity(),
            R.layout.adress_list_layout, adressList);

    listView.setAdapter(adapter);
    return myFragmentView;

}

public class MyAdapter extends ArrayAdapter<String>{

    private List<String> values;
    ImageView btn1, btn2, btn3;
    Context cont;
    int layoutID;

    public MyAdapter(Context context, int resource, List<String> values) {
        super(context, resource);
        this.values = values;
        this.cont = context;
        this.layoutID = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) this.cont
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(this.layoutID,
                parent, false);

        TextView tv1 = (TextView) rowView
                .findViewById(R.id.textView_listViewText);
        tv1.setText(values.get(position));

        btn1 = (ImageView) rowView
                .findViewById(R.id.imageView_adressList1);

        btn2 = (ImageView) rowView
                .findViewById(R.id.imageView_adressList2);

        btn3 = (ImageView) rowView
                .findViewById(R.id.imageView_adressList3);

        return rowView;

    }

    }
}

问题是listView没有显示在屏幕上。这只是一个白色的屏幕。 显然我做的事情非常错误,但我无法弄清楚是什么。

2 个答案:

答案 0 :(得分:1)

尝试

view = View.inflate(cont,R.layout.name_of_your_listView_layout,null);

而不是

    View rowView = inflater.inflate(this.layoutID,
            parent, false);

答案 1 :(得分:1)

更改高度值,然后重试......