How to keep text with out changing on Spinner selection in android? - android

I would like to implement a drop down with label "CATEGORY" in android.
Here, when I click on the "CATEGORY" button, then a list should be populated. But, when I select any item in that list, then I dont want to change the label "CATEGORY".
How to do that?

Once you understand how a spinner works, this becomes easy. :)
The spinner uses the getView method to populate the closed spinner, and the getDropDownView method to create the dropdown. With this information you can create a custom adapter that can have something other than the current selection showing in the closed view. This also lets you avoid having to have non-data (like the word "CATEGORY") in your data.
A quick example:
public class CustomAdapter extends ArrayAdapter {
private Context context;
private int textViewResourceId;
private String[] objects;
public CustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.textViewResourceId = textViewResourceId;
this.objects = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = View.inflate(context, textViewResourceId, null);
TextView tv = (TextView) convertView;
tv.setText("CATEGORY");
}
return convertView;
}
}
The rest of the spinner would function as normal, so you could capture the selection in the onItemSelectedListener.

Inside your OnItemSelectedListener method, get the value of your selection and store it somewhere shared prefs and reset the position using spinnersetSelection(0); :) if the spinner is clicked again load the value from shared prefs :)

Related

Custom ArrayAdapter without custom item data-type

I try to implement a ListView with specific icon, title and subtitle at each item.
The data of all items is in an ArrayList of objects from following class:
class ItemObject{
String title="";
String subTitle="";
String unit="";
int icon;
int quantity;
int parentID;
int orderInList;
}
ArrayList<ItemObject> listViewData;
Here is the code I used first:
class ExtendedArrayAdapter<String> extends ArrayAdapter<String>{
private Context context;
private String[] items;
public ExtendedArrayAdapter(Context context, String[] items){
super(context,-1,items);
this.context = context;
this.items=items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.lv_item_bg,parent,false);
TextView itemTitle = (TextView)itemView.findViewById(R.id.itemTitle);
TextView itemSubTitle = (TextView)itemView.findViewById(R.id.itemSubTitle);
ImageView itemIcon = (ImageView)itemView.findViewById(R.id.itemIcon);
itemTitle.setText(listViewData.get(position).title);
itemSubTitle.setText(listViewData.get(position).subTitle);
itemIcon.setBackgroundResource(listViewData.get(position).icon);
return itemView;
}
}
But the problem regarding this custom ArrayAdapter is that ExtendedArrayAdapter needs a String[] of items to get the number of ListView items.
Question:
Is it possible to change the ExtendedArrayAdapter class to get directly the length of ListView instead of String[] items?
EDIT1:
In super(), I can use only following set of parameters:
and all need Array<String> or List<String>. So I cannot use my ArrayList of objects here.
Is it possible to change the ExtendedArrayAdapter class to get
directly the length of ListView instead of String[] items?
the length of the ListView in terms of item it is handling, is given from Adapter.getCount(). The answer to your question, if I understand it correctly, is then no. The definition of your Adapter should change like
class ExtendedArrayAdapter extends ArrayAdapter<ItemObject>{
you will have to change the constructor and the type of items as well

ListView of Progress Bars in android

I want to create a scrollable list of progress bars. The number of progress bars and the content of each progress bar is decided dynamic and decided at run time.
Is it possible to put create and put progress bars in a ListView? I couldn't find any relevant resources on the internet and hence wanted to confirm if this is even possible in the current android framework.
Yes it is possible, you have to create a custom listView and can add items dynamically whenever you want to add
create a layout that will contain your listView
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
create an item layout which will represent your single item of the listView and create a listViewAdapter that will assign your values each of your item of the list
public class ListAdapter extends ArrayAdapter{
private final Context context;
private int layoutResourceID;
private ArrayList<YourDataModel> objects;
public ListAdapter(Context context, int layoutResourceID, ArrayList<YourDataModel> objects) {
super(context, layoutResourceID, objects);
this.context = context;
this.layoutResourceID = layoutResourceID;
this.objects = objects;
}
#Override
public View getView(int position, final View convertView, ViewGroup parent) {
View row = convertView;
final ListHolder listHolder;
if(row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceID, parent, false);
itemProgressBar = (ProgressBar) row.findViewById(R.id.item_progressbar);
row.setTag(listHolder);
} else {
listHolder = (ListHolder) row.getTag();
}
final DataCategory list = objects.get(position); // get your data object
// assign values to your items of listView
return row;
}
class ListHolder {
ProgressBar itemProgressBar;
}
}
in the above code YourDataModel your java class containing your data
and last in your main set all the things to make it work
ListAdapterHome listAdapter = new ListAdapter(YourActivity.this,
R.layout.item, ArrayListOfYourDataModel); // pass the arrayList of your dataModel there
ListView listView = (ListView) view.findViewById(R.id.listView);
listView.setAdapter(listAdapterHome);
It is possible. You would have to write your own ListAdapter (i would recommend overwriting SimpleArrayAdapter, but it depends on your Data) and overwrite the getView Method to generate Views that contain a ProgressBar.
Check this question: Custom Adapter for List View
FYI: The ListAdapter is the class that generates the Views of the ListView and assigns their values. Usually if you generate a SimpleArrayAdapter you pass the Layout you want it to create. That works if you only want to set some text in the layout. However to work with a View as specific as a ProgressBar you would have to create your own ListAdapter to handle that specific view.

Why is the Spinner's interface SpinnerPopup private? Why can't I add my own popup view?

