what is parent and view in onItemClick? - java

I have a difficulty in understanding following method. In the documentation, the method description is as follows:
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
Parameters:
parent The AdapterView where the click happened.
view The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position The position of the view in the adapter.
id The row id of the item that was clicked.
I understand last two, but couldn't understand what parent does here and why view is required?
if someone have a good explanation, then please let me understand.

The AdapterView could be a ListView, GridView, Spinner, etc. This is called generics in Java. You can use parent in code to do something to the whole view. For example, if you were using a ListView you could hide the whole ListView by the following line of code:
parent.setVisibility(View.GONE);
The View refers to a specific item within the AdapterView. In a ListView it is the row. Thus, you can get a reference to a TextView within a row by saying something like this:
TextView myTextView = (TextView) view.findViewById(R.id.textView1);
String text = myTextView.getText().toString();

Related

Getting particular view from expandable listview

In my list view I have an textview in expandable group and I want to open the dialog when textview is clicked to fill the information through edittext and update textview.
Problem: how could I get the groupview textview item in my fragment oncreateview() method.
You shouldn't pass your view item form a fragment to an other. You should retrieve the object associated with your group view, pass this object to your second/edition fragment. You can use
setTargetFragment(...) and onActivityResult(...) to send the modified text from your second to your first fragment. And then you can update you list of object and refresh your expandableListView to display the updated text.
If you need help doing this please post your code.

Android getView method confusion

I am doing a BaseAdapter sub class for a custom ListView.
I would like to know what the parameter, ViewGroup parent does?
Does it refer to the list view or the parent of the XML file to be inflated ?
public View getView(int position, View convertView, ViewGroup parent)
Thank you
ViewGroup is a parent of your View.
Explanation :
When you make an xml layout you put your view into some layout(Relative,Linear etc.) That is a parent of your view. And when android call getView method he need know, what the parent of your attached View.
getView method must return a View list item . To do this, we create layout- resource R.layout.*.xml. In this method, we must of R.layout.*.xml create a View, fill it out and send data list . But before you create, we try to use convertView, which goes to the input method. It has previously created View, but unused at the moment. For example, when you scroll through the list of the items out for the screen and no longer draws the need . View of the "invisible" items used for new items . We can only fill them with data . This significantly speeds up the application , as do not need to chase inflate once again .
If convertView this time we were not given (null), then we create view.
ViewGroup is a parent of our View, that means our xml view. getView method basically for to working with our view which you would like to show in a one row. So obviously it consists your layout's parent view.
getView() method returns a view for each item (each row of ListVew)within the adapter view. The layout format and the corresponding data for an item within the adapter view is set in the getView() method.It will be a performance nightmare if getView() returns a new View every time it is called that's where the concept of Adapter came into feature.However, ListView recycles the views that are not shown any more, and gives them back through convertView.
To understand how Adapter works, you can refer the following,
http://www.edureka.in/blog/what-are-adapters-in-android/
http://www.piwai.info/android-adapter-good-practices/

Will the View returned by LayoutInflater.inflate() always also be a ViewGroup?

I'm wanting to add a LinearLayout programmatically to my screen. When I call getlayoutInflater().inflate(R.layout.whatever, null), will the returned View always be also a ViewGroup? What could it ever return that is not a ViewGroup?
The returned view depends on what is defined in the layout as the root view. It can also be a simple view like a TextView so it is not necessarily a ViewGroup.
The answer is no. You can check which are ViewGroups from here: http://developer.android.com/reference/android/view/ViewGroup.html
The basic principle is that ViewGroup can contain child Views
For example if you inflate layout which has only ImageView in it. Then that is a View.

Add single switch in list view

