Java Android-如何在片段中添加ListView
我将在片段中添加ListView 这是我的activitymain.xml:Java Android-如何在片段中添加ListView,java,android,fragment,Java,Android,Fragment,我将在片段中添加ListView 这是我的activitymain.xml: <com.google.android.material.tabs.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="wrap_content" > <com.goo
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.google.android.material.tabs.TabItem
android:id="@+id/tabAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="All"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/tabTravel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Travel"/>
<com.google.android.material.tabs.TabItem
android:id="@+id/tabTech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tech"/>
</com.google.android.material.tabs.TabLayout>
当我尝试添加adapter.java类时,我收到以下错误:
“无法解析片段中的findviewbyid方法”通过检查屏幕截图,您犯了几个错误 纠正他们
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_all, container, false);
// whatever you have written after this, move to the below method.
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
// blah
ArrayList<CouponCategory> couponcategory = whatever you have written
//
ListView couponcategoryListView = view.findViewById(R.id.your_list_id);
// whatever you have written....\
// adapter initialization
// settinh up blah...
}
@覆盖
创建视图上的公共视图(LAYOUTINGER充气机,@Nullable ViewGroup container,
@可为空的捆绑包savedInstanceState){
返回充气机。充气(R.layout.fragment_all,容器,假);
//无论您在这之后写了什么,请转到下面的方法。
}
@凌驾
已创建公用void onview(@NonNull视图,@Nullable Bundle savedInstanceState){
//废话
ArrayList couponcategory=您所写的内容
//
ListView couponcategoryListView=view.findViewById(R.id.your_list_id);
//不管你写了什么\
//适配器初始化
//胡说八道。。。
}
发布你的片段类代码,可能需要编辑为“view.findViewById”嗨,我在主帖子中添加了片段类代码。你能提供链接吗?CreateView上的公共视图(LayoutFlater充气器、ViewGroup容器、Bundle savedInstanceState){//膨胀此片段的布局SetHasOptions菜单(true);返回充气器。充气(R.layout.fragment_all,container,false);}完成类代码并发布logcat完整错误谢谢您,notTdar。当然,在这里发布之前,我阅读了文档,但没有找到解决方案。我遵循了您的建议,在这一行中出现了与以前相同的错误:CouponCategoryAdapter=new CouponCategoryAdapter(这是couponcategory);其中,在“this,couponcategory);”上,Android Studio建议我将适配器上的第一个参数从Context更改为Allfragment。相信我,您还没有完成非常基本的操作,而是通过-->view.getContext()来代替这个@parameter,谢谢@notTdar。我解决了我的问题。我是新手,如果我问了这么简单的问题,请原谅。对于进进出出的基础知识,通过多个教程是非常重要的。考虑到android文档有时会让人不知所措,这就是为什么您必须查看其他教程并熟悉它的原因。我希望你能推断出要点。
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_all, container, false);
// whatever you have written after this, move to the below method.
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
// blah
ArrayList<CouponCategory> couponcategory = whatever you have written
//
ListView couponcategoryListView = view.findViewById(R.id.your_list_id);
// whatever you have written....\
// adapter initialization
// settinh up blah...
}