I'm trying to get my spinneradapter working. I get no error's but
the spinner remains empty. I have read multiple tutorials but they aren't seem to work
very well. I'm trying to turn every row in a specifik color, that's why I need the adapter.
This is my code:
public class spinnerAdapter extends ArrayAdapter<String> implements
SpinnerAdapter {
Context context;
ArrayList<String> dateArray;
public spinnerAdapter(Context context, ArrayList<String>dateArray) {
super(context, R.layout.ruilen2_spinner);
this.context = context;
this.dateArray = dateArray;
}
static class ViewHolder {
public TextView textView;
public TextView textView2;
}
#Override
public View getDropDownView(int position, View view, ViewGroup parent) {
View rowView = view;
final ViewHolder holder;
if (rowView == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
rowView = vi.inflate(R.layout.ruilen2_spinner, null);
holder = new ViewHolder();
holder.textView = (TextView)rowView.findViewById(R.id.spinnerdate);
//holder.textView2 = (TextView)rowView.findViewById(R.id.spinnerworkplace);
rowView.setTag(holder);
}
else{
holder = (ViewHolder) rowView.getTag();
}
holder.textView.setText(dateArray.get(position));
return super.getDropDownView(
position, rowView, parent);
}
}
Here's the code snippet where I call the class:
s.setAdapter(new spinnerAdapter(getParent(),namen));
And last but not least the XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:background="#color/white"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/spinnerdate" android:textColor="#color/black"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/spinnerworkplace" android:textColor="#color/black"></TextView>
</LinearLayout>
If it is displaying but is empty then you might not be creating your dateArray properly. Did you check to make sure namen in s.setAdapter(new spinnerAdapter(getParent(),namen)); has anything in it?
Related
Can someone point out what's the problem with this code snippet
Custom adapter
public class NavigationAdapter extends ArrayAdapter<NavItem> {
private Context context;
private NavItem[] values;
public NavigationAdapter(Context context, int layoutResourceId,NavItem[] values) {
super(context, layoutResourceId);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder;
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView1 = convertView;
if (convertView1 == null) {
holder = new Holder();
convertView1 = vi.inflate(R.layout.item_row, parent,false);
holder.imageView = (ImageView) convertView1.findViewById(R.id.rowIcon);
holder.textView = (TextView) convertView1.findViewById(R.id.rowText);
convertView1.setTag(holder);
} else {
holder = (Holder) convertView1.getTag();
}
NavItem item = values[position];
holder.imageView.setImageResource(item.icon);
holder.textView.setText(item.name);
return convertView1;
}
private class Holder {
public TextView textView;
public ImageView imageView;
}
}
custom listview layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding ="10dp">
<ImageView
android:id="#+id/rowIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_settings"
android:paddingRight="10dp"/>
<TextView
android:id="#+id/rowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/rowIcon"
android:paddingRight="10dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"/>
Main activity
NavItem settings = new NavItem();
settings.icon = R.drawable.ic_action_settings;
settings.name = "Settings";
NavItem send = new NavItem(R.drawable.ic_action_settings,"Sent");
NavItem[] values = new NavItem[]{
settings,send
};
NavigationAdapter adapter = new NavigationAdapter(this, R.layout.item_row, values);
mDrawerListView.setAdapter(adapter);
When I open the drawer it just shows a blank page. If I change the adapter to ArrayAdapter of string and inflate the built in layout then it works, but I want to add image next to the text.
I have faced similar problem in my experience.
I solved this problem by changing :
public class NavigationAdapter extends ArrayAdapter<NavItem> {
}
to :
public class NavigationAdapter extends BaseAdapter<NavItem> {
}
I have extended my custom adapter from BaseAdapter class which have worked for my case. Please try this if it works for you also
I have successfully implemented Spinners in my project using the Spinner class which exists in this link:
How to make an Android Spinner with initial text "Select One"
Also, I customized the layout of each item and named it as spinner_entries_style.xml ..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:background="#640c1c"
android:maxEms="10"
android:padding="10dp"
android:singleLine="false"
android:textColor="#d7a801"
android:textSize="18sp" />
</LinearLayout>
Also, These are the Adapter classes I used in my code..
class LoanTypeAdapter extends ArrayAdapter<String> {
Context context;
String[] items;
public LoanTypeAdapter(Context context, String[] items) {
super(context, R.layout.spinner_entries_style, R.id.textView, items);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(android.R.layout.simple_spinner_item,
parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(items[position]);
holder.textView.setTextColor(Color.parseColor("#d7a801"));
holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
holder.textView.setSingleLine(true);
return holder.textView;
// ------------------------------------------
}
class ViewHolder {
final TextView textView;
ViewHolder(View view) {
textView = (TextView) view;
}
}
}
And this..
class LoanProgramAdapter extends ArrayAdapter<String> {
Context context;
String[] items;
public LoanProgramAdapter(Context context, String[] items) {
super(context, R.layout.spinner_entries_style, R.id.textView, items);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(android.R.layout.simple_spinner_item,
parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(items[position]);
holder.textView.setTextColor(Color.parseColor("#d7a801"));
holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
holder.textView.setSingleLine(true);
return holder.textView;
}
class ViewHolder {
final TextView textView;
ViewHolder(View view) {
textView = (TextView) view;
}
}
}
But, there is something strange I encountered after building the spinners. When running the app in Android versions (4.2 or less), there is a white space below the dropdown list.
Here are the screenshots of what happens..
http://www.mediafire.com/view/cqj51t8e3aju18m/01.png
http://www.mediafire.com/view/ure788yetrt00v3/02.png
That is not just on the emulator, but also in the real devices having Android 4.2 or less. Also, this white space seems a bit bigger in some devices.
Is there any idea to hide or kill these white area? Any solution for this problem?
I found the solution of this problem after reading this post.
How to wrap lengthy text in a spinner
In my layout called spinner_entries_style.xml, I should specify the height of text view like that:
android:layout_height="?android:attr/listPreferredItemHeight"
And it will be like that:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_marginBottom="2dp"
android:background="#640c1c"
android:maxEms="10"
android:padding="10dp"
android:singleLine="false"
android:textColor="#d7a801"
android:textSize="18sp" />
</LinearLayout>
That solved my problem successfully..
Remove this line from spinner_entries_style.xml.
android:layout_marginBottom="2dp"
I created my own custom adapter that extends Array Adapter:
public class AdapterGeoArea extends ArrayAdapter<GeoArea>{
private Context _context;
private int resource;
private ArrayList<GeoArea> _myGeoArea;
public AdapterGeoArea(Context context, ArrayList<GeoArea> myGeoArea) {
super(context, 0, myGeoArea);
_context = context;
_myGeoArea = myGeoArea;
}
public int getCount() {
return _myGeoArea.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout GeoAreaView;
if (convertView == null) {
GeoAreaView = new LinearLayout(_context);
LayoutInflater li = LayoutInflater.from(_context);
convertView = li.inflate(R.layout.layout_geo_areas, null);
}
else {
GeoAreaView = (LinearLayout) convertView;
}
GeoArea curGeoArea = _myGeoArea.get(position);
TextView name = (TextView) GeoAreaView.findViewById(R.id.txtGeoAreaName);
name.setText(curGeoArea.name);
return convertView;
}
static class ViewHolder {
TextView name;
RelativeLayout childContainer;
}
#Override
public Filter getFilter() {
return null;
}
}
And here is how I am using it in my main:
public class ActivityGeoAreas extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_geo_areas);
GeoArea.searchTerm = "Bar & Grill";
GeoArea torontoArea = new GeoArea("cityOfToronto");
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
testList.add(torontoArea);
AdapterGeoArea adapter = new AdapterGeoArea(this, testList);
ListView lv = (ListView) findViewById(R.id.textGeoArea);
lv.setAdapter(adapter);
}
}
Everything seem to be fine except for this line:
TextView name = (TextView) GeoAreaView.findViewById(R.id.textGeoArea);
And here is how I defined my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".ActivityGeoAreas" >
<TextView
android:id="#+id/textGeoArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Choose Area"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="#+id/LinearLayoutGeoAreas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textGeoArea"
android:layout_below="#+id/textGeoArea"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewGeoArea"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
</RelativeLayout>
It simply can't find the TextView textGeoArea and this line become null, what am I doing wrong?
Change this
TextView name = (TextView) GeoAreaView.findViewById(R.id.txtGeoAreaName);
to
TextView name = (TextView) convertView.findViewById(R.id.txtGeoAreaName);
I guess you have got confused.
Move textview to a new layout list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textGeoArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Choose Area"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Now change your getView as below
LayoutInflater inflater;
public AdapterGeoArea(Context context, ArrayList<GeoArea> myGeoArea) {
super(context, 0, myGeoArea);
_context = context;
_myGeoArea = myGeoArea;
inflater = LayoutInflater.from(context); // initialize inflater
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row,parent,false);
holder = new ViewHolder();
holder.tv = (TextView)convertView.findViewById(R.id.textGeoArea);
holder.setTag(convertView);
}
else {
holder = (ViewHolder) convertView.getTag();
}
GeoArea curGeoArea = _myGeoArea.get(position);
holder.tv.setText(curGeoArea.name);
return convertView;
}
static class ViewHolder // use a view holder for smooth scrolling an performance
{
TextView tv;
}
On the first pass, GeoAreaView is a childless LinearLayout since you just created it. convertView is the View initialized your layout which holds the TextView. So you need to use that to find the TextView.
TextView name = (TextView) convertView.findViewById(R.id.txtGeoAreaName);
I see you have a ViewHolder but you don't appear to be using it. You may want to take a little time and go through a good tutorial on using ListView.
This guy usually seems to have good tutorials
How can I create something like this?
I want to have the "selected image" as the main view, and then a time line effect at the bottom; something like a Gallery would work, but that has been deprecated.
I'm thinking a HorizontalScrollView at the bottom, but will that recycle the views properly? Can I implement the ViewHolder pattern here?
What about StackView? I found this: http://www.gdgankara.org/2012/03/25/stackview-non-widget-sample/ which could be cool, but I couldn't find a lot of things about StackViews online, so I'm wondering how much it gets implemented?
I solved this problem by using a custom horizontal ListView by MUKESH YADAV
I changed his HorizontalImageAdapter.java class to use a CursorAdapter.
My solution here:
public class HorizontalImageAdapter extends CursorAdapter {
//...some initialization here
public HorizontalImageAdapter(Context context, Cursor c, int count) {
super(context, c, true);
this.context = (Activity) context;
this.count = count;
this.cursor = c;
inflater = LayoutInflater.from(context);
cursor.moveToFirst();
}
private static class ViewHolder {
public ImageView imageview;
public TextView textview;
public ImageView imageviewvideo;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
cursor.moveToPosition(position);
if (convertView == null) {
Log.d(TAG, "converView == null <<<<<<<<<<<<<<<<<<<<<<<<");
convertView = inflater.inflate(R.layout.activity_media_items, null);
holder = new ViewHolder();
holder.textview = (TextView) convertView.findViewById(R.id.tv_details_title);
holder.imageview = (ImageView) convertView.findViewById(R.id.iv_details_resource_image);
holder.imageviewvideo = (ImageView) convertView.findViewById(R.id.iv_details_resource_video);
convertView.setTag(holder);
} else {
Log.d(TAG, "converView != null >>>>>>>>>>>>>>>>>>>>>>>>");
}
//get the type of the item
type = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_ITEM_TYPE));
if(type.equalsIgnoreCase("text")){
//handle for text item
}
if(type.equalsIgnoreCase("image")){
//handle for image item
}
if(type.equalsIgnoreCase("video")){
//handle for video item
}
return convertView;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
String title = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_TITLE));
String fileName = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIELD_RESOURCE));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View eachitem = inflater.inflate(R.layout.activity_media_items, parent, false);
holder.textview = (TextView) eachgrid.findViewById(R.id.tv_details_title);
holder.imageview = (ImageView) eachgrid.findViewById(R.id.iv_details_resource_image);
holder.imageviewvideo = (ImageView) eachgrid.findViewById(R.id.iv_details_resource_video);
eachgrid.setTag(holder);
return eachitem;
}
}
And the XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/black" >
<ImageView
android:id="#+id/selected_imageview"
android:layout_width="#dimen/exhibit_item_width"
android:layout_height="#dimen/exhibit_item_height"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:maxHeight="#dimen/exhibit_item_height"
android:maxWidth="#dimen/exhibit_item_width"
android:src="#drawable/logo" />
<RelativeLayout
android:id="#+id/gallery_relative_layout"
android:layout_width="wrap_content"
android:layout_height="#dimen/exhibit_items_height"
android:layout_gravity="bottom"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<com.example.testdatabase2.HorizontalView
android:id="#+id/gallery"
android:layout_width="match_parent"
android:layout_height="#dimen/exhibit_items_height"
android:layout_centerHorizontal="true"
android:smoothScrollbar="true"
android:spacing="20dip" >
</com.example.testdatabase2.HorizontalView >
</RelativeLayout>
</LinearLayout>
Is there anyway to add two text views or strings inside of the Actionbar Spinner?
Similar to how the Android GMAIL app does it.
You could use a custom ArrayAdapter:
public class spinnerAdapter extends ArrayAdapter {
private LayoutInflater inflater;
public spinnerAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.spinner_text_layout, null);
holder.text1 = (TextView)convertView.findViewById(R.id.spinnerText1);
holder.text2 = (TextView)convertView.findViewById(R.id.spinnerText2);
convertView.setTag(R.layout.spinner_text_layout, holder);
} else{
holder = (ViewHolder)convertView.getTag(R.layout.spinner_text_layout);
}
holder.text1.setText("Position: " );
holder.text2.setText(position);
return convertView;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
ViewHolder2 holder;
if(convertView == null){
holder = new ViewHolder2();
convertView = inflater.inflate(R.layout.spinner_text_layout, null);
holder.text1 = (TextView)convertView.findViewById(R.id.spinnerText1);
holder.text2 = (TextView)convertView.findViewById(R.id.spinnerText2);
convertView.setTag(R.layout.spinner_text_layout, holder);
} else{
holder = (ViewHolder2)convertView.getTag(R.layout.spinner_text_layout);
}
holder.text1.setText("Position: " );
holder.text2.setText(position);
return convertView;
}
static class ViewHolder{
TextView text1;
TextView text2;
}
static class ViewHolder2{
TextView text1;
TextView text2;
}
}
spinner_text_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/spinnerText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="16dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/spinnerText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentRight="true"
android:layout_marginRight="23dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
ActionBar.setListNavigationCallbacks(...)
It's just a SpinnerAdapter. If you are familiar with adapters, you can make your own class extending BaseAdapter and have a custom layout. If you aren't familiar with adapters, I suggest you watch The World of ListView.