我以编程方式创建了Spinner
并将其添加到ListView的适配器中。但是,我无法从Spinner
获取ListView
。
//listView = (NestedListView)findViewById(R.id.exListView);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Spinner spinner;
for (int i=0; i<listView.getChildCount(); i++) {
if (listView.getChildAt(i) instanceof Spinner){
spinner = (Spinner) listView.getChildAt(i);
String selection = (String) spinner.getSelectedItem();
Log.e("spinner device", selection);
}
}
}
});
我的适配器
public class ExpListAdapter extends BaseExpandableListAdapter {
private ArrayList<ArrayList<String>> mGroups;
private ArrayList<DeviceObject> deviceObList;
private ArrayList<RoomSuggestion> roObjList;
private Context mContext;
public ExpListAdapter (Context context,ArrayList<ArrayList<String>> groups, ArrayList<DeviceObject> deviceObList, ArrayList<RoomSuggestion> roObjList){
mContext = context;
mGroups = groups;
this.deviceObList = deviceObList;
this.roObjList = roObjList;
}
@Override
public int getGroupCount() {
return mGroups.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mGroups.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return mGroups.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mGroups.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_view, null);
}
if (isExpanded) {
//Изменяем что-нибудь, если текущая Group раскрыта
} else {
//Изменяем что-нибудь, если текущая Group скрыта
}
Typeface lightFace = Typeface.createFromAsset(mContext.getAssets(), "font/GothamProLight.ttf");
TextView textGroup = (TextView) convertView.findViewById(R.id.textGroup);
textGroup.setTypeface(lightFace);
textGroup.setText("Thereses gate 46");
return convertView;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_view, null);
}
Typeface mediumFace = Typeface.createFromAsset(mContext.getAssets(), "font/GothamProMedium.ttf");
TextView textChild = (TextView) convertView.findViewById(R.id.textChild);
textChild.setTypeface(mediumFace);
textChild.setText( mGroups.get(groupPosition).get(childPosition) );
RelativeLayout rl = (RelativeLayout) convertView.findViewById(R.id.bg_button_screen);
if( !deviceObList.get(childPosition).getProduct_id().equals("0") ) {
rl.setBackgroundColor(Color.parseColor("#4fcc54"));
} else {
rl.setBackgroundColor(Color.parseColor("#e5910d"));
}
View linearLayoutG = convertView.findViewById(R.id.container);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 0, 10, 30);
linearLayoutG.setLayoutParams(layoutParams);
RelativeLayout spinnerOpen = (RelativeLayout) convertView.findViewById(R.id.spinnerOpen);
View linearLayout = convertView.findViewById(R.id.spinnerL);
ImageView imageS = (ImageView)convertView.findViewById(R.id.spinnerImage);
imageS.getLayoutParams().width = 20;
imageS.getLayoutParams().height = 20;
imageS.setImageResource(R.drawable.spin_ok);
ArrayList<String> list = new ArrayList<String>();
for (int i=0; i<roObjList.size(); i++) {
list.add(roObjList.get(i).getName() );
}
final Spinner spinner = new Spinner(mContext);
// Make sure you have valid layout parameters.
spinner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100));
spinner.setBackgroundResource(R.drawable.bg_spinner);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(mContext,
R.layout.spinner_item, list);
spinner.setAdapter(spinnerArrayAdapter);
// open spinner
spinnerOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
spinner.performClick();
}
});
((LinearLayout) linearLayout).addView(spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.e("selected", String.valueOf(parent.getItemAtPosition(position)) );
Log.e("childPosition", String.valueOf(childPosition));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
答案 0 :(得分:0)
您可以尝试以下代码:
//listView = (NestedListView)findViewById(R.id.exListView);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0; i < listView.getAdapter().getCount(); i++ ) {
for(int i = 0; i < listView.getChildCount(); i++ ) {
View _mainView = listView.getChildAt(i);
LinearLayout _linearLayout = _mainView.findViewById(R.id.spinnerL);
Spinner spinner = (Spinner) _linearLayout.getChildAt(0);
String selection = (String) spinner.getSelectedItem();
Log.e("spinner device", selection);
}
}
}
});