获取列表视图中的值微调器(android)

Get values spinner in listView(android)

我为我的英语道歉。我想了几天如何在 ListView 中制作一个微调器,我能够做到。但是现在我不知道如何从微调器中获取数据。我问请告诉我如何从 ListView

中的每个微调器中获取数据

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
    <ListView
        android:id="@+id/lvCustomList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

loyout_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"
    android:padding="10dp">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

MainActivity

    ListView lvDetail;
    Button btn;
    Context context = MainActivity.this;
    ArrayList myList = new ArrayList();

    String[] nameQuestions = new String[] {
            "question1", "question2", "questions3",
            "questions4", "questions5"
    };

    ArrayList<HashMap<String, String>> listArray = new ArrayList<HashMap<String, String>>();

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

        lvDetail = (ListView) findViewById(R.id.lvCustomList);
        btn = (Button) findViewById(R.id.sendQuestions);

        getDataInList();
        lvDetail.setAdapter(new MyBaseAdapter(context, myList));

        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //how get value all spinners?
            }
        });
    }

    private void getDataInList() {
        for(int i=0;i<4;i++) {
            ListData ld = new ListData();
            ld.setQuestions(nameQuestions[i]);
            myList.add(ld);
        }
    }


}

列表数据

public class ListData {

    String questions;

    public String getQuestions() {
        return questions;
    }

    public void setQuestions(String questions) {
        this.questions = questions;
    }


}

MyBaseAdapter

public class MyBaseAdapter extends BaseAdapter {

    ListData ld = new ListData();

    ArrayList<ListData> myList = new ArrayList<ListData>();
    LayoutInflater inflater;
    Context context;
    String cardStatusString;

    ArrayList<String> spinnerArray = new ArrayList<String>();


    public MyBaseAdapter(Context context, ArrayList myList) {
        this.myList = myList;
        this.context = context;
        inflater = LayoutInflater.from(this.context);

        spinnerArray.add("1");
        spinnerArray.add("2");
        spinnerArray.add("3");
        spinnerArray.add("4");
        spinnerArray.add("5");
    }

    @Override
    public int getCount() {
        return myList.size();
    }

    @Override
    public ListData getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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

        if(convertView == null) {
            convertView = inflater.inflate(R.layout.layout_list_item, null);
            mViewHolder = new MyViewHolder();
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }

        mViewHolder.sp = spinnerDetails(convertView, R.id.spinner, myList.get(position).getQuestions());
        mViewHolder.nameText = nameDetails(convertView, R.id.nameText, myList.get(position).getQuestions());

        return convertView;
    }


    private TextView nameDetails(View v, int resId, String text) {
        TextView nameText = (TextView) v.findViewById(resId);
        nameText.setText(text);
        return nameText;
    }

    //this is create spinner
    private Spinner spinnerDetails(View v, int resId, String text) {
        Spinner mySpinner = (Spinner) v.findViewById(resId);

        @SuppressWarnings({ "unchecked", "rawtypes" })
        ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(context,
                android.R.layout.simple_spinner_dropdown_item,
                    spinnerArray);
        mySpinner.setAdapter(spinnerArrayAdapter);

        return mySpinner;
    }



    private class MyViewHolder {
        Spinner sp;
        TextView nameText;
    }

}

您可以遍历 ListView 并将所有选择收集到 List as

btn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

      List<String> selections = new ArrayList<String>();
      for (int i = 0; i < lvDetail.getChildCount(); i++) {

        // Get row's spinner
        View listItem = lvDetail.getChildAt(i);
        Spinner spinner = (Spinner) listItem.findViewById(R.id.spinner);

        // Get selection
        String selection = (String) spinner.getSelectedItem();
        selections.add(selection);
      }

      // process selections
    }
});

我会说在适配器上添加一个方法,该方法 returns 该值到 activity 其所在的位置,因为除 Ravi 指出的 getChildAt 外,没有其他方法可以获取该值出去。但这可能也需要进一步调整,因为对于 ListViews 屏幕中不可见的元素不会加载(微调器值将重置为默认值),但这取决于您想要什么这对于