微调器未显示

时间:2016-11-05 06:35:41

标签: android spinner

这是函数,我试图使用微调器,从API获取数据,但问题是微调器下拉不显示。我是android.I的新手附加下面的代码。请建议我的解决方案。代码中没有任何错误。我打印数组我得到的数据。唯一的问题是下拉不显示。当我点击按钮响应获取。

        @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"
            />

2 个答案:

答案 0 :(得分:1)

试试这段代码......

XML文件

 <?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文件

// 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);

答案 1 :(得分:0)

在布局中添加

<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>

在您的drawable文件夹中创建spinner_border.xml

<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>

在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) {

        }
    });


}

这是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>