gpt4 book ai didi

java - 为什么我的listView不可见?

转载 作者:行者123 更新时间:2023-12-02 06:55:54 25 4
gpt4 key购买 nike

我在我的 Fragment 和我自己的适配器中创建了一个 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

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

fragment 代码:

    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没有显示在屏幕上。这只是一个白屏。显然我做错了一些事情,但我不知道是什么。

最佳答案

尝试使用

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

而不是

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

关于java - 为什么我的listView不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17363149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com