gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:22:04 25 4
gpt4 key购买 nike

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

主要 Activity
    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作为

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

关于android - 在listView(android)中获取值微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28528158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com