Reputation: 137
I apologize for my English. I thought a few days how to make a spinner in the ListView, and I was able to do it. But now I do not know how to take data from the spinner. I ask please tell me how to take the data from each spinner in 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;
}
}
Upvotes: 2
Views: 2697
Reputation: 356
I would say add a method on the Adapter that returns that value to the activity its in, since there is no other way to get the value otherwise, except with getChildAt as Ravi pointed out. But this may need further tweaking too since for ListViews the elements that are not visible in the screen are not loaded (and the spinner value will reset to default), but that depends on what you want this for
Upvotes: 1
Reputation: 51711
You can iterate over your ListView
and collect all the selections into a 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
}
});
Upvotes: 3