【发布时间】:2020-10-20 04:24:32
【问题描述】:
我正在尝试在 android 应用程序上显示一些内容,包括一个标题和一个 ArrayList 的 URL。现在我正在使用占位符数据,我想使用ListView 来显示字符串的ArrayList,但它只显示第一个元素。这是 XML 和 java 代码。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/picture_background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="48sp"
android:textStyle="bold"
android:textColor="@android:color/white"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
tools:text="Home"/>
</RelativeLayout>
<ListView
android:id="@+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
这是我要显示的,这里是java代码。
package com.zunkufteducation;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.category, container, false);
ArrayList<String> urls = new ArrayList<>();
urls.add("wrsgarsg");
urls.add("arhn");
urls.add("asrnhnet");
urls.add("awgetsh4q");
urls.add("aerfn er");
urls.add("arehbbsdva");
urls.add("aerherhgsa");
urls.add("adreasrfar");
ListView listView = (ListView) rootView.findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, urls);
listView.setAdapter(adapter);
return rootView;
}
}
我很困惑为什么它不起作用,感谢任何帮助。
谢谢你, 阿尔伯特
【问题讨论】:
标签: java android android-layout android-listview