GridView parameters - android

I've got a Gridview, which gets populated with data from internet. One of the parameters is an id of an element - my goal is to store this id somehow in order to use it in onItemClickListener method.
At first in the activity I pull data with custom components, afterwards I use it adapter's method:
public View getView(int position, View convertView, ViewGroup parent)
Where and how should I keep relation between the grid's elements and actual data ids, so when I click on an grid element, for instance, activity with appropriate id parameter gets launched.
p.s. ID isn't showed in the gridview.

Make a custom object (class) and store all values inside the ArrayList, pass it to the custom adapter to display data inside the getView() method.
And on click method, you can easily have that clicked position object from the ArrayList.

Related

Show 2 object data in single row in listview in Android

I have a requirement as shown in this image.
I have 2 show 2 object data on single row. Normally we show these information as one object in one row. How could we achieve it.
You can use,
GridView
RecyclerView with GridLayoutManager
You can achieve it customizing getView() method in listview adapter.
what getView() method do?
getView() normally generates view for each row in a general sense.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//inside this we populate data using position value
}
Here you need to show two object (sequence or random) data in a single row. You need to use another int variable to keep track about object size.
initialize it on top, int i = 0;
inside getView() you need to do something extra because you need to populate 2 object data on a single getView() method call.
Create 2 object and populate data using i.
increment i value after every object creation.
Put an if condition do check i value is less than your array size. Its because getView() method will call based on your array size and you need to populate data half / less of that.
****Populate data inside above if condition.
*Better you try Gridview for this type of needs.
*Custom layout GridView example
You need ListView xml, ListItem xml, ListItem class (object), ArrayAdaper class
Set a row view and items in that view in ListItem xml.
Create ListItem class (object) with variables (Such as String, int) you needed.
Bind the view and item in ArrayAdapter class.

ExpandableListView forgets child changed textView

I have an expandable list view with checkboxes. When i click a child, an alertdialog and i choose the quantity and then the textView of a child changes. BUT when i scroll down the list and this child disappers from view , the list forget changed textview and set the old one. What's the reason?
ListView (and its descendent ExpandableListView) does NOT create and store the views forever; instead it creates them on-the-fly as needed.
Imagine a scenario where you have a ListView with a list containing 1000 items; but the views for only any 5 items can be visible on the screen at a given time. Do you think that ListView would create and maintain 1000 different views on the screen? That would be a waste of memory, and might cause the UI to lag.
Instead, ListView internally calls getView() function to obtain the view for each item and shows it on the screen. It does this every time the item is brought into the screen display, and only for those many number of items which can fit into the screen at a given time (ListView handles these things internally so you do not have to worry about this)
All you need to do is set the text in the corresponding list item, and use this text to populate the textview in getView(). Maintain a text String and create some form of getText() and setText(String) methods in whatever Object type you are using as an Item.
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
......//initialize text view for this position
Item item = getItem(position);
textView.setText(item.getText());
.......
}
Once you set the text in the list item via alertDialog, Call notifyDataSetChanged() on the adapter to indicate that getView needs to be called again for all the views currently in display.
In your Listener, pass a reference to the adapter of the ListView. When you set the quantity in the alertDialog, just use
{
......
adapter.getItem(position).setText(quantityText);
adapter.notifyDataSetChanged();
......
}
The first line sets the text in the item; the second line tells the adapter that the information in the items have changed and it needs to create the views again.

Get child view inside a Listview Adapter

Inside my getView method I want to get a specific child view. I know that my adapter holds several items, by doing calling getCount:
getCount();
According to the JavaDoc:
public abstract int getCount ()
Added in API level 1
How many items are in the data set represented by this Adapter.
Now in my getView method:
public View getView(final int position, View convertView, ViewGroup parent) {
View lastAddedItem = parent.getChildAt(getCount()-1);
if(lastAddedItem == null) {
Log.w("ListAdapter", "View is null");
}
}
This message always appears in the LogCat, which means that the View I try to grab is Null.
I've also tried this:
View lastAddedItem = parent.getChildAt(parent.getChildCount()-1);
So how can I grab a specific view from my ViewGroup object?
I know that my adapter holds several items, by doing calling
getCount:
Yes it does, but the getCount()(returning the items in the Adapter) method of an adapter has nothing to do with getting the children of a ListView with getChildAt()(where you should use getChildCount() as this will return the Views present in the ViewGroup).
lastAddedItem = parent.getChildAt(parent.getChildCount()-1);
This should return a non-null view, the last view returned by the getView() method.
I just want to animate a specific View in my ViewGroup
If you are going to animate a list row(or part of that row) when it becomes visible than register a scroll listener for the ListView and start the animation from there when the target view is appearing on the screen.
I don't how are you implemented the rest of the adapter, but if you are doing it in the right way, and I am right, the parent only contains the Views being displayed on the device's screen, and each time you pull down or pull up the list, it modifies the content of those views, that is why you get Null.
What you want to achieve (animate a view), should be done in runtime, checking if the view currently being displayed in the screen should be animated or not.
Hope it helps.

Android List Activity-Each row with different layout

I would like to create List Activity in Android where each row will contain different layout. For e.g. first row contains an edit text and a text view. Second row contains a button. Third row contains only an image.Thus, each row element as different layout.Please help me with this.
You can create custom adapter, and use a array of layout ids.
In adapter getView method:
public View getView(final int position, View convertView, ViewGroup parent)
you can inflate different layout for different position value.
Implement a custom ListView. In the getView() method, depending on the data being processed, inflate a different layout as required.
If you are using the convertView, I guess you might also want to set a tag to the views being returned from getView() to mark what type of layout it is using. When reusing, you can check the tag, if its not the required type, then inflate a new one, etc.

How could I go about using two ListViews with a BaseAdapter?

Basically, I want to be able to use two ListViews through my BaseAdapter class in the same activity. The ListViews will be displayed side-by-side and each item within the ListView will contain multiple views (ImageView, TextView, etc.).
My main issue is retreiving/displaying data through the getView() method inherited from BaseAdapter. How would I go about detecting which ListView is being updated through this method so I will know which code to call/update? I've tried looking at the ViewGroup parameter in getView() hoping that it led me to the parent of the ListItem but the id it returns was different from my ListView's resource ID...in fact it wasn't even in my R.java file at all:
12-14 04:44:58.613: ERROR/ParentFromGetView(312): 16908298
12-14 04:44:58.623: ERROR/MyListViewId(312): 2131165191
I was hoping to do something like so:
public View getView(int position, View convertView, ViewGroup parent) {
if (parent.getId() == R.id.ListView1) {
//Do stuff
} else if (parent.getId() == R.id.ListView2) {
//Do different stuff
}
}
...but the Ids are drastically off as seen above.
Thanks in advance!
You cannot share a ListAdapter between two ListViews.
Well, I would like for them both to run on the same activity.
So?
Currently I'm extending my activity as a ListActivity and I set my adapter accordingly.
That only affects one of the two ListViews, whichever one has the #android:id/list value. You need to create a second ListAdapter for use with the second ListView. You get the second ListView by calling findViewById(), and you associate the adapter with that ListView via setAdapter().

Categories

Resources