このコードは私にとってうまく機能しています。あなたにも役立つことを願っています。
item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="1dip" >
<TextView
android:id="@+id/spinnerItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="12sp" >
</TextView>
</RelativeLayout>
details.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="20dp"
android:background="#FF00FF">
<Spinner
android:id="@+id/dropStatus"
android:layout_width="250dp"
android:layout_height="30dp"
android:layout_marginBottom="7dp"
android:drawSelectorOnTop="true"/>
</LinearLayout>
アダプタークラス:
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class StatusAdapter extends ArrayAdapter<StatusBean> {
private Context context;
private ArrayList<StatusBean> statuses;
public Resources res;
StatusBean currRowVal = null;
LayoutInflater inflater;
public StatusAdapter(Context context,
int textViewResourceId, ArrayList<StatusBean> statuses,
Resources resLocal) {
super(context, textViewResourceId, statuses);
this.context = context;
this.statuses = statuses;
this.res = resLocal;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.status_item, parent, false);
currRowVal = null;
currRowVal = (StatusBean) statuses.get(position);
TextView label = (TextView) row.findViewById(R.id.spinnerItem);
if (position == 0) {
label.setText("Please select status");
} else {
label.setText(currRowVal.getStatus());
}
return row;
}
}
StatusBeanクラス:
public class StatusBean {
private String status;
private String statusCode;
public StatusBean() {
}
public StatusBean(String status,
String statusCode) {
this.status = status;
this.statusCode = statusCode;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getStatusCode() {
return statusCode;
}
public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}
}
活動クラス:
onCreateメソッドの内部:
static ArrayList<StatusBean> STATUS_LIST = new ArrayList<StatusBean>();
for(int i=0;i<=10;i++) {
STATUS_LIST.add(new StatusBean(“Status ”+i, “Stattus ”+i));
}
final Spinner dropStatus = (Spinner)findViewById(R.id.dropStatus);
Resources res = getResources();
StatusAdapter adapter = new StatusAdapter(this, R.layout.item, SessionData. STATUS_LIST, res);
dropStatus.setAdapter(adapter);