java - 无法从 ListView 获取值微调器

标签 java android android-layout android-listview

抱歉我的英语不好。我花了很多次尝试解决我的问题,但我做不到。我有 listView,在此 ListView 中为某些元素创建微调器。在适配器中创建的微调器。我无法获得值(value)旋转器。这对我来说很难。请大家帮忙

我的适配器

   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"));

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) {

                    }
                });
            } else {
                rl.setBackgroundColor(Color.parseColor("#e5910d"));
            }





        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

来自适配器的我的 xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/bgchilddevice"
            android:layout_width="match_parent"
            android:layout_weight="0.3"
            android:layout_marginLeft="5dp"
            android:background="#26ffffff"
            android:layout_marginRight="2.5dp"
            android:layout_height="60dp">

            <TextView
                android:id="@+id/textChild"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#fff"
                android:textSize="18dp"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/bg_button_screen"
            android:layout_weight="0.9"
            android:layout_marginRight="5dp"
            android:background="#4fcc54"
            android:layout_width="match_parent"
            android:layout_marginLeft="2.5dp"
            android:layout_height="60dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/check"
                android:id="@+id/imageView6"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:background="#26ffffff"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/spinnerL"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_weight="0.2"
            android:layout_centerHorizontal="true" />

        <RelativeLayout
            android:id="@+id/spinnerOpen"
            android:layout_width="match_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="0.8"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/spinnerImage"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>


    </LinearLayout>

</LinearLayout>

我尝试获取旋转元素

create.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        for(int i = 0; i < listView.getAdapter().getCount(); i++ ) {

                    for(int j = 0; j < listView.getChildCount(); j++ ) {

                        View _mainView = listView.getChildAt(j);

                        LinearLayout _linearLayout = (LinearLayout) _mainView.findViewById(R.id.spinnerL);

                        Spinner spinner = (Spinner) _linearLayout.getChildAt(0);

                        String selection = (String) spinner.getSelectedItem();

                        Log.e("spinner device", selection);
                    }

                }
    }

});

并且我有

致命异常:main java.lang.NullPointerException

行内Spinner spinner = (Spinner) _linearLayout.getChildAt(0);

我尝试写

Spinner spinner = (Spinner) _linearLayout.getChildAt(0);

Spinner spinner = (Spinner) _linearLayout.getChildAt(1);

Spinner spinner = (Spinner) _linearLayout.getChildAt(2);

Spinner spinner = (Spinner) _linearLayout.getChildAt(3);

它的错误

最佳答案

为什么不使用 findViewById() 作为微调器?

Spinner spinner =(Spinner) findViewById(R.id.spinnerL);
String value = spinner.getSelectedItem().toString();

关于java - 无法从 ListView 获取值微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31416265/

相关文章:

java - 一个 int 值中的多个标志

android - 如何访问 gradle 构建文件中其他地方的 buildConfigField

java - 第一个 Hibernate 测试给出错误 [编辑]

android - 从单一构建类型生成使用不同 key 签名的多个构建

android-layout - android ImageView缩放以适合另一个 View

android - 表格布局间距问题

Android对齐布局中心

java - Eclipse 不再启动?

java - 如何编译不包含任何带有main方法的类的java包?

java - jaxb 可以解析系统属性吗?