How to get string value from multiple Spinner in ListView?

advertisements

I have a ListView, on each row of ListView I have a Spinner. In the MainActivity,that own the above ListView,I have a buttonSubmit I want when the users finish choosing the value on each Spinner of ListView, they press on buttonSubmit and the buttonSubmit's listener will get all the value of each Spinner of ListView and push them into SQLiteDatabase. My problem here is, the buttonSubmit is not located in the class CustomAdapter of the ListView, it locates in the MainActivity that own the ListView. So I don't know how to get the value from multiple Spinners in each row of ListView into something like a String Array when the user click on the buttonSubmit here is my code : * code of the custom adapter class of ListView,which has a Spinner in each row :

public class ChonLopTheoMonAdapter extends ArrayAdapter<MonHocDTO> implements AdapterView.OnItemSelectedListener{
Context context;
int layout;
ArrayList<MonHocDTO> listMonHoc;

ArrayAdapter<String> adapterSpinner;
String[] listLop;

public ChonLopTheoMonAdapter(Context context, int layout, ArrayList<MonHocDTO> listMonHoc) {
    super(context, layout, listMonHoc);
    this.context = context;
    this.layout = layout;
    this.listMonHoc = listMonHoc;
}

public class ViewHolder {
    TextView txtTenMonHoc_ChonMon;
    Spinner spChonLop;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewHolder holder;
    if (convertView == null) {
        convertView = inflater.inflate(this.layout, parent, false);
        holder = new ViewHolder();
        holder.txtTenMonHoc_ChonMon = (TextView) convertView.findViewById(R.id.txtTenMonHoc_ChonMon);
        holder.spChonLop = (Spinner) convertView.findViewById(R.id.spChonLop);

        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtTenMonHoc_ChonMon.setText(this.listMonHoc.get(position).getTenMonHoc());

    switch (this.listMonHoc.get(position).getIdMonHoc()){
        case "QTM":
            listLop = this.context.getResources().getStringArray(R.array.QTM);
            break;
        case "CTDL":
            listLop = this.context.getResources().getStringArray(R.array.CTDL);
            break;
        case "TCC":
            listLop = this.context.getResources().getStringArray(R.array.TCC);
            break;
    }
    adapterSpinner = new ArrayAdapter<String>(this.context,android.R.layout.simple_spinner_item,listLop);
    adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    holder.spChonLop.setAdapter(adapterSpinner);

    holder.spChonLop.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

    return convertView;
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}

}

*Code of the MainActivity that own the listView :

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ListView lvChonLopTheoMonHoc;
Button btnXacNhanChonLop;

MonHocDAO monHocDAO;

ArrayList<MonHocDTO> listMonHoc;
ChonLopTheoMonAdapter chonLopTheoMonAdapter;

TypedArray thuTuMonHoc;
String[] tenMonHoc;

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

private void addControls() {
    monHocDAO = new MonHocDAO(MainActivity.this);
    lvChonLopTheoMonHoc = (ListView) findViewById(R.id.lvChonLopTheoMonHoc);
    btnXacNhanChonLop = (Button) findViewById(R.id.btnXacNhanChonLop);
    listMonHoc = new ArrayList<>();
    listMonHoc = monHocDAO.layListMonHoc("NAM11");

    chonLopTheoMonAdapter = new ChonLopTheoMonAdapter(MainActivity.this, R.layout.layout_chon_mon_hoc_theo_lop, listMonHoc);
    chonLopTheoMonAdapter.notifyDataSetChanged();
    lvChonLopTheoMonHoc.setAdapter(chonLopTheoMonAdapter);

    btnXacNhanChonLop.setOnClickListener(MainActivity.this);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btnXacNhanChonLop:
            break;
    }
}

}

please help me :(

*Here is the UI: enter image description here


Your architecture is kinda broken. You don't save the data anywhere, so there really is no way to retrieve it all. Your data model needs to reflect the data that is displayed in your ListView and when the user changes the data (by selecting something in a Spinner), you need to react to the onItemSelected() callback by updating your data model with the item the user selected. Assuming that the Activity passes a reference to the data into your adapter, the Activity has access to the updated data all the time.