在listView中获取值微调器(android)

时间:2015-02-15 16:16:43

标签: android listview baseadapter

我为我的英语道歉。我想了几天如何在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);
        }
    }


}

的ListData

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;
    }

}

2 个答案:

答案 0 :(得分:2)

您可以迭代ListView并将所有选择收集到List

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

答案 1 :(得分:1)

我想在Adapter上添加一个方法,将该值返回给它所在的活动,因为没有其他方法可以获取该值,除非使用getChildAt作为Ravi指出。但是这可能还需要进一步调整,因为对于ListViews ,未加载屏幕中不可见的元素(并且微调器值将重置为默认值),但这取决于您对此的要求< / p>