我正在尝试从另一个类更新适配器的数据集,但没有任何帮助。当我从RecyclerView类中调用该方法时,它可以工作但不能从外部工作。
private Filters dataFilters = new Filters();
private HomeSpinnerHandler homeSpinnerHandler = new HomeSpinnerHandler(this);
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mHomeAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private static Context context;
private SwipeRefreshLayout mSwipeRefreshLayout;
private View rootView;
private String homeCompanySelection = null;
private static final String objectTypeProductString = "Product";
private static MaterialDialog dialogHome;
private FloatingActionButton fabHome;
private View viewHome;
private MaterialSpinner spinnerHomeCompany;
private static boolean filterIsCalled = false;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home_layout, null);
mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.home_refresh);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.home_recycler_view);
spinnerHomeCompany = (MaterialSpinner) rootView.findViewById(R.id.spinnerHomeCompany);
viewHome = rootView.findViewById(R.id.sheetHome);
context = container.getContext();
fabHome = (FloatingActionButton) rootView.findViewById(R.id.fabhome);
dialogHome = new MaterialDialog(context);
mRecyclerView.setHasFixedSize(false);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mHomeAdapter = new RecyclerViewHomeAdapter(dataFilters.getAllHomeDataSet());
PreCachingLayoutManager layoutManager = new PreCachingLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setSmoothScrollbarEnabled(true);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mHomeAdapter);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
updateView();
mSwipeRefreshLayout.setRefreshing(false);
}
});
homeSpinnerHandler.addItemsOnCompanySpinner();
spinnerHomeCompany.setOnItemSelectedListener(homeSpinnerHandler);
fabHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewHome.setVisibility(View.VISIBLE);
if (viewHome != null) {
ViewGroup parent = (ViewGroup) viewHome.getParent();
if (parent != null) {
parent.removeView(viewHome);
}
}
dialogHome.setView(viewHome).setPositiveButton("ΑΝΑΖΗΤΗΣΗ", new View.OnClickListener() {
@Override
public void onClick(View v) {
companyFilter();
dialogHome.dismiss();
viewHome.setVisibility(View.GONE);
}
}).setNegativeButton("ΑΚΥΡΩΣΗ", new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogHome.dismiss();
viewHome.setVisibility(View.GONE);
}
});
dialogHome.show();
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
((RecyclerViewHomeAdapter) mHomeAdapter).setOnItemClickListener(new RecyclerViewHomeAdapter.MyClickListener() {
@Override
public void onItemClick(int position, View v, int id, String objectType) {
if (objectType.contains(objectTypeProductString)) {
((MainActivity) getActivity()).onProductItemClick(id);
} else {
((MainActivity) getActivity()).onPostItemClick(id);
}
}
});
}
public boolean onBackPressed()
{
if (filterIsCalled){
Toast.makeText(context,"dsdsd",Toast.LENGTH_SHORT).show();
updateView();
filterIsCalled = false;
} else
return true;
return false;
}
public void companyFilter() {
homeSpinnerHandler.addItemsOnCompanySpinner();
mHomeAdapter = new RecyclerViewHomeAdapter(dataFilters.getFilteredHomeDataSet(homeCompanySelection));
mRecyclerView.setAdapter(mHomeAdapter);
filterIsCalled = true;
}
public void updateView() {
mHomeAdapter = new RecyclerViewHomeAdapter(dataFilters.getAllHomeDataSet());
mRecyclerView.setAdapter(mHomeAdapter);
}
public Context getContext() {
return context;
}
public void setHomeCompanySelection(String homeCompanySelection) {
this.homeCompanySelection = homeCompanySelection;
}
public MaterialSpinner getSpinnerHomeCompany() {
return spinnerHomeCompany;
}
从另一个班级我正在创建一个实例
HomeFragment homeFragment = new HomeFragment();
并调用方法,如。
homeFragment.updateView();
我也尝试过使用静态变量,但没有再次成功。
答案 0 :(得分:0)
不要将其设为静态或创建homeFragment的新实例。
尝试将mHomeAdapter传递给新类并在那里进行更新。你也不应该制作新的适配器。在适配器中创建这样的方法:
public void changeData(Data data ) {
mData = data;
notifyDataSetChanged();
}
然后调用它来更改数据。
更好的是你可以使用startActivityForResult()而不是传递你的适配器,或者你可以在HomeFragment中创建一个广播接收器来更新适配器。