2016-12-19 2 views
0

Ich möchte ein Spinner haben, der eine Liste von Elementen enthalten wird.Wie erstellt man eine Liste von Spinner?

Der Entwurf, den ich will, ist wie folgt:

enter image description here

Ich versuchte Hintergrund für die Spinner zu setzen, so jetzt ist es nur einen Hintergrund zeigt. Ich möchte weißes Layout innerhalb und mit Trennzeichen.

Ich habe versucht, ein Layout für das Element zu erstellen und an die Spinner gelten, aber es gibt Fehler:

Process: com.kiranaapp, PID: 16697 
                  java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 

Mein Entwurf sieht nun wie folgt aus:

enter image description here

Und Onclick der Spinner Ich bekomme Layout wie folgt:

enter image description here

Aber ich möchte, dass die Liste onClick von TextInputLayout angezeigt wird.

Hier ist XML-Datei:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/bg" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.kiranacustomerapp.Activities.SearchActivity" 
    tools:showIn="@layout/activity_search"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/white" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:layout_height="wrap_content"> 


      <android.support.design.widget.TextInputLayout 
       android:id="@+id/input_layout_item_name" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:layout_marginTop="20dp" 
       android:paddingTop="05dp" 
       android:layout_marginLeft="16dp" 
       android:layout_marginRight="16dp"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="150dp" 
        android:background="@drawable/bg"> 
       <Spinner 
        android:id="@+id/items_spinner" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:visibility="visible" 
        android:layout_marginLeft="10dp" 
        android:layout_marginRight="10dp" 
        android:background="#ffffff" 
        android:layout_marginTop="10dp" 
        android:layout_marginBottom="10dp" /> 
       </LinearLayout> 
      <android.support.design.widget.TextInputEditText 
       android:layout_width="240dp" 
       android:layout_height="45dp" 
       android:id="@+id/editTextItemName" 
       android:layout_gravity="center_horizontal" 
       android:hint="@string/item_name" 
       android:textSize="15sp" 
       android:padding="10dp" > 

       </android.support.design.widget.TextInputEditText> 
      </android.support.design.widget.TextInputLayout> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="vertical" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <android.support.design.widget.TextInputLayout 
       android:id="@+id/input_layout_item_unit" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:paddingTop="05dp" 
       android:layout_marginLeft="16dp" 
       android:layout_marginRight="16dp"> 
       <android.support.design.widget.TextInputEditText 
        android:layout_width="240dp" 
        android:layout_height="45dp" 
        android:id="@+id/editTextItemUnit" 
        android:layout_gravity="center_horizontal" 
        android:hint="@string/unit" 
        android:textSize="15sp" 
        android:padding="10dp" /> 
      </android.support.design.widget.TextInputLayout> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="vertical" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <android.support.design.widget.TextInputLayout 
       android:id="@+id/input_layout_item_quantity" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:paddingTop="05dp" 
       android:layout_marginLeft="16dp" 
       android:layout_marginRight="16dp"> 
       <android.support.design.widget.TextInputEditText 
        android:layout_width="240dp" 
        android:layout_height="45dp" 
        android:id="@+id/editTextItemQuantity" 
        android:layout_gravity="center_horizontal" 
        android:hint="@string/quantity" 
        android:textSize="14sp" 
        android:padding="10dp" /> 
      </android.support.design.widget.TextInputLayout> 
     </LinearLayout> 

     <Button 
      android:layout_width="100dp" 
      android:layout_height="30dp" 
      android:text="Select" 
      style="?android:attr/borderlessButtonStyle" 
      android:id="@+id/buttonSelect" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="100dp" 
      android:layout_marginBottom="50dp" 
      android:background="@drawable/btn_hlf_blue" 
      android:textColor="@android:color/white" 
      android:textSize="12sp" /> 
    </LinearLayout> 

</RelativeLayout> 

Aktivität:

public class SearchActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { 

    private TextInputEditText edt_Item_Name,edt_Item_Unit,edt_Item_quantity; 
    private TextInputLayout textInput_Item_name,textInput_Item_Unit,textInput_Item_quantity; 

    private Spinner spinner; 
    private Button btnSelect; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_search); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setNavigationIcon(R.drawable.ic_back); 
     setSupportActionBar(toolbar); 

     toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       finish(); 
      } 
     }); 

     setUpUI(); 
    } 

    public void setUpUI(){ 

     edt_Item_Name = (TextInputEditText) findViewById(R.id.editTextItemName); 
     edt_Item_quantity = (TextInputEditText)findViewById(R.id.editTextItemQuantity); 
     edt_Item_Unit = (TextInputEditText)findViewById(R.id.editTextItemUnit); 

     textInput_Item_name = (TextInputLayout)findViewById(R.id.input_layout_item_name); 
     textInput_Item_quantity = (TextInputLayout)findViewById(R.id.input_layout_item_quantity); 
     textInput_Item_Unit = (TextInputLayout)findViewById(R.id.input_layout_item_unit); 

     spinner = (Spinner)findViewById(R.id.items_spinner); 
     btnSelect = (Button)findViewById(R.id.buttonSelect); 

     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
       R.array.items_array, R.layout.order_item_layout); 
