2017-12-08 5 views
-5

여기 안드로이드 개발자 페이지를 참조 : https://developer.android.com/guide/topics/ui/controls/spinner.html#Populate회 전자에 ID를 추가하는 방법은 무엇입니까?

어떻게 id를 전달하고 목록을 선택 때이 ID를 선택 할 수 있습니다? 예 :

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string-array name="planets_array"> 
    <item id=1>Mercury</item> 
    <item id=2>Venus</item> 
    <item id=3>Earth</item> 
    <item id=4>Mars</item> 
    <item id=5>Jupiter</item> 
    <item id=6>Saturn</item> 
    <item id=7>Uranus</item> 
    <item id=8>Neptune</item> 
</string-array> 
</resources> 

그리고 사용자가 항목을 클릭 할 때이 ID를 선택할 수 있습니다.

+0

은 어레이의 위치를 ​​사용합니다. 또는 같은 인덱스에있는 다른 배열의 id를 저장할 수 있습니다 –

+0

배열 정의에'id = n'과 같은 것이 없습니다. 링크 한 페이지를 다시 읽으십시오. –

답변

-2

배열에서 id를 전달할 필요가 없으므로 자동으로 0부터 시작합니다. 이렇게하면 사용할 수 있습니다.

<resources> 
<string-array name="planets_array"> 
    <item>Mercury</item> 
    <item>Venus</item> 
    <item>Earth</item> 
    <item>Mars</item> 
    <item>Jupiter</item> 
    <item>Saturn</item> 
    <item>Uranus</item> 
    <item>Neptune</item> 
</string-array> 
</resources> 
+0

이 목록을 회 전자에 추가 할 수 있으며 회 전자에 설정 한 후 ID 및 목록의 위치를 ​​얻을 수 있습니다. –

-1

사용자 스피너 위젯에서 아이템을 선택하고, 아래의 방법에 대하여 onItemSelected 방법에서, 하나의 위치 값을 볼 수

public class SpinnerActivity extends Activity implements OnItemSelectedListener { 
... 

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 
} 

를}라고한다. 이 위치 값을 사용하여 행성 문자열 리소스 파일에서 선택한 항목의 위치를 ​​가져올 수 있습니다.

String[] some_array = getResources().getStringArray(R.array.planets_array) 

값은

value = some_array[position value from the onItemSelected method ]; 

은 또한 영업 이익이이 질문을 참조 할 수있다. 희망이 도움이됩니다.

Can I give an ID to an item in String-array?

-1

당신은 당신을 위해 ID와 드롭 다운 항목을 개최한다 스피너 어댑터와 사용자 정의 개체를 사용할 수 있습니다. 예를 들면 : 당신이 데이터 ArrayList을 회 전자와 당신이 어댑터 클래스를 사용할 수 있습니다

public class PlanetData { 
    int id; 
    String planetName; 

    public void setId(int id) { 
     this.id = id; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setPlanetName(String planetName) { 
     this.planetName = planetName; 
    } 

    public String getPlanetName() { 
     return planetName; 
    } 

    // We will use this method to get the expand collapse string value of spinner. 
    @Override 
    public String toString() { 
     return planetName; 
    } 
} 

:

는이 같은 사용자 정의 클래스가 어떻게 보일지입니다.

import android.content.Context; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.support.annotation.NonNull; 
import android.support.v4.content.res.ResourcesCompat; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

import java.util.List; 

/** 
* <p> 
* Created by Angad Singh on 6/11/17. 
* </p> 
*/ 

public class TransactionSpinnerAdapter<T> extends ArrayAdapter<T> { 
    private Context context; 
    private List<T> objects; 

    public TransactionSpinnerAdapter(@NonNull Context context, int resource, @NonNull List<T> objects) { 
     super(context, resource, objects); 
     this.context = context; 
     this.objects = objects; 
    } 

    @NonNull 
    public TextView getView(int position, View convertView, @NonNull ViewGroup parent) { 
     TextView v = (TextView) super.getView(position, convertView, parent); 
     v.setText(objects.get(position).toString()); 
     return v; 
    } 

    public TextView getDropDownView(int position, View convertView, @NonNull ViewGroup parent) { 
     TextView v = (TextView) super.getView(position, convertView, parent); 
     v.setText(objects.get(position).toString()); 
     return v; 
    } 
} 

당신 Activity/Fragment 클래스에서, 당신은 RecyclerView/ListView에 사용할 같은 방법으로 ArrayList를 사용합니다.

// List of Planets 
ArrayList<PlanetData> planets = new ArrayList<>(); 
//... 
//... 

PlanetData planet1 = new PlanetData(); 
planet1.setId(1); 
planet1.setPlanetName("Mercury"); 
//... 
//... Create rest of your planets similarly. 
//... 

planets.add(planet1); 

그리고 콜백 번호 OnItemSelectedListener의 콜백은 목록의 위치에서 행성 ID를 가져옵니다.

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
    int id = planets.get(pos).getId(); 
} 
관련 문제