简体   繁体   中英

Why is my listView not visible?

i have created a listView in my Fragment and my own adapter to create my own layout for the listView but its not showing anything

the xml for my listView layout:

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

the xml for my 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" />

the fragment code:

    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;

    }

    }
}

The Problem is that the listView is not shown on the screen. It´s just a white screen. Apparently I am doing something quite wrong, but I cant figure out what.

try with

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

and not

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

改变高度值再试一次......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM