First I have build a simple android application which had only five optios to select. For this purpose I used five Buttons on main Activity. Now I have more than twenty buttons in a ScrollView to select. What is the best way to represent this kind of application (using buttons in a ScrollView? using TabHost? or with some other widget?)
The app look like this now:
Grid View or List View or Recycler View
the Adapter automatically will add buttons with the names you want, something that I did for my upcoming app.
I made a java class called data which has 'data` for my app.
it has an array of images for my GridView.
SO:
Make a class called data
Add a public final static String[] myArray array of your names, or data
Now, whenever you want to access them, use data.myArray
If you want to access one item ,use data.myArray[itemIndex]
Don't forget, indexes are zero based, not 1
Put your button inside a viewHolder class
find the id of the button in the getView if convertView is null & set the holder as a tag
NOTE : after finding the ID of the button, just leave it don't do anything or edit the text, continue reading please.
Use that array with your custom adapter
as
gridView.setAdapter(new myCustomAdapter(parameter1, parameter2,data.myArray);
use this , I just made it yesterday, added array of buttons feature now. You can just learn it or use it or commit changes.
NOTE :
You can make an array of listeners just like any primitive data type, View.OnClickListener[] and name it, initialize it.
Use grid view. it will be easy to show multiple buttons on screen using grid view.
Related
I'm looking for an effective and reusable way of reusing lists of data in slightly different contexts.
What I have is a list like the following:
[Bitmap] PlayerName
[Bitmap] PlayerName
...
These are stored as ArrayLists that I get from Cursors and are parcelable so I can move them around as required.
The issue I have is that I want to re-use the Image and name but change info within the line.
What I mean is this:
When I show the players in the first list, I have a remove button that removes that player from the whole game.
In another list I also use a remove button but that's to remove them from a specific instance of a game.
In both these instances I've got a Removebutton with the list, I use lazy hackery when I construct the ListaAapter to choose what the button does. The listitems themselves have no listener, just the button.
I now want to reuse the player list again, but have the whole items clickable (i.e. use the OnItemClickListener or some such) and then place a number next to them (in the order they were clicked).
I imagine I could add a TextView under the Remove box and wire it into the same thing and make the TextView and the Button visible as required but that seems like a pretty bad way to manage it.
How should I separate the presentation from the data in this instance?
Potential solutions
1.
As per a suggestion on using layouts, I have a constructor like this:
public PlayerListAdapter(Context context, ArrayList<Player> players, GameInstance gameInstance) {
super(context, R.layout.row_player_empty, players);
this.context = context;
this.players = players;
this.gameInstance = gameInstance;
}
Would I add an int for layout to that constructor and then change my getView and onClick methods to use the layout to drive the different data sources?
2.
Another solution I've looked at is 'merging' layouts. Here I create a base 'player' and then for each set of additional UI elements I wrap the base into the additional elements as a new layout. This at least means I'm re-using the font/size/margins etc.
I'd probably rather overlay the elements a bit more dynamically but at least If I change the base style i'm doing it once rather than 3 times.
You can manage both list with different xml for it.
From fragment/activity, you can passs which kind of list view you are looking and on depneds on that, your xml out of 2 layout will inflate and you can set values.
In first case, you need just delete button clickable, so you can set listener on that only, in second case you need entire button clickable, so you can set onClickListner when you set that kind of list view xml.
As you have 2 cases:
in case 1, you should pass any boolean or enum kind value in constructor, so you can identify which kind of xml, I need to populate
And in getView() before inflating view, you should compare that value and depends on that, you should populate xml, like:
getView(){
if(case1){
View view = mInflater.inflate(R.layout.case_one_layout, null);
//set all values and listener on delete button as your require.
}else if(case2){
View view = mInflater.inflate(R.layout.case_two_layout, null);
//here you can set listener on entire row as onItemclick from fragment.
}
}
You can also manage with switch or other way as you like.
If you are creating a very dynamic list, say, where every row can have a different set of input types plus optional buttons, and the list length is based on another dynamic value, is it better to do this in a list adapter or creating a custom view in a scroll window?
After struggling with list adapters for quite a while now something finally occurred to me- this seems dumb. It seems like I am going through a lot of work keeping track of what spinner is set to what value, which row was clicked and so forth.
For example, say you are showing something like a contacts screen with various details that can be entered about a contact. Some rows will have text inputs (name, address etc), some will have spinners (ie. state, group), some will have checkboxes (like 'favorite' or something). Also, there is an 'add' button that allows you to add another field to edit. Is it worth making this in a list adapter or is it better to populate a custom view, and if the "add" button is clicked, we re-create the custom view, adding a view of the type they want to add?
I hope this is clear.
ListViews (and List Adapters) are meant for data that is to be displayed in mainly similar views. For your example, it is much easier and more natural to have a predefined layout file with the screen and use view visibility so select which views are to be shown. If you need to add views to the screen you can do this dynamically by using findViewById on the layout and then using it's addView method.
Let me know if you need more clarification or sample code...
I am doing android application. In That I want to display a List of podcast urls like shown in the Image. In this I also share this url into FB, twitter and etc and also the user clicks the arrow symbol I want to forward to that podcast url.
I am having those values in separate arraylist (i.e. "4353,3424" as a arraylist and "567567, 234234" as a another arraylist likewise). So how can I display these values like shown in the attached image. Can anyone help me how to proceed to display like this?
I'd recommend you had a look at some of the tutorials on how to implement your own custom ListView.
An example can be found here: http://www.thepcwizard.in/2012/09/android-creating-custom-listview-for.html
Also I'd recommend you create a custom class for holding the different informations in every row of the ListView. When doing it like this you can have one single List<MyCustomObject> holding all informations and then when a row is clicked, you simple get the item from the List<MyCustomObject> and get the specific property of the custom object and act accordingly.
EDIT: Small example of how to add onClickListener to a sub-view of the row:
Inside the getView method of the custom Adapter you can use setOnClickListener to the views, you'd like to respond to clicks. For instance:
myImageView.setOnClickListener(this);
Then let your custom Adapter implement the interface OnClickListener and act accordingly to the clicks.
Another way would also be to add a Share Intent to the different images, like in this example:
http://sudarmuthu.com/blog/sharing-content-in-android-using-action_send-intent
You would need to create a custom ArrayAdapter to populate a ListView from this objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an Idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter
I want to create list view just like this: http://dl.maximumpc.com/galleries/androidpower/Alarm_full.jpg
I need to create view like on second and fourth screens. As for fourth screen, it seems that each row has it's own layout...
I've searched Internet and even downloaded alarm source code from git repository, but it doesn't contains what I want. Any help would be useful. Thanks!
The second screen is pretty straightforward. It's basically a RelativeLayout with a full-width button at the top and a listview. The listview items will use a custom layout e.g. a RelativeLayout with a Button showing the time, a TextView to show the description, a TextView to show the selected days and a CheckBox to indicate selection. The fourth screen looks like a preference activity which can be built up from an xml file and/or custom preferences.
Yes, as John J Smith said, it's not very complicated. And i'm sure there are many articles on Internet about how to build a custom list view(at least there're many in China).
And here is a general way to do this:
To custom a list view, follow these ways in general:
1. create your own list adapter, usually extends BaseAdapter, write getView method etc;
2. bind your adapter to a list view;
3. write a layout file implements your list view item, and bind data in your adapter.
Especially, if your custom list view item has a button/checkbox/etc, you'll need more work.
Suppose I have a listview with 3 rows. If the user clicks a button in row 1, it expands a menu (it's just a linear layout that's shown/hidden). If they then click on an item in row 2, I'd like to be able to collapse the menu in row 1. Is this possible? If so, how?
Save a reference to the View you would like to manipulate later. Probably wrap in a final variable, then you will be easy to change.
You can always use getContext() on the View to get a reference to the containing Activity, which you can cast to a ListActivity (assuming you're using that) and call the usual methods to get the data at that position in the ListAdapter:
((ListActivity) v.getContext()).getListAdapter().getItem(1)
Then you can manipulate your data however you want to and call notifyDataSetChanged() on your ListAdapter.
But it'd probably be a lot easier for you and your users to just use ExpandableListView, which gives you expandable lists with predictable interactions your users already know. You can check the history activity in the Browser source code in AOSP for a real life example. And if it doesn't exactly meet your needs, you can always yank the code for ExpandableListView itself from the Android source.