당신은 당신을 위해 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();
}
은 어레이의 위치를 사용합니다. 또는 같은 인덱스에있는 다른 배열의 id를 저장할 수 있습니다 –
배열 정의에'id = n'과 같은 것이 없습니다. 링크 한 페이지를 다시 읽으십시오. –