I was looking at a way to answer this question where the OP is trying to limit the number of items displayed in the spinner's dropdown view. It seems that it cannot be done.
The Spinner class has its own private interface called SpinnerPopup which defines how dropdown items can be shown. This is currently based on the spinnerMode allowing for a dropdown or dialog list.
Both options are also implemented inside the Spinner class as private classes: DialogPopup and DropdownPopup. So it seems to me that the only way to customize this to add another popup option would be to copy the spinner source code and create my own version of it.
But if the SpinnerPopup interface were public, it seems like it would be easy to just:
Create my own popup implementing SpinnerPopup; and
Create my own spinner extending the original one where I override the constructor Spinner(Context context, AttributeSet attrs, int defStyle, int mode) to handle my popup.
Does anyone have any idea (or guess) why this is not the case? Or am I missing a simpler solution here?
Thanks!
I need set my own view to the dropdown, so this was what I did:
public class SpinnerAdapter_Tabela_Preco extends ArrayAdapter<Tabela_Preco>{
// Your sent context
private Context context;
private List<Tabela_Preco> list_tabela_preco;
public SpinnerAdapter_Tabela_Preco(Context context, int textViewResourceId, List<Tabela_Preco> values) {
super(context, textViewResourceId, values);
this.context = context;
this.list_tabela_preco = values;
}
public int getCount(){
return list_tabela_preco.size();
}
public Tabela_Preco getItem(int position){
return list_tabela_preco.get(position);
}
public long getItemId(int position){
return position;
}
// And the "magic" goes here
// This is for the "passive" state of the spinner
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Tabela_Preco t_p = getItem(position);
// I created a dynamic TextView here, but you can reference your own custom layout for each spinner item
TextView label = new TextView(context);
label.setTextColor(Color.BLACK);
label.setTypeface(null, Typeface.BOLD);
// Then you can get the current item using the values array (Users array) and the current position
// You can NOW reference each method you has created in your bean object (User class)
label.setText(t_p.toString());
// And finally return your dynamic (or custom) view for each spinner item
return label;
}
// And here is when the "chooser" is popped up
// Normally is the same view, but you can customize it if you want
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
Tabela_Preco t_p = getItem(position);
TextView label = new TextView(context);
label.setTextColor(Color.BLACK);
label.setBackgroundColor(Color.WHITE);
label.setMinimumHeight(50);
label.setGravity(Gravity.CENTER_VERTICAL);
label.setTextSize(18);
label.setText(t_p.toString());
return label;
}
}
And I use with this:
final SpinnerAdapter_Tabela_Preco adapter = new SpinnerAdapter_Tabela_Preco(Consulta_Produto.this, android.R.layout.simple_spinner_item, lista_tabela_preco);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_tabela_preco.setAdapter(adapter);
I hope this could help you.

Multiple List Views?

I'm trying to display high scores for a game I'm creating, with two columns, one for their name, and the other for the amount of moves it took them to complete the game.
Currently it is all stored in a SQLiteDatabase and presented in a list view, where it appears as one column in the format
name,moves
But I'd like to get the moves on the opposite of the screen, would this require multiple list views or would it require editing of the one list view, or its adapter?
The code currently used is:
datasource = new HighScoreDataSource(this);
datasource.open(); //Open the connection
List<HighScore> values = datasource.getAllHighScores(); //Retrieve all the data
//Using a simple cursor adapted to show the elements
ArrayAdapter<HighScore> adapter = new ArrayAdapter<HighScore>(this, android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
Make a row layout with two TextViews placed the way you want and implement a simple custom ArrayAdapter:
public class CustomAdapter extends ArrayAdapter<HighScore> {
private LayoutInflater inflater;
public CustomAdapter(Context context, int textViewResourceId,
List<HighScore> objects) {
super(context, textViewResourceId, objects);
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.new_row_layout, parent, false);
}
HighScore item = getItem(position);
TextView name = (TextView) findViewById(R.id.name_id_textview);
name.setText(/*get the name from the item HighScore object*/);
TextView moves = (TextView) findViewById(R.id.moves_id_textview);
moves.setText(/*get the moves from the item HighScore object*/);
return convertView;
}
}
Another option is to break your List<HighScore> values in a List of HashMaps(containing two entries, one for name and one for moves) and use a SimpleAdapter(with the row layout above).

how can i use android ListView to display fields in an object?

I am trying to construct a ListActivity that uses a listview to display a list of friends and their associated status (ie single, in a relationship).
So far, I have an ArrayAdapter set like so:
Friend[] friendList = new Friend[] {
new Friend("john doe", "single"),
new Friend("jane doe", "married")
};
setListAdapter(new ArrayAdapter<Friend>(this, R.layout.portal_listview, friendList));
I would like each item in the listview to display as such:
1name: john doe
status: single
2name: jane doe
status: married
Create you own CustomArrayAdapter which extends ArrayAdapter and overwrite the View getView(int position, View convertView, ViewGroup parent) method
I could be something like this
public class FriendAdapter extends ArrayAdapter<Friend>{
private Context context;
private int resource;
private ArrayList<Friend> friends;
public FriendAdapter(Context context, int resource, ArrayList<Friend> friends) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
this.friends = friends;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
if (view == null){
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(your_list_item_resource, null);
}
Friend friend = friends.get(position);
view.setId(position);
if (friend != null){
TextView name = (TextView) view.findViewById(R.id.friendName);
name.setText(friend.getName());
TextView status = (TextView) view.findViewById(R.id.friendStatus);
status.setText(friend.getStatus());
}
return view;
}
Hope this can help you.
In the xml row file, you can add a composite object, like a LinearLayout
and inside that you could include more than one textview, or a combination etc.
I guess the linearlayout would be unnecessary if the individual items within the list are displayed vertically, but you can try.
You need to override getView in the ArrayAdapter class. In that method, inflate your R.layout.portal_listview instance and fill it with whatever you want.
That xml layout should be some layout form that includes a TextView for the two things you want to display.
Easiest would be a linear layout, set to vertical orientation, with 2 TextViews in it, stacked.

Categories

Resources