I need to add a switch (Android 4.0+) to my ListView , just a single switch with an ImageView Button , I have seen soo many tutorials that teach of adding several views in the list activities but what I need is just a single option of the list to have this switch . I know if I can put a button or textView I can put a switch in there , but I can't understand how is putting a button or any view there without repeating the whole same scenario on all the options , as for me , I am new to the ArrayAdapters and ListViews , but I need to learn these two so I can use them later on ! what I need is like this for example:
______________________
Image | Switch
______________________
Normal option
______________________
Button | Whatever view
_______________________
Normal option
_______________________
Checkable Option
_______________________
and so on ... So if you clearly , I want full control over every single option in there , thats what I need and want . Thanks for any help :)
if you're gonna do it by listview, then you can do it by being explicit about each position in the getView method of the adapter. here's an example:
public View getView(int position, View convertView, ViewGroup parent) {
switch (position) {
case 0:
// do image switch option
// convertView = layout 0
break;
case 3:
// do button whatever option
// convertView = layout 3
break;
case 5:
// do button whatever option
// convertView = layout 5
break;
default:
// do normal option
// convertView = layout 1
break;
}
return convertView;
}
getView is a method in many android adapters that returns a view. When hooked up to a listView, it will be recognized as the method to return a view for each one of its rows. Each row in a listview that you see is the product of one executed instance of this method. You don't call it: it is called automatically whenever rows of a listview need to appear, or whenever a row position moves off-screen and comes back, or whenever the listView is being refreshed, in general whenever listview rows must be "made". It is there for you to override so that you can customize how ever for the specific look that you want.
convertView is a view as well. It is the product of a concurrent mechanism running in the listview adapter called view-recycling. Imagine that you have a listview that needs to display 1000 rows for your game's shopping menu. a conventional approach would be that you simply make an inflate command for your in getView for a vibrant xml and edit the components to show the item differences. So, that's inflating a 1000 times (and inflating is a expensive step, mind you), plus all that has to be in memory and is just clogging up your garbage collector. Plus, what if you need to refresh something to show changes? Get outta here! There are quad-core phones now, but there is a better way.
listviews done right don't actually make 1000 views at once but only the 7 or 8 that are on the screen at a time. When a listview row view is scrolled beyond the boundaries of the phone's screen, it isn't thrown out just yet. This concurrent mechanism runs and this view is saved and held under the title of convertView and passed in as a parameter for the next getView call. Not to (re-)use it is a terrible waste, because essentially you have a view that looks like the next row in the next getView call, perhaps with only the text in a textview changed or something like that, but you're gonna do another expensive inflation anyway. Then the unused convertView knows that it's second chance has been disregarded, into the GC, and it's business as usual.
The above code doesn't make use of convertView, because frankly it wasn't asked in the question, but it's very simple to utilize.
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// inflation step
// convertView = inflation
}
// rest of code involving components of inflated view
return convertView;
}
First you check if there's nothing there, which you can imagine is when a listview is first launched and nothing has had the chance to be on deck to be thrown out. And only then do your inflation then. Next time around, getView will have it's mouth full with used view and will plug it in (if it's being returned). This is the single most beneficial action for your listview performance-wise that you can do right off the bat.
But you should know this, about half the questions tagged android on this site are on this alone, there are no guarantees that convertView will plop in the position that you want, as in right below if you're scrolling up and right above if scrolling down. Just make sure that you're being explicit about what goes at what position and you'll be fine. For more information on this and other listview know-how watch this: http://www.youtube.com/watch?v=wDBM6wVEO70.

Calling Button from different view in Android

In my app I have 3 radiobuttons on top of the view. When i choose one the 'body' underneath that changes to the XML-view i defined.
(If you want more information + pics, I asked a question about this earlier: Dynamically change view inside view in Android)
Now i want to call and change the text of the buttons and edittext's from the different views.
When I do btnAutopech = (Button) findViewById(R.id.btnAutopech); it's gives an NullPointerException.
How can I do this?
Try this.......
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll= new LinearLayout(context);
ll=(LinearLayout)layoutInflater.inflate(R.layout.gegevens_verzekeringen, ll);
btnAutopech = (Button) ll.findViewById(R.id.btnAutopech);
Thanks.........
Depends on how you solved the last question you linked to. If your inflating new views into the area you want to change then you won't be able to retreive a reference to them using finViewById. I.e. they don't actually exist.
When you re-display your view, within which you want to display text based, on your other views outcome, you would have to re-assign the text when you re-attach/display your view. You could then assign text from a member variable of the class, or perhaps from sharedPreferences or a contentProvider.
EIther way, this sort of all depends on how you've solved the issue of your original question and when you attach/remove your views.
To summerise:
It looks like your removing your other views when you visit other views, so store your data in a member variable that persists.
attach view A
got to view B
Click button or something in view B and update member variable used by view A
Go to view A (Removing view B and attaching view A)
set text on view A from member variable (updated as a result of clicking button in view B for example)

Categories

Resources