Reputation: 5097
I have created a Spinner with Custom Adapter in which i have an image view and a text view when i select item from spinner then i display image with selected Item which is working Perfectely fine.
Problem:
But The Problem is when i select any item it show image with selected item in drop down. and also display the same (Image and Text) in spinner..
What I want
I want to show only text in spinner with no image as you can see in
image it is showing imges with text in spinner... and i dont want this
Here is the code that i am using please suggest me the solution Thanks
public class SpinnerAdapter extends ArrayAdapter<SpinnerItem> {
private Context mContext;
private ArrayList<SpinnerItem> listState;
public SpinnerAdapter(Context context, int resource,
ArrayList<SpinnerItem> objects) {
super(context, resource, objects);
this.mContext = context;
this.listState = objects;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@SuppressLint("InflateParams")
public View getCustomView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item_layout,
null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckedImage = (ImageView) convertView
.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
if (listState.get(position).isSelected()) {
holder.mCheckedImage.setVisibility(View.VISIBLE);
} else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
}
return convertView;
}
private class ViewHolder {
private TextView mTextView;
private ImageView mCheckedImage;
}
public void setSpinnerAdapter(ArrayList<SpinnerItem> spinnerItems) {
this.listState = spinnerItems;
notifyDataSetChanged();
}
}
private void setBottelCountData() {
final String[] select_qualification = { "", "1", "2",
"3" };
bottelCountList = new ArrayList<>();
for (int i = 0; i < select_qualification.length; i++) {
SpinnerItem spinnerItem = new SpinnerItem();
spinnerItem.setTitle(select_qualification[i]);
if (i == 2) {
spinnerItem.setSelected(false);
} else {
spinnerItem.setSelected(false);
}
bottelCountList.add(spinnerItem);
}
bottelCountAdapter = new SpinnerAdapter(getActivity(), 0,
bottelCountList);
bottelCountAdapter.setDropDownViewResource(R.layout.spinner_item_layout);
bottel_Count_Spinner.setAdapter(bottelCountAdapter);
bottel_Count_Spinner.setSelection(3);
}
bottel_Count_Spinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
if (position == 0) {
for (int count = 0; count < bottelCountList.size(); count++) {
bottelCountList.get(count).setSelected(false);
}
noOfBottel = 0;
return;
}
bottelCountList.get(position).setSelected(true);
for (int count = 0; count < bottelCountList.size(); count++) {
if (position != count) {
bottelCountList.get(count).setSelected(false);
}
}
bottel_Count_Spinner.setPrompt("Hello");
bottelCountAdapter.setSpinnerAdapter(bottelCountList);
noOfBottel = Integer.parseInt(bottelCountList.get(
position).getTitle());
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
xml for spinner item layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkbox"
android:layout_centerVertical="true"
android:padding="10dp"/>
<ImageView
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:src="@android:drawable/checkbox_on_background"
android:contentDescription="@string/app_name"
android:layout_marginLeft="10dp"
android:layout_alignParentStart="true" />
</RelativeLayout>
Upvotes: 2
Views: 1128
Reputation: 1924
Try this,
<RelativeLayout
android:id="@+id/rlCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="@android:color/white" >
<Spinner
android:id="@+id/spnCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:popupBackground="@android:color/transparent"
android:visibility="invisible" />
<Button
android:id="@+id/btnAllergies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:background="@android:color/transparent"
android:gravity="start|center"
android:paddingBottom="5dp"
android:paddingEnd="5dp"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:paddingStart="10dp"
android:paddingTop="5dp"
android:tag="0"
android:text="Select Country"
android:textColor="#ACACAC"
android:textSize="14sp" />
</RelativeLayout>
Upvotes: 0
Reputation: 3346
You need to seperate views for getDropDownView
and getView
methods. getDropDownView
is called for the row which is using for selected value. On the other hand getView
is called for each rows when you open spinner.
Upvotes: 1
Reputation: 20513
You have two methods
getView
- is shown after you select an valuegetDropDownView
- defines how you view should look when drop down is openedYou have to modify your getCustomView
and handle the difference (hide the checkbox image).
Try this
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent, true);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent, false);
}
@SuppressLint("InflateParams")
public View getCustomView(int position, View convertView, ViewGroup parent, boolean isDropDown) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.spinner_item_layout,
null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckedImage = (ImageView) convertView
.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
if (isDropDown) {
if (listState.get(position).isSelected()) {
holder.mCheckedImage.setVisibility(View.VISIBLE);
} else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
}
}
else {
holder.mCheckedImage.setVisibility(View.INVISIBLE);
}
return convertView;
}
Upvotes: 3