英文:
Recycler View on Fragment Activity
问题
我在代码中让50条数据显示出来,但是我把它放在一个片段(Fragment)中的可循环视图(RecyclerView)中,因为我有一个下拉菜单,但是当我添加调用所有内容的代码时,我遇到了这两行错误:
package com.example.tallerof.ui.gallery;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.tallerof.R;
import com.example.tallerof.adapters.UsuariosAdapter;
import java.util.ArrayList;
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
ArrayList<String> ListadeDatos;
RecyclerView recyclerView;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
recyclerView = (RecyclerView) findViewById(R.id.Recycler1); /* 错误 #1 无法解析方法 findViewById 在 GalleryFragment 中 */
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false)); /* 错误 #2 需要类型: context 提供: GalleryFragment */
ListadeDatos = new ArrayList<String>();
for (int i = 0; i <= 50; i++) {
ListadeDatos.add("Dato # " + i + "");
}
UsuariosAdapter adapter = new UsuariosAdapter(ListadeDatos);
recyclerView.setAdapter(adapter);
return null;
}
}
注意:由于您要求只返回翻译的代码部分,因此我已经删除了您原始代码中的错误注释。如果需要错误注释,请在您的代码中添加。
英文:
I am making 50 data appear in my code, but I am putting it in a recycler view in a fragment because I have a dropdown menu, but when I put the code to call everything I get these two lines of error:
package com.example.tallerof.ui.gallery;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.tallerof.R;
import com.example.tallerof.adapters.UsuariosAdapter;
import java.util.ArrayList;
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
ArrayList<String> ListadeDatos;
RecyclerView recyclerView;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
recyclerView=(RecyclerView) findViewById(R.id.Recycler1); /* Error #1 Cannot resolve method findViewById in GalleryFragment*/
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); /* Error #2 required type: context provide: Gallery Fragment*/
ListadeDatos=new ArrayList<String>();
for(int i=0; i<=50; i++){
ListadeDatos.add("Dato # "+i+"");
}
UsuariosAdapter adapter= new UsuariosAdapter(ListadeDatos);
recyclerView.setAdapter(adapter);
return null;
}
}
答案1
得分: 0
你必须这样做。
recyclerView = getActivity().findViewById(R.id.Recycler1);
谢谢。
英文:
you must to do this.
recyclerView = getActivity().findViewbyId(R.id.Recycler1);
Regards.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论