如何 return 来自 Spinner 适配器的 arrayList
how to return a arrayList from a spinner adapter
我的客户微调器有以下微调器适配器。除了每个项目的不同文本外,我还有不同的字体和背景颜色。我通过传递一个 arrayList<ArrayList<String>>
来指示这一点,它指示每个项目的颜色和文本值 - 内部数组列表代表一个项目。
现在我想通过获取其 id 并拉出正确的 arrayList 来将所选项目数组列表返回到调用 class,但我不确定如何构造此方法的参数。像 spinner.getItemDetails_Arraylist(...);
public class SpinnerAdapter extends ArrayAdapter<ArrayList<String>>{
ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
private Context c;
public SpinnerAdapter (Context context, int textViewResourceId, ArrayList<ArrayList<String>> objectList) {
super(context, textViewResourceId, objectList);
// TODO Auto-generated constructor stub
this.c = context;
this.list =objectList;
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
LayoutInflater inflater=(LayoutInflater) c.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.spinner_custom, parent, false);
TextView textView_item=(TextView)row.findViewById(R.id.textView_teamName);
//Pull team details from the object at position
ArrayList<String> itemDetails = list.get(position);
String itemName = itemDetails.get(0);
String itemAbbreviation = itemDetails.get(1);
String iteme_primaryColor = itemDetails.get(2);
String item_secondaryColor = itemDetails.get(3);
//Update the spinner item controls
textView_team.setText(teamName);
textView_item.setBackgroundColor(Color.parseColor(item_primaryColor));
textView_item.setTextColor(Color.parseColor(item_secondaryColor));
return row;
}
}
您可以在您的微调器上尝试此 setOnItemSelectedListener
并获取所选位置的视图,然后您可以提取您的列表
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long arg3) {
TextView tv=(TextView) view.findViewById(R.id.textView_teamName);
int color_id = tv.getCurrentTextColor(); // get textColor
ColorDrawable cd = (ColorDrawable) tv.getBackground();
int backcolor_id = cd.getColor(); // get background color
String text = tv.getText().toString(); // get the text
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
另一种解法:
如果您仍然有原始的 ArrayList,您可以使用相同的 setOnItemSelectedListener
来获取所选项目的位置,然后您可以从原始列表
中获取它的小列表
ArrayList<String> selected_item = new ArrayList<String>();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long arg3) {
selected_item = originalList.get(position);
// where original list is your main ArrayList<ArrayList>
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
我的客户微调器有以下微调器适配器。除了每个项目的不同文本外,我还有不同的字体和背景颜色。我通过传递一个 arrayList<ArrayList<String>>
来指示这一点,它指示每个项目的颜色和文本值 - 内部数组列表代表一个项目。
现在我想通过获取其 id 并拉出正确的 arrayList 来将所选项目数组列表返回到调用 class,但我不确定如何构造此方法的参数。像 spinner.getItemDetails_Arraylist(...);
public class SpinnerAdapter extends ArrayAdapter<ArrayList<String>>{
ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
private Context c;
public SpinnerAdapter (Context context, int textViewResourceId, ArrayList<ArrayList<String>> objectList) {
super(context, textViewResourceId, objectList);
// TODO Auto-generated constructor stub
this.c = context;
this.list =objectList;
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
LayoutInflater inflater=(LayoutInflater) c.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.spinner_custom, parent, false);
TextView textView_item=(TextView)row.findViewById(R.id.textView_teamName);
//Pull team details from the object at position
ArrayList<String> itemDetails = list.get(position);
String itemName = itemDetails.get(0);
String itemAbbreviation = itemDetails.get(1);
String iteme_primaryColor = itemDetails.get(2);
String item_secondaryColor = itemDetails.get(3);
//Update the spinner item controls
textView_team.setText(teamName);
textView_item.setBackgroundColor(Color.parseColor(item_primaryColor));
textView_item.setTextColor(Color.parseColor(item_secondaryColor));
return row;
}
}
您可以在您的微调器上尝试此 setOnItemSelectedListener
并获取所选位置的视图,然后您可以提取您的列表
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long arg3) {
TextView tv=(TextView) view.findViewById(R.id.textView_teamName);
int color_id = tv.getCurrentTextColor(); // get textColor
ColorDrawable cd = (ColorDrawable) tv.getBackground();
int backcolor_id = cd.getColor(); // get background color
String text = tv.getText().toString(); // get the text
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
另一种解法:
如果您仍然有原始的 ArrayList,您可以使用相同的 setOnItemSelectedListener
来获取所选项目的位置,然后您可以从原始列表
ArrayList<String> selected_item = new ArrayList<String>();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long arg3) {
selected_item = originalList.get(position);
// where original list is your main ArrayList<ArrayList>
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});