2016-11-16 42 views
2

我有一個ListView,ListView的每一行我有一個微調。 在擁有上述ListView的MainActivity中,我有一個buttonSubmit 我希望當用戶完成在每個ListView的Spinner上選擇值時,他們按下buttonSubmit並且buttonSubmit的監聽器將獲得ListView的每個Spinner的所有值,並且將它們推入SQLiteDatabase。 我的問題在於,buttonSubmit不在ListView的類CustomAdapter中,它位於擁有ListView的MainActivity中。 所以我不知道如何從ListView的每一行中的多個Spinners獲取值到類似字符串數組的東西當用戶點擊 buttonSubmit 這裏是我的代碼: *自定義適配器類的ListView的代碼,其中有各行中的微調:如何從ListView中的多個微調器獲取字符串值?

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) { 

} 

}

*在MainActivity的代碼擁有的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; 
    } 
} 

}

請幫我:(

*以下是UI: enter image description here

回答

0

你的架構是有點破。您不會將數據保存到任何地方,因此實際上無法將其全部檢索。您的數據模型需要反映ListView中顯示的數據,並且當用戶更改數據時(通過選擇Spinner中的某些內容),您需要通過用用戶選擇的項目更新數據模型來回應onItemSelected()回調。假設Activity將對數據的引用傳遞到適配器,則Activity可以始終訪問更新的數據。

+1

大衛你好,非常感謝你幫助我,我解決了我的問題。 再次,謝謝!!!!!! ^^ –

0

onItemSelected()使用此

spinner.getSelectedItem().toString(); 

獲得所選擇的項目。要能夠使用MainActvity中的值,請嘗試使用SharedPreferences來保存所選項目的值。

+0

感謝您的幫助朋友:) –

+0

@ViệtDũngLê隨時:) –

相關問題