Populating data on gridview - android

In my android app, i have a particular scenario, for one of the screens.
I require 2 button,one on each side of the corner(left and right).
Below this i want to populate data in a control.
If left button is clicked, the control should be a gridview.
If right button is clicked , the control should be a listview.
And accordingly the data should be populated.
How should i approach this scenario.
Should i create controls dynamically, or use xml instead
Rgds

Create the view with two different layouts.
Assume that you have 2 xml layouts named gridLayout.xml and listLayout.xml
and that somehow mode is determined earlier in your code and set to one of two constants GRIDVIEW or LISTVIEW. Than you can use a code fragment like:
private Context m_Context = activity.getBaseContext();
private ViewHolder m_Inflater = LayoutInflater.from(m_Context);
...
if (mode == GRIDTYPE)
viewDisplay = m_Inflater.inflate(R.layout.gridLayout, null);
} else {
viewDisplay = m_Inflater.inflate(R.layout.listLayout, null);

Related

How do I initialize elements of a GridView on creation?

My android is very rusty, so this is the best way I can explain this:
A card contains an image, a value, and a URL
I have an array of values, a parallel array of images, and of URLs (values[i] <-> images[i] <-> URLs[i])
Have a GridView that I want to use to display many of these cards
The problem:
I have a class that extends BaseAdapter to create a custom view to display the three elements of the card
Using the getView method of said adapter, I use the "i" expected by getView as a mental index of which card we are talking about.
Unfortunately I realized that i=0 means the currently visible first card, I thought it meant the overall first card. This makes it useless as a system to keep track of the overall position of cards.
So, the visible elements are populated correctly in the view. But, if I scroll down and then back up, some internal elements have been jumbled up. So clicking a card might now lead to the URL of a card that was initialized after it.
What I need help with:
A better way to index or populate each card's content that will be permanent.
I am wildly confident I am doing this in a horrendous way. I'm imagining there must be some way to say that:
When GridView is created -> populate each card's details and fill in GridView.
Current Main Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_list);
gv = (GridView) findViewById(R.id.cardGridView);
gv.setAdapter(new CardView(this, cardURLs, cardNames, cardPrices, cardImages));
}
Current CardView Activity:
public CardView(CardListActivity mainActivity, String[] cardURLs, String[] cardNames, Double[] cardPrices, int[] cardImages){
//...
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public class Holder{
TextView priceTV;
ImageView cardIV;
String cardName;
}
#Override
public View getView(final int i, View convertView, ViewGroup parent) {
//...
View rowView;
rowView = inflater.inflate(R.layout.card_item_view, null);
//HERE IS WHERE I SET THE PRICE AND IMAGE USING i
holder.priceTV.setText("$" + prices[i].toString());
holder.cardIV.setImageResource(images[i]);
//...
return rowView;
}
Turns out the problem was something else.
The actual problem ended up being caused by these Dialogs I would create to verify if the user wanted to open the website.
I was creating them inside getView, all in the same variable, which meant that the last elelemnt to get initialized would be the one used in the dialog.
I fixed this by moving the dialog creation into the onClick for the view.
Firstly, You should wrap your contents into objects so that each CardContent object contains a url, an image and a value, Then pass those into your adapter. That will be much easier on you, you only need to maintain 1 List of CardContent rather than 3 individual lists and hoping the order doesn't get messed up.
Secondly, This sounds like a case for a Recyclerview. You can use a GridLayoutManager with a Recyclerview instead of a GridView so that your views get recycled and you have less overhead. Luckily the code is largely the same.
See https://developer.android.com/training/material/lists-cards.html for pretty much what you want.

Adding and Deleting between two seperate Array ListViews

I want to create a custom ListView.
Initially, the custom ListView has one array of data, but when user taps one of the list items, it's then removed from current array and added to another. If the user taps on the second array, the list item is then added back over into the first array.
Please suggest how to apply logic to do this.
Updates : I wants to use only one listview/recyclerview.
Following are screen shots..
Regarding the object switch - this is a simple transfer between lists, , just know beforehand if the insertion and removal is index based, e.g:
contacts.add(iLocation, ContactObject);
favorites.remove(iOtherLocation);
Regarding the ListView stuff, I would suggest converting to RecyclerView, let's build a general scenario:
You have a screen (Activity or Fragment) that holds one list (the implementation can be ListView or Recycler), and another screen that holds the other list.
In both of your lists you have adapters in which you implement the logic for the clicks on the objects in the lists.
The click transfers the object, either directly to the other list, OR to a temporary Object holder (because you might need it for other stuff), in which case you will need to pull that object from the other view, either way you remove it from the current one.
you switch to the other view, and refresh it.
An easy way to go -
Assuming the screens are the same, use only one Activity, holding a single RecyclerView, and handle 2 adapters, each for every list, the adapters allow you to handle the clicks easily, with an index for the object clicked, the click executes the info swap action,the Activity handles the visual swap Action.
a very general example would be:
//init everything obviously ;)
List<ContactObject> contacts;
List<ContactObject> favoritesContacts;
//the AdapteListener is an interface declared inside the adapter
mContactsRecyclerAdapter = new ContactsRecyclerAdapter(this, contacts,new ContactsRecyclerAdapter.AdapterListener()
{
#Override
public void cellClicked(int iIndex, ContactObject object)
{
favoritesContacts.add(iIndex, ContactObject);
contacts.remove(iIndex);
mContactsRecyclerAdapter.notifyDataSetChanged();
mFavoritesRecyclerAdapter.notifyDataSetChanged();
mRecyclerView.swapAdapter(mFavoritesRecyclerAdapter, false);
}
});
And vice-versa for the other adapter.
Hope this helps, comment if you have problems and I'll update.
Please implements with custom view extend with Linearlayout
Custom view has 2 child Linearlayout in which will add with this custom view
First time add all the element in first Linearlayout and based on user action please remove from first Linearlayout and add it in another layout

Inflating Android list subviews -> duplicate IDs

I have some content in an Android app which is shown in a list. Each list entry has similar fields - let's say a picture, some text, and a text box. However, some list entries are different than others. The order of the content is based on the result of a server call.
The list itself needs to be fairly dynamic, and I'm currently using a linearlayout rather than a listview for a few reasons. My code looks something like this:
LinearLayout list = findViewById(android.R.id.list);
while (more content to add) {
switch (content type) {
case A:
View v = layoutInflater.inflate(R.layout.list_item_type_a, list, false);
EditText editText = (EditText)v.findViewById(android.R.id.edit);
// Do stuff with editText
list.addView(v);
break;
case B:
View v = layoutInflater.inflate(R.layout.list_item_type_b, list, false);
....
}
}
This works great. Except - when I put this in a fragment, and rotate the screen, now my app crashes because I have multiple fields with the same android.R.id.edit identifier.
I had thought this was a fairly elegant solution and the Android gods seem to disagree. Do I need to rip out the ID for all of my xml sublayouts? If I go this route, how should I grab references to the content?
So obviously, ListView or RecyclerView would be preferable to use here, but since you've stated you have reasons not to, I'd suggest that you disable automatic state saving for each of the views.
You can just call editText.setSaveEnabled(false), which will fix the issue, but have the side effect of not automatically retaining the view's state (e.g. input data will be lost). If you're maintaining this data yourself and restoring it on configuration changes or state restoration, this should be a totally workable solution.
I believe you could also just call setSaveFromParentEnabled(false) on the containing LinearLayout (although I haven't used that flag myself), which should disable state saving for any view in the sub-hierarchy. Same caveat applies.
I would suggest using a ListView instead of the LinearLayout and creating a custom adapter to fill the ListView. You could still have the list_item layouts that you have and then add them to the list in the newView method of your adapter. Pass your content type through a method, say getItemViewType(). Something like this:
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Choose the layout type
int contentType = getItemViewType();
int layoutId = -1;
switch (contentType) {
case A: {
View v = layoutInflater.inflate(R.layout.list_item_type_a, list, false);
EditText editText = (EditText)v.findViewById(android.R.id.edit);
// Do stuff with editText
list.addView(v);
break;
}
case B: {
View v = layoutInflater.inflate(R.layout.list_item_type_b, list, false);
....
}
}
I think this problem occur due to device screen rotation
When the phone rotates and the screen changes orientation, Android usually destroys your application’s existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.
The most important aspect of handling orientation changes is saving state. In most cases this involves implementing the onSaveInstanceState method (this could be in your Activity, Fragment or both) and placing the values you need to save in the Bundle argument that gets passed to the method.
For more details and code examples...
Please read this article
If you have a dynamic list, you should be using a ListView or RecyclerView.

Android ListView/Recyclerview dynamic content

Hello I am iOS developer just started with android, I know basic concepts of android like RecyclerView, ListView.
I want to make a feed page in which each row can contain one photo and multiple comments, no of comments are dynamically changing depending on data.
How to achieve the same using RecyclerView/ListView?
I can achieve the same in iOS like this : http://t.co/z1IRHTTjED
If you already know concepts of ListView and RecyclerView you should know that we use Adapters to provide views for List.
You can create a custom Adapter which will create a view, where, depends on data you have, you will display comments or not.
Edit.
For example you have a layout like this
<LinearLayout>
<ImageView />
<TextView android:id="#+id/comments" />
</LinearLayout>
in your adapter you should call
TextView comments = (TextView) findViewById(R.id.comments);
if(haveComments) {
comments.setVisibility(View.VISIBLE);
//display comments
} else {
comments.setVisibility(View.GONE);
}
If you need to have more complex layout for comments, for example with images, and date field, instead of TextView declare LinearLayout with vertical orientation. You can fill it with items from code.
LinearLayout commentsLayout = (LinearLayout) findViewById(R.id.commentsLayout);
if(haveComments) {
comments.setVisibility(View.VISIBLE);
//display comments
commentsLayout.addView(inflateCommentView());
} else {
comments.setVisibility(View.GONE);
}
private View inflateCommentView() {
View myComment = LayoutInflater.from(context).inflate(R.layout.my_complex_comment, null);
//set listeners, fill with data
return myComment;
}

click dynamically inflated views using robotium

In my application, I have inflated a view multiple times dynamically as the data comes from the server. For Example, I am inflating a linearLayout multiple times. Now I am trying to test the click on these dynamic views with robotium. The problem is that the layouts that are added dynamically have same id and these are added multiple times. How can I click each of the dynamically inflated linearlayouts.
Here is a static function that does what you want (i think, i do not have the android sdk or anything on my current machine so cannot test). I would not use it as a static function myself, I would put it somewhere that makes sense, but you can do with it what you want!
public static List<View> getViewsById(Solo solo, int id) {
List<View> allViews = getViews();
List<View> matchedViews = new ArrayList<View>();
for(View view : allViews){
if(view!=null && view.getId() == id){
matchedViews.add(view);
}
}
return matchedViews;
}
Use solo.getView(view.class,index).performClick(); to get the view in the specific index and click on it.
It can be done by several ways. One simple solution is when you inflate a view, apply a click listener at that time. Like if you are using some loop, in each iteration, add click listener there in loop.

Categories

Resources