2014-04-23 1 views
0

안녕하세요 저는 Android에서 새로 생겼습니다. Spinner에 개체 목록을 채우고 싶습니다. 나는 그것을 어떻게하는지 봤지만 문자열 배열로 예제를 찾는다.스피너에 문자열이 아닌 객체 목록을 어떻게 채울 수 있습니까?

아무도 도와 줄 수 있습니까?

내 코드입니다 :

Categories 클래스 :

public class AgregarActividadActivity extends ActionBarActivity 
{ 

    private MobileServiceClient mClient; 
    private MobileServiceTable<Activities> mActivitiesTable; 
    private MobileServiceTable<Categories> mCategoriesTable; 
    private MobileServiceTable<Projects> mProjectsTable; 


    private EditText mTxtTitulo; 
    private EditText mTxtDescr; 
    String categryId = null; 
    List<Categories> catList = new ArrayList<Categories>(); 

    Spinner spEstado; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_agregar_actividad); 

     try 
     { 
      mClient = new MobileServiceClient(
        "https://site.azure-mobile.net/", 
        "AppKey", 
        this); 
      mActivitiesTable = mClient.getTable(Activities.class); 
      mCategoriesTable = mClient.getTable(Categories.class); 
     } 
     catch (MalformedURLException e) 
     { 
      createAndShowDialogExc(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error"); 
     } 

     mTxtTitulo = (EditText) findViewById(R.id.txtTitulo); 
     mTxtDescr = (EditText) findViewById(R.id.txtDescripcion); 
     getCategories(); 

     spEstado = (Spinner)this.findViewById(R.id.spEstado); 

     ArrayAdapter<Categories> Adapter = new ArrayAdapter<Categories>(this, 
       android.R.layout.simple_spinner_item, catList); 
     Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spEstado.setAdapter(Adapter); 
     spEstado.setOnItemSelectedListener(
       new AdapterView.OnItemSelectedListener() { 
        public void onItemSelected(
          AdapterView<?> parent, 
          View view, 
          int position, 
          long id) { 
         Categories item = (Categories) parent.getItemAtPosition(position); 
        } 

        public void onNothingSelected(AdapterView<?> parent) { 
        } 
       } 
      ); 

     spProjects = (Spinner)this.findViewById(R.id.spProyecto); 
     ArrayAdapter<Projects> proAdapter = new ArrayAdapter<Projects>(this, 
       android.R.layout.simple_spinner_item, proList); 
     proAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spProjects.setAdapter(proAdapter); 

    } 

    private void getCategories() 
    { 
     mCategoriesTable.execute(new TableQueryCallback<Categories>() 
     { 
      public void onCompleted(List<Categories> result, int count, Exception exception, ServiceFilterResponse response) 
      { 
       if (exception == null) 
       { 
        for (Categories item : result) 
        { 
         catList.add(item); 
        } 
       } 
       else 
       { 
        createAndShowDialog(exception, "Error"); 
       } 
      } 
     }); 
    } 
} 

내가 물체로 드롭 다운리스트를 얻을 수 있지만 하나 개의 항목을 선택하면이 내 활동 코드

public class Categories 
{ 
    @com.google.gson.annotations.SerializedName("id") 
    private String mId; 

    @com.google.gson.annotations.SerializedName("name") 
    private String mName; 

    public Categories() 
    {} 

    public Categories(String id, String name) 
    { 
     this.setId(id); 
     this.setName(name); 
    } 

    @Override 
    public String toString() 
    { 
     return mName; 
    } 

// ******** GET  ************* 
    public String getId() 
    { 
     return mId; 
    } 

    public String getName() 
    { 
     return mName; 
    } 

// ******** SET  ************* 

    public final void setId(String id) 
    { 
     mId = id; 
    } 

    public final void setName(String name) 
    { 
     mName = name; 
    } 
} 

드롭 다운 목록이 숨겨져있을 때 선택한 항목으로 표시되지 않습니다.

어떤 아이디어라도 도움이 될 것입니다! 고맙습니다!!

답변

0

이 경우 CustomAdapter를 작성해야합니다. ListView에 대한 CustomAdapter를 작성하는 것과 비슷합니다. Custom Adapter for List View 아이디어를보실 수 있습니다

+0

대단히 감사합니다! 정말 잘됐다! – josher93

관련 문제