【发布时间】:2021-10-04 00:56:03
【问题描述】:
我正在尝试为片段中的 ListView 使用数组适配器,但由于某种原因,无论何时我运行它都不会显示。我不确定我哪里出错了。对此真的很陌生,所以可能有很大的错误空间。
这是片段的代码:
public class FirstFragment extends Fragment {
ArrayList<String> arrayList=new ArrayList<>();
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
View firstLayOut = inflater.inflate(R.layout.fragment_first, container, false);
ListView listView=firstLayOut.findViewById(R.id.listview);
arrayList.add("apple");
arrayList.add("banana");
arrayList.add("cat");
arrayList.add("dog");
arrayList.add("egg");
arrayList.add("f");
arrayList.add("g");
arrayList.add("h");
arrayList.add("k");
arrayList.add("l");
arrayList.add("m");
ArrayAdapter adapter1=new ArrayAdapter(firstLayOut.getContext(),android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(adapter1);
return firstLayOut;
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
这是 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/BackgroundLight"
tools:context=".MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="fill_parent">
</ListView>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/BackgroundGray"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To frag2"
app:backgroundTint="@color/buttonBlueL" />
</androidx.appcompat.widget.Toolbar>
</RelativeLayout>
以及主要活动的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
和xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/BackgroundLight"
tools:context=".MainActivity">
<include
layout="@layout/fragment_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
请让我知道我做错了什么。
【问题讨论】:
标签: java android listview android-fragments android-arrayadapter