如何从Single Layout ArrayAdapters检索小部件

时间:2015-04-01 08:12:49

标签: android android-layout

所以我有一个充满行的Listview,每个行都有一个textview和一个微调器。我使用ArrayAdapter来填充每一行。所有行都有一个布局。

我希望在我可以按下的所有行下方都有一个按钮,以便将所有选择的微调器保存在文本文件中。

我的问题是我不知道如何引用旋转器,因为我使用单一布局方法。我不再拥有唯一的ID:(spinner1,spinner2,spinner3等),因为它们是逐行填充的。

有关如何处理此问题的任何建议,以便我可以引用每个微调器并调用它们的"获取文本"方法?

越多越好,提前谢谢!

1 个答案:

答案 0 :(得分:0)

这就是我要这样做的方法:如果数组的模型对象类型没有当前微调器选择的变量,则需要更新模型或扩展适配器,以便捕获当前选择的该项目的微调器。

在适配器的getView()中,当您创建微调器时,向其添加OnItemSelectedListener,以更新阵列中相应项的模型。

然后按下保存按钮时,只需遍历适配器数组中的项目并获取所有当前的微调器值。

这是一个代码示例:


MainActivity.java

package com.example.spinnerdemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";

    private static final String[] mValues = {
            "Strongly Agree",
            "Agree",
            "Neutral",
            "Disagree",
            "Strongly Disagree"
    };

    public static class MyModel {
        public String text;
        public int spinnerVal;
        public MyModel(String txt, int val) {
            text = txt; spinnerVal = val;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final List<MyModel> items = createItemList(); 

        ListView listView = (ListView) findViewById(R.id.listView1);
        final MyListAdapter adapter = new MyListAdapter(this, items);
        listView.setAdapter(adapter);

        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // here is where we get all the spinner values
                Log.d(TAG, "------- save button clicked");
                for (MyModel model : items) {
                    Log.d(TAG, model.text + " : " + mValues[model.spinnerVal]);
                }
                Log.d(TAG, "-------");
            }
        });
    }

    public static class MyListAdapter extends ArrayAdapter<MyModel> {

        public MyListAdapter(Context context, List<MyModel> objects) {
            super(context, 0, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.list_item, parent, false);
            }

            final MyModel model = (MyModel) getItem(position);

            TextView textView = (TextView) convertView.findViewById(R.id.textView1);
            textView.setText(model.text);

            Spinner spinner = (Spinner) convertView.findViewById(R.id.spinner1);
            spinner.setAdapter(new ArrayAdapter<String>(parent.getContext(),
                    android.R.layout.simple_spinner_dropdown_item,
                    android.R.id.text1, mValues));
            // here is where the spinner gets the value from the model
            spinner.setSelection(model.spinnerVal);
            spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                        int position, long id) {
                    // here is where we update the model with the current position of the spinner
                    model.spinnerVal = position;
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });

            return convertView;
        }

    }

    private List<MyModel> createItemList() {
        List<MyModel> items = new ArrayList<MyModel>();
        // initialize with "Neutral" spinner setting
        items.add(new MyModel("question 1", 2));
        items.add(new MyModel("question 2", 2));
        items.add(new MyModel("question 3", 2));
        items.add(new MyModel("question 4", 2));
        items.add(new MyModel("question 5", 2));
        return items;
    }

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.spinnerdemo.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Save" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

    </ListView>

</RelativeLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>