// Specify the layout to use when the list of choices appears 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
// Apply the adapter to the spinner 
     spinner.setAdapter(adapter); 
     spinner.setOnItemSelectedListener(this); 

     textInput_Item_name.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       spinner.setVisibility(View.VISIBLE); 
       edt_Item_Unit.setVisibility(View.GONE); 
       edt_Item_quantity.setVisibility(View.GONE); 
       btnSelect.setVisibility(View.GONE); 

       textInput_Item_name.setBackgroundResource(R.drawable.purple_bg); 

      } 
     }); 

    } 
    public void onItemSelected(AdapterView<?> parent, View view, 
           int pos, long id) { 
     // An item was selected. You can retrieve the selected item using 
     // parent.getItemAtPosition(pos) 
    } 

    public void onNothingSelected(AdapterView<?> parent) { 
     // Another interface callback 
    } 
} 

Wie kann ich diesen Entwurf erreichen? Kann mir bitte jemand helfen? Danke ..

+0

Verwenden Sie den benutzerdefinierten Adapter für sipper http://androidexample.com /Custom_Spinner_With_Image_And_Text_-_Android_Example/index.php?view=article_discription&aid=84 –

+0

Auf der Fehler oben, können Sie einen anderen Konstruktor für die ArrayAdapter haben: 'neue ArrayAdapter (dieses, R.layout.layout_for_item, R.id.the_id_of_the_textview_from_the_layout, items_list) ' –

Antwort

0

Verwenden Listview oder RecyclerView und einen Spinner in einem Layout platzieren, die den Einsatz in Ihrem viewholder.class mit getView Methode in Adapterklasse zugeordnet wird, nach-die Ihren Datensatz mit Arraylist/HashMap mit Hilfe des Konstruktors der Adapterklasse.

+0

kannst du bitte irgendein Beispiel geben? – Sid

+0

Warum eine Reycler-Ansicht oder Listenansicht haben? – Sid

+0

Entschuldigung für den Vorschlag zur Verwendung von Listview oder Recyclerview. Missverständnis ! –

0

Folgen Sie dieser

package com.example.spinner; 

import java.util.ArrayList; 
import java.util.List; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemSelectedListener; 

class AndroidSpinnerExampleActivity extends Activity implements OnItemSelectedListener{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Spinner element 
     Spinner spinner = (Spinner) findViewById(R.id.spinner); 

     // Spinner click listener 
     spinner.setOnItemSelectedListener(this); 

     // Spinner Drop down elements 
     List<String> categories = new ArrayList<String>(); 
     categories.add("Automobile"); 
     categories.add("Business Services"); 
     categories.add("Computers"); 
     categories.add("Education"); 
     categories.add("Personal"); 
     categories.add("Travel"); 

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

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     // On selecting a spinner item 
     String item = parent.getItemAtPosition(position).toString(); 

     // Showing selected spinner item 
     Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show(); 
    } 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 
    } 
} 

Verwenden Sie dieses Layout

<?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> 
+0

Wie kann ich die Liste onClick von TextInputLayout anzeigen? – Sid

0

Zeichnen Sie Ihr Layout somthing wie folgt aus:

<RelativeLayout 
         android:id="@+id/spinner_layout" 
         android:layout_width="match_parent" 
         android:layout_height="@dimen/dp_30" 
         android:layout_margin="@dimen/dp_20" 
         android:background="@color/fragment_bg" 
         android:orientation="horizontal"> 

         <TextView 
          android:id="@+id/txt_city" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_centerVertical="true" 
          android:layout_marginStart="@dimen/dp_20" 
          android:hint="@string/select_city" 
          android:singleLine="tru 
          android:textSize="@dimen/sp_15"/> 

         <Spinner 
          android:id="@+id/spinner_city" 
          android:layout_width="match_parent" 
          android:layout_height="@dimen/dp_30" 
          android:layout_centerVertical="true" 
          android:entries="@array/StateName" 
          android:gravity="center" 
          android:padding="@dimen/sp_15" 
          app:binding="@{location.locations}"/> 
        </RelativeLayout> 

und Ihre TextInput- Layout anstelle von TextviewverwendenVerwenden Sie Edittext use Textview nicht zum Festlegen von angeklicktem Elementtext. "Wenn Sie edittext verwenden, müssen Sie zwei Mal tippen, um den Aufruf der Methode onClickListener zu erhalten, da beim ersten Klicken auf den Fokus gesetzt wird und die Soft-Tastatur geöffnet wird."

+0

Ich habe dieses Design vom Kunden, also kann ich Änderungen im Design nicht machen. – Sid

+0

nicht ändern Sie Ihr Design nur bekommen Idee aus meinem Code und implementieren in Ihrem vorhandenen XML, nur noch einmal versuchen zu verstehen. Wenn Sie demselben Muster folgen, können Sie Spinner und Ihr Textinput-Layout an der gleichen Position neben anzeigen, da Spinner bereits ohne Klick-Listener anklickbar ist und Sie keinen Klick-Listener auf Textansicht/Textinput-Layout anwenden müssen, sobald Sie auf Spinner klicken Sie können Ihren Inhalt zeigen. benutze einfach den obigen Code und mache Demo, dann ist es einfach zu verstehen, was ich sage. bitte versuche –

Verwandte Themen