Accessing a layout from adapter - java

I am new with Android development.
My activity_main.xml file contains a Listview and wanna put a TextView each line of list view. Then I added a new layout file under layout/ folder to identify my text.
But, I couldn't access the layout and TextView while writing inflate in Adapter class.
Below the piece of Adapter class code.
View lineView;
lineView = mInflater.inflate(R.layout.line_layout, null);
TextView textView = (TextView) lineView.findViewById(R.id.namesurname);
Andy idea why getting compile error with this code?

Related

Android: Can we add recyclerview dynamically in layout

Like any other dynamic view we can add that view dynamically into layout, is it possible with recyclerview that we can add/create dynamically and attach into a viewgroup?
Thanks
RecyclerView.LayoutParams lparams = new RecyclerView.LayoutParams(
RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT);
RecyclerView recyclerView = new RecyclerView(this);
recyclerView.setLayoutParams(lparams);
rootview.addView(recyclerView);
Here rootview is linear layout or any other view group of your layout XML file after that you can add Adapter to this recycle view as usual. Here you can add any number of recycle view using rootview.addView(recyclerView);

How to remove or change Recyclerview footer?

I am using this EndlessRecyclerView.java class to add footer or load more view to the RecyclerView. I have set a footer using the setProgressView() of the EndlessRecyclerView.java i.e.
mRecyclerView.setProgressView(R.layout.recyclerview_footer_layout);
Now after a specific situation, say after a network timeout, I want to change the TextView value in footer. But I am unable to do so using the follwing code:
View progressView = inflater.inflate(R.layout.recyclerview_footer_layout, null);
progressViewText = (TextView) progressView.findViewById(R.id.loadMoreText);
What is wrong here?

Can't access to a spinner in Java

I've got an activity where there is a button which opens an AlertDialog.
My dialog works and I decided to add a layout to it which contains a spinner.
So I have 3 documents :
mainActivity.java : Its role is to open a Dialog
activity_main.xml
dialog_main.xml : The dialog's layout containing the spinner
I try to retrieve in mainActivity.java the spinner declared in dialog_main.xml (in order to add it an adapter) :
Spinner mySpinner = (Spinner)findViewById(R.id.mySpinner);
However mySpinner = NULL, I can't find mySpinner. What is the problem ?
findViewById
as a method of the Activity class finds Views in your Activity's layout. When you are showing a Dialog, it is not part of your layout, so you have to findViewById in the Dialog's layout.
You are probably inflating a View for the Dialog that you show, you cou can use this view to find your Spinnner.
It probably looks like
View view = layoutInflater.inflate (...);
then you do
view.findViewById(...);
Post the code of how how show the Dialog and I'll show you if you can't do it.

Getting view from child-layout from tablerow (Android)

I have a tablerow in a tablelayout. Now, I can get tablerows without problem. Tablerow contains a linearlayout and the linearlayout contains views that I need. Is there any possibility to get the linearlayout, and then the from it views?
I cannot do this, because View doesn't have getChildAt method:
View layout = row.getChildAt(j);
EditText first = (EditText)layout.getChildAt(0);
I cannot do this either, because it leads to runtime error when converting View to Layout:
LinearLayout layout = (LinearLayout)row.getChildAt(j);
EditText first = (EditText)layout.getChildAt(0);
If someone has better solution then post it and I'll mark it as solution.
The way I solved this was by adding ID to layout before adding it to the tablerow:
layout.setId(id);
row.addView(layout);
And then getting layout by ID:
View layout =row.getChildAt(j);
LinearLayout layoutLL = (LinearLayout) findViewById(layout.getId());
EditText first = (EditText)layout1.getChildAt(0);
I couldn't post XML-file because layout is made programmatically.

Dialog context issue - Android

I have created a dialog box like so using a custom layout:
dialog = new Dialog(FetchMenu.this, R.style.CustomDialogTheme);
dialog.setContentView(R.layout.custom_dialog_iab);
dialog.show();
I am now trying to edit a textbox within 'layout.custom_dialog_iab' for example:
TextView text = (TextView) findViewById(R.id.all_topics_unlock_button);
text.setText("Purchased");
My Question: How do I get the right context to be able to edit the textboxes?
P.S. I have tried 'dialog.getContext()' but still keep throwing null pointers?
You need to use:
TextView text = (TextView) dialog.findViewById(R.id.all_topics_unlock_button);
Note the dialog. in front of findViewById
regular findViewById() will search your activity layout rather than the dialog layout.

Categories

Resources