How to Hide One Item in an Android Spinner

Hiding Spinner Items in Dropdown

Instead of Spinner you should use EditText with a hint attribute.

For example:

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_text_hint"
android:cursorVisible="false"
android:inputType="none"
android:textIsSelectable="true"/>

In Java-code:

editText.setOnFocusChangeListener(

@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) return;

// Показываем диалог
MyDialog dialog = new MyDialog();
dialog.show(getFragmentManager(), "");
});

public void onItemSelected(String itemName) {
editText.setText(itemName);
}

And you should create a DialogFragment class MyDialog which displays a list of items and calls onItemSelected callback.

How do i hide the selected item of spinner

You can play with the spinner adapter, changing the text of the spinner item when it is selected to be blank, and restoring it once it is unselected. This isn't the most graceful solution, but it will give you what you want.

how to hide the i'd of the item in the spinner?

First make your list take an object like

final ArrayList<Target> target = new ArrayList<Target>();

Then Create a POJO class for your target data

public class Target {

private String targetId;
private String targetName;

public void setTargetId(String targetId){
this.targetId = targetId;
}

public String getTargetId(){
return targetId;
}

public void setTargetName(String targetName){
this.targetName= targetName;
}

public String getTargetName(){
return targetName;
}

}

Then inside your loop

for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);

Target targetObject = new Target();
targetObject.setTargetId(jsonObject.getString("target_id"));
targetObject.setTargetName(jsonObject.getString("target_name"));

target.add(targetObject);
}

Then inside your CustomAdapter_Spinner class's getView() method get only the target name like

@Override
public View getView(int position, View convertView, ViewGroup parent) {

convertView = inflter.inflate(R.layout.custom_spinner_items, null);
TextView names = (TextView) convertView.findViewById(R.id.textView);
names.setText(countryNames.get(position).getTargetName());
return convertView;
}

And you are done. Hope this helps.



Related Topics



Leave a reply



Submit