I have an Android activity with a RecycleView and I have implemented a ClickEvent on this RecycleView.
If I try to click on one items, I want to display a Dialog with another RecycleView.
So this is my activity code:
public class ResultActivity extends AppCompatActivity {
private List<Result> lista= new ArrayList<Result>();
private RecyclerView recyclerView;
private RecyclerView recyclerViewResult;
private ResultsAdapter pAdapter;
private ResultXResultAdapter prAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.results_activity);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
ResultDAO manager = new ResultDAO(this);
lista=manager.getResults();
pAdapter = new ResultsAdapter(lista, new ResultsAdapter.OnItemClickListener() {
#Override
public void onItemClick(Result item) {
try{
final Dialog dialog = new Dialog(ResultActivity.this);
dialog.setContentView(R.layout.result_modal);
recyclerViewResult = (RecyclerView) findViewById(R.id.recycler_result_view);
dialog.setTitle("Parametri");
prAdapter = new ResultXResultAdapter(item.getListaParametri());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerViewResult.setLayoutManager(mLayoutManager);
recyclerViewResult.setItemAnimator(new DefaultItemAnimator());
recyclerViewResult.setAdapter(prAdapter);
dialog.show();
}catch(Exception e){
Log.e("","");
}
}
});
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(pAdapter);
// prepareMovieData();
}catch(Exception e){
}
}
}
If I run my application and I try to click on one items, I have null exception. The problem is in this line code:
recyclerViewResult = (RecyclerView) findViewById(R.id.recycler_result_view);
after this code, the recyclerViewResult is null, but should not be null.
The reason your RecyclerView is returning null is because you're calling findViewById without the correct view prefix. Because you're using a custom layout in your Dialog you should use a LayoutInflater to inflate the layout then use the inflated view object to find the RecyclerView that belongs in the dialog like so:
View dialogView = inflater.inflate(R.layout.result_modal, null);
recyclerViewResult = (RecyclerView) dialogView.findViewById(R.id.recycler_result_view)
dialog.setContentView(dialogView)
Related
im trying to achieve a recycler view in my fragment but these error are not letting me to do this kindly see and tell me
here is my fragment java class
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
MyListData[] myListData = new MyListData[] {
new MyListData("Email", android.R.drawable.ic_dialog_email),
new MyListData("Info", android.R.drawable.ic_dialog_info),
new MyListData("Delete", android.R.drawable.ic_delete),
new MyListData("Dialer", android.R.drawable.ic_dialog_dialer),
new MyListData("Alert", android.R.drawable.ic_dialog_alert),
new MyListData("Map", android.R.drawable.ic_dialog_map),
new MyListData("Email", android.R.drawable.ic_dialog_email),
new MyListData("Info", android.R.drawable.ic_dialog_info),
new MyListData("Delete", android.R.drawable.ic_delete),
new MyListData("Dialer", android.R.drawable.ic_dialog_dialer),
new MyListData("Alert", android.R.drawable.ic_dialog_alert),
new MyListData("Map", android.R.drawable.ic_dialog_map),
};
RecyclerView recyclerView = root.findViewById(R.id.recyclerView);
MyListAdapter adapter = new MyListAdapter(myListData);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
return root;
}
}
i am error on these lines
RecyclerView recyclerView = root.findViewById(R.id.recyclerView)
and
recyclerView.setLayoutManager(new LinearLayoutManager(this));
here is my fragment view model class
public class HomeViewModel extends ViewModel {
private MutableLiveData<String> mText;
public HomeViewModel() {
mText = new MutableLiveData<>();
//mText.setValue();
}
public LiveData<String> getText() {
return mText;
}
}
here is my xml
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/t1"
android:scrollbars="vertical"
android:id="#+id/recyclerView"
/>
You have to use
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
instead of
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Try this,
RecyclerView recyclerView = root.findViewById(R.id.recyclerView);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
Rcv_Results.setLayoutManager(mLayoutManager);
Previous Question:
May I know why this error will occur when I try to start RecyclerView activity doing a search from fragment.
After added adapter:
error: OwnerDAO.getOwner()' on a null object reference
Fragment code:
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
SearchAdapter adapter;
OwnerDAO mOwnerDao;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.activity_search, container, false);
recyclerView = (RecyclerView)v.findViewById(R.id.recycler_search);
layoutManager = new LinearLayoutManager(this.getActivity());
mOwnerDao = new OwnerDAO(getContext())
adapter = new SearchAdapter(getContext(),mOwnerDao.getOwner());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter( adapter );
return v;
}
Search Adapter:
public SearchAdapter(Context context, List<Owner> owners) {
this.context = context;
this.owners = owners;
}
Set the layoutManager after setAdapter like this :-
recyclerView.setAdapter( adapter );
recyclerView.setLayoutManager(layoutManager);
The error will be removed !!
Recyclerview in my Fragment is giving me this error but I couldn't figure it out. There are lots of questions about this issue but none of them solved my problem so far.
Here is a piece of code from my fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
initCollapsingToolbar();
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
prepareSectors();
try {
Glide.with(this).load(R.drawable.cover).into((ImageView) view.findViewById(R.id.backdrop));
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
You did not create an instance of your adapter before setting it to RecyclerView.
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
// Your adapter initialization here
adapter = new YourAdapter(getActivity(), ....);
recyclerView.setAdapter(adapter);
UPDATE:
Get LayoutInflater from passed context. Update your SectorAdapter code portion as below:
public Context mContext;
public List<Sector> sectorList;
LayoutInflater layoutInflater;
public SectorAdapter(Context mContext, List<Sector> sectorList) {
this.mContext = mContext;
this.sectorList = sectorList;
layoutInflater = LayoutInflater.from(mContext);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.sector_card, parent, false);
return new MyViewHolder(itemView);
}
Try moving your RecyclerView codes to onViewCreated()
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
sectorList = new ArrayList<>();
prepareSectors();
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new SectorAdapter(getActivity(), sectorList);
recyclerView.setAdapter(adapter);
}
Hope this will help~
There is no any code for your adapter. Thus you need to implement the adapter and pass the instance to the recyclerView.
RecyclerView.LayoutManager mLayoutManager ....
....
// Your adapter initialization here
adapter = new someAdapter(....);
recyclerView.setAdapter(adapter);
This will help you!
I have never seen like this before. There is no error or exception the code just skipps some line.
I have a class which is doing something and when it's done returns with an Adapter and it's working fine the adapter contains the correct number and returns with it correctly. After that in my Fragment I'm trying to use this Adapter with a RecyclerView and create a list. Here is my fragment:
public class ServicesFragment extends Fragment {
ArrayList<Item> list;
public RecyclerView mRecyclerView;
public RecyclerView.LayoutManager mLayoutManager;
public ItemAdapter itemAdapter;
DownloadDataThread downloadDataThread;
public static boolean dataisready;
ProgressDialog dialog;
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_service, parent, false);
mRecyclerView = (RecyclerView)v.findViewById(R.id.services_recycler_view);
list = new ArrayList<Item>();
list.add(0,new Item());
itemAdapter = new ItemAdapter(list);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(itemAdapter);
downloadDataThread = ((MainActivity)getActivity()).downloadDataThread;
final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
Runnable helloRunnable = new Runnable() {
public void run() {
dataisready = downloadDataThread.ready;
if(dataisready){
itemAdapter = downloadDataThread.getItemAdapter();
Log.d("THREAD","adapter " + itemAdapter);
readAndLoadList(itemAdapter);
executor.shutdown();
}
Log.d("THREAD","nézem "+dataisready);
}
};
executor.scheduleAtFixedRate(helloRunnable, 0, 200, TimeUnit.MILLISECONDS);
return v;
}
private void readAndLoadList(ItemAdapter itemAdapter1){
itemAdapter = itemAdapter1;
itemAdapter.notifyDataSetChanged();
Log.d("THREAD"," adaptert22 "+itemAdapter.getItemCount());
mRecyclerView.setHasFixedSize(true);
Log.d("THREAD","muszáfá0 "+ mRecyclerView);
mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
Log.d("THREAD","muszáfá1 "+ mLayoutManager);
mRecyclerView.setLayoutManager(mLayoutManager);
// And after that the other logs never shows up
Log.d("THREAD","muszáfá2 "+ mLayoutManager);
mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
Log.d("THREAD","muszáfá3 "+ mLayoutManager);
mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
Log.d("THREAD","muszáfá4 ");
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
Log.d("THREAD","muszáfá5 ");
mRecyclerView.setAdapter(itemAdapter);
mRecyclerView.invalidate();
mRecyclerView.getAdapter().getItemCount();
Log.d("THREAD","lássuk az adaptert33 "+mRecyclerView.getAdapter().getItemCount());
}
}
So as you can see I'm checking in every 200 millisecond that the adapter is ready or not and when it's ready I'm returning with it's adapter and trying to load the RecyclerView but after that line : mRecyclerView.setLayoutManager(mLayoutManager);˛ the other logs never shows up.
Before you ask me I'm created a complete RecyclerView and load in the beginning of the onCreateView because I always got an error for RecyclerView but before I have added that it was also bad.
Have you any idea whats going on here?
I create a RecyclerView list with a default layout. I then add one new item to the list and the layout updates to show the new item. I then navigate to a previous activity. When I return to the RecyclerView activity I am returned to the generic, default list and my new item in the RecyclerView list is gone.
So how do I return to the RecyclerView and the new item that I created rather than the generic list? Do I need to add some code that says if the size of the adapter is > 0 then don't create a new list use the existing one? And should I be doing the test in the RecyclerView activity or in the adapter? If not, does my issue arise because the adapter is not related somehow to the savedInstanceState of the RecyclerView activity?
Activity:
public class ListContactsActivity extends AppCompatActivity {
private ListContactsAdapter mContactsAdapter;
private RecyclerView mRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerviewlist);
final List<Contact> mContacts;
mContacts = new ArrayList<>();
mRecyclerView = (RecyclerView) findViewById(R.id.cardList);
mRecyclerView.setLayoutManager(getLayoutManager());
mRecyclerView.setHasFixedSize(true);
mContactsAdapter = new ListContactsAdapter(this, mContacts);
mRecyclerView.setAdapter(mContactsAdapter);
...
}
private RecyclerView.LayoutManager getLayoutManager() {
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
return llm;
}
Adapter:
class ListContactsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
...
#Override
public int getItemCount() {
return mItems.size();
}
Toolbar code in Activity:
...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_action_previous_item);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
When you hit back from an activity you are popping it from the stack i.e the activity object will be destroyed. This is explained in further detail here. The crux of the concept can be understood from this picture.
You can look at this to see how you should recreate your activity - and this to see how you should apply that to a recyclerview.