如何在另一个类中调用recyclerview适配器?

时间:2018-12-23 19:08:19

标签: android android-recyclerview recycler-adapter

我想在另一堂课中更新我的RecyclerView列表。我无法在第二个类中调用RecyclerView适配器,实际上,在那里对适配器进行了初始化时,显示错误“ classXXX不是封闭的类”。我怎样才能做到这一点?

这是我的Fragment,其中包含名为RecyclerView的{​​{1}}:

AllworksFragment

这是package ir.applinkfinder.hw6; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.util.List; import ir.applinkfinder.hw6.model.WorksModel; import ir.applinkfinder.hw6.model.WorksRepository; /** * A simple {@link Fragment} subclass. */ public class AllworksFragment extends Fragment { private RecyclerView mRecyclerView; private MyAdapter myAdapter; private Button mButtonInsert; private Button mButtonRemove; private EditText mEditTextInsert; private EditText mEditTextRemove; private FloatingActionButton mFloatingActionButtonAdd; private Button mButtonAdd; public AllworksFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_allworks, container, false); mRecyclerView = view.findViewById(R.id.recyclerview); mFloatingActionButtonAdd = view.findViewById(R.id.floatingActionButton_add); mButtonAdd = view.findViewById(R.id.button_test_addd); mButtonAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = AddItemToListActivity.newIntent(getActivity()); startActivity(intent); } }); mFloatingActionButtonAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Intent intent = AddItemToListActivity.; Intent intent = AddItemToListActivity.newIntent(getActivity()); startActivity(intent); } }); myRecyclerViewLayoutManager(); updateUI(); return view; } private void myRecyclerViewLayoutManager(){ mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); // moshakhas mikone recyclerview chejuri namayesh dade beshe // mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3)); // LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()); // linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL) ; // mRecyclerView.setLayoutManager(linearLayoutManager); }//myRecyclerViewLayoutManager(); private void updateUI(){ WorksRepository worksRepository = WorksRepository.getInstance(); List<WorksModel> works = worksRepository.getmWorksList(); if (myAdapter == null) { myAdapter = new MyAdapter(works); mRecyclerView.setAdapter(myAdapter); } else{ // workAdapter.setCrimeList(works); // myAdapter.setWorkList(works); /* notifyDataSetChanged() method should be called after you've updated the list which holds all your data, in order to refresh the RecyclerView */ myAdapter.notifyDataSetChanged(); // jahate update e list bad az taqyir e edit text ha, in kole list racheck mikonad // myAdapter.notifyItemChanged(positionnnn); // notifyItemChanged faqat haman position e khas ra check mikonad } }//updateUI //ViewHolder public class myViewHolder extends RecyclerView.ViewHolder{ protected TextView mTextViewTitle; protected TextView mTextViewDetail; protected TextView mTextViewDate; protected TextView mTextViewHour; public myViewHolder(View itemView) { super(itemView); mTextViewTitle = itemView.findViewById(R.id.textview_title); mTextViewDetail = itemView.findViewById(R.id.textview_detail); mTextViewDate = itemView.findViewById(R.id.textview_date); mTextViewHour = itemView.findViewById(R.id.textview_hour); } }//ViewHolder // /////////////////////////////////////////////////////////// // CustomListListener mListener; // public interface CustomListListener{ // void onListChanged(List<WorksModel> myList); // } // public void setOnListChangeListener(CustomListListener listener){ // this.mListener = listener; // } // Adapter public class MyAdapter extends RecyclerView.Adapter<myViewHolder>{ private List<WorksModel> worksList; public MyAdapter(List<WorksModel> workM) { worksList = workM; } @Override public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_list, parent, false); return new myViewHolder(itemView); } @Override public void onBindViewHolder(myViewHolder holder, final int position) { WorksModel worksModel = worksList.get(position); holder.mTextViewTitle.setText(worksModel.getTitle()); holder.mTextViewDetail.setText(worksModel.getDetail()); // holder.mTextViewDate.setText(worksModel.getDate()); holder.mTextViewHour.setText(worksModel.getHour()); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = WorkDetailActivity.newIntent(getActivity(), worksList.get(position).getTitle()); startActivity(intent); } }); } // private void myAddItemMethod(int position, WorksRepository worksRepository){ // } @Override public int getItemCount() { return worksList.size(); } }//Adapter } ,我希望向Fragment添加一些新数据:

RecyclerView

问题在于,在 package ir.applinkfinder.hw6; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.ImageButton; import java.util.ArrayList; import java.util.Date; import ir.applinkfinder.hw6.model.WorksModel; import ir.applinkfinder.hw6.model.WorksRepository; public class AddItemToListFragment extends Fragment{ // implements WorksRepository.CustomListListener { WorksRepository mWorksRepository; private ArrayList<WorksModel> mWorksList; private RecyclerView.Adapter mAdapter; private EditText mEditTextAddTitle; private EditText mEditTextAddDetail; private EditText mEditTextAddDate; private EditText mEditTextAddHour; private ImageButton mImageButtonAddDone; public AddItemToListFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_add_item_to_list, container, false); // WorksRepository.setOnListChangeListener(this); mEditTextAddTitle = view.findViewById(R.id.edittext_add_title); mEditTextAddDetail = view.findViewById(R.id.edittext_add_details); mEditTextAddDate = view.findViewById(R.id.edittext_add_date); mEditTextAddHour = view.findViewById(R.id.edittext_add_hour); mImageButtonAddDone = view.findViewById(R.id.imagebutton_add_done); mImageButtonAddDone.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { insertItem(); } }); return view; } public void insertItem(){ mAdapter = new AllworksFragment.MyAdapter(mWorksRepository.getmWorksList()); // mAdapter = new AllworksFragment.MyAdapter(mWorksRepository); mWorksRepository = new WorksRepository(); WorksModel worksModel = new WorksModel(); worksModel.setTitle(mEditTextAddTitle.getText().toString()); worksModel.setDetail(mEditTextAddDetail.getText().toString()); // worksModel.setDate(Date); worksModel.setDate(new Date()); worksModel.setHour(mEditTextAddHour.getText().toString()); mWorksRepository.getmWorksList().add(worksModel); // mWorksList.add(worksModel); mAdapter.notifyDataSetChanged(); }//insertItem } 方法中,我无法初始化insertItem()适配器。它调用错误: “ RecyclerView不是一个封闭的片段”

0 个答案:

没有答案