Reputation: 123
This is the function ,I am trying to use the spinner , data getting from the API but the issue is that the spinner drop down not display.I am new to android.I attached the code below.Please suggest me solution. There is no any error in the code.I print the array i get the data.Only problem is that the drop down not display. When i click on the button response getting.
@Override
public void onClick(View v)
{
String access=op.getUrl(getApplicationContext(),"ticket","update_properties","");
JSONArray access_denied = null;
try
{
access_denied = new editProperties(access).execute().get();
String access_result =access_denied.toString();
if(access_result.equals("[\"Accessdenied\"]"))
{
Operation.showToast(getApplicationContext(), R.string.access);
}
else
{
//Log.d("In the else Condition : ","Success");
String DEPT_URL=op.getUrl(getApplicationContext(),"ticket","get_department","&vis_all_department=1");
final String dept=tv_dept.getText().toString();
filter_dept_id=tv_dept_id.getText().toString();
if(v.getId()==R.id.td_tv_dept)
{
ArrayAdapter<String> dept_Adapter=op.get_dept_adapter(DEPT_URL,DEPARTMENT,Ticket_properties.this,domain_id);
final ArrayAdapter<String> dept_id_Adapter=op.get_dept_adapter(DEPT_URL,DEPARTMENT_ID,Ticket_properties.this,domain_id);
spin.setAdapter(dept_Adapter);
final int dept_Position = dept_Adapter.getPosition(dept);
spin.setSelection(dept_Position);//THIS ONE
spin.performClick();
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
String selected_item=parent.getItemAtPosition(pos).toString();
String selected_item_id=dept_id_Adapter.getItem(pos).toString();
String filter="&vis_ticket_id="+Ticket_id+"&vis_action=department&vis_update_id="+selected_item_id;
String UPDATE_DEPT_URL=op.getUrl(getApplicationContext(),"ticket","update_properties",filter);
JSONArray dept_array;
try
{
dept_array = new editProperties(UPDATE_DEPT_URL).execute().get();
String result =dept_array.toString();
if(result.equals("[\"success\"]"))
{
tv_dept.setText(selected_item);
tv_dept_id.setText(selected_item_id);
}
else {Operation.showToast(getApplicationContext(), R.string.error);}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
spin.setSelection(dept_Position);
}
});
}
else if
{
//NEXT CODE
}
//**********************************************************************
public ArrayAdapter<String> get_dept_adapter(String URL, String ITEM, Activity context,String domain_id)
{
// TODO Auto-generated method stub
JSONArray array ;
List<String> item_list = new ArrayList<String>();
try {
array=new adapter(URL+"&vis_encode=json",context).execute().get();
for (int i = 0; i <array.length(); i++)
{
JSONObject object;
try {
object = array.getJSONObject(i);
String new_domain_id=object.getString("domain_id");
if(domain_id.equals(new_domain_id))
{
String item=object.getString(ITEM);
item_list.add(item);
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayAdapter<String> item_Adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,item_list);
item_Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
return item_Adapter;
}
//***********************************************************
Hello i just added the android:spinnerMode="dialog"
style="@android:style/Widget.Spinner.DropDown" these two lines and working fine.
Previous
==========
<Spinner
android:id="@+id/td_spin"
android:layout_width="0dp"
android:layout_height="0dp" />
After
=======
<Spinner
android:id="@+id/td_spin"
android:layout_width="0dp"
android:layout_height="0dp"
android:spinnerMode="dialog"
style="@android:style/Widget.Spinner.DropDown"
/>
Upvotes: 0
Views: 94
Reputation: 740
Add this in your layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="@drawable/spinner_border"
android:orientation="horizontal">
<Spinner
android:id="@+id/select_model"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:gravity="center"
android:layout_marginLeft="5dp"
android:spinnerMode="dropdown" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:src="@drawable/ic_arrow_drop_down_black_24dp" />
</RelativeLayout>
creat spinner_border.xml in your drawable folder
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/transparent" />
<corners android:radius="5dp" />
<stroke
android:width="1dp"
android:color="@android:color/darker_gray"/>
</shape>
setup spinner like this in your activity.java
private void setUpSpinner(ArrayList<String> mModels){
modelSpinnerAdapter = new SelectionListAdapter(this, mModels);
dropdown.setAdapter(modelSpinnerAdapter);
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// model = parent.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
and this is SelectionListAdapter.java
public class SelectionListAdapter extends BaseAdapter {
private Context context;
private List<String> listItemTitles;
public SelectionListAdapter(Context context, List<String> listItemTitles) {
this.context = context;
this.listItemTitles = listItemTitles;
}
@Override
public int getCount() {
return listItemTitles.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return listItemTitles.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.selector_row_item, parent, false);
}
TextView listItemTitleView = (TextView)convertView.findViewById(R.id.selector_item_texiView);
listItemTitleView.setText(listItemTitles.get(position).toUpperCase());
return convertView;
}
}
select_row_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="35dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Title"
android:textColor="@color/md_black_1000"
android:textSize="14sp"
android:id="@+id/selector_item_texiView" />
</RelativeLayout>
Upvotes: 0
Reputation: 419
Try this code...
XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Category:"
android:layout_marginBottom="5dp"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/spinner_title"/>
</LinearLayout>
Java file
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Spinner Drop down List (in your case set data from api)
List<String> categories = new ArrayList<String>();
categories.add("Item 1");
categories.add("Item 2");
categories.add("Item 3");
categories.add("Item 4");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
Upvotes: 1