我试图从spinner获取使用getter和setter以及自定义适配器动态生成的值。我能够显示它,但我想为每个微调器添加onclicklistners并添加从中获取所选值并将其存储在数组列表中。
到目前为止,这是我的代码 getter setter
public class Baggage_details {
String status;
String sector_name;
String title_count;
ArrayList<String> baggage_list;
public String getstatus() {
System.out.println("----- : "+status);
return status;}
public void setstatus(String status) {this.status = status;}
public String getsector_name() {
System.out.println("----- : "+sector_name);
return sector_name;}
public void setsector_name(String sector_name) {this.sector_name = sector_name;}
public String gettitle_count() {
System.out.println("----- : "+title_count);
return title_count;
}
public void settitle_count(String title_count) {this.title_count = title_count;}
public ArrayList getbaggage_list() {
return baggage_list;
}
public void setbaggage_list(ArrayList<String> baggage_list) {this.baggage_list = baggage_list;}
public Baggage_details(String status,String sector_name,String title_count,ArrayList<String> baggage_list)
{
this.status = status;
this.sector_name = sector_name;
this.title_count=title_count;
this.baggage_list=baggage_list;
}
}
自定义适配器
public class Baggage_Adapter extends BaseAdapter {
Context context;
public static ArrayList<Baggage_details> rowItems;
public static ArrayList<String> selected_baggage=new ArrayList<String>();
public static ArrayList<String> selected_baggage_final=new ArrayList<String>();
Baggage_Adapter(Context context, ArrayList<Baggage_details> rowItems) {
selected_baggage.clear();
selected_baggage_final.clear();
this.context = context;
this.rowItems = rowItems;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
private class ViewHolder {
TextView sector,status;
TextView title_count;
Spinner spinner;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.baggage_adapter, null);
holder = new ViewHolder();
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.sector = (TextView) convertView.findViewById(R.id.sector);
holder.title_count = (TextView) convertView.findViewById(R.id.title_count);
holder.spinner = (Spinner) convertView.findViewById(R.id.spinner);
try
{
final Baggage_details row_pos = rowItems.get(position);
String status = String.valueOf(row_pos.getstatus());
String title_count = String.valueOf(row_pos.gettitle_count());
String sector=String.valueOf(row_pos.getsector_name());
holder.status.setText(status);
holder.sector.setText(sector);
holder.title_count.setText(title_count);
ArrayAdapter<String> adapter1;
adapter1 = new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item,row_pos.getbaggage_list());
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.spinner.setAdapter(adapter1);
}
catch (Exception e)
{
Log.e("PASS_ADAP ERROR:", e.getMessage());
}
convertView.setTag(holder);
holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String Text = parent.getSelectedItem().toString();
String key = holder.title_count.getText().toString()+"!"+holder.sector.getText().toString();
if(Text.equals("Select") )
{
selected_baggage.add(key);
selected_baggage_final.add(Text);
}
else{
for (int i = 0; i <selected_baggage.size() ; i++)
{
if(selected_baggage.get(i).equals(key))
{
selected_baggage.set(i,key);
selected_baggage_final.set(i,Text);
}
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return convertView;
}
}
以下是我的称呼方式
ArrayList<String> omword = new ArrayList<String>();
omword.add("No Baggage");
omword.add("123");
omword.add("456");
for (int i = 0; i < 2; i++)
{
bagdetails.add(new Baggage_details("going","BNG","key-"+(i+1),omword));
baggageAdapter=new Baggage_Adapter(getContext(),bagdetails);
}
答案 0 :(得分:0)
您正在使用微调器选择项目,因此您必须在适配器holder.spinner.getSelectedItem().toString();
现在改变这个: -
holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String Text = holder.spinner.getSelectedItem().toString();
String key = holder.title_count.getText().toString()+"!"+holder.sector.getText().toString();
if(Text.equals("Select") )
{
selected_baggage.add(key);
selected_baggage_final.add(Text);
}
else{
for (int i = 0; i <selected_baggage.size() ; i++)
{
if(selected_baggage.get(i).equals(key))
{
selected_baggage.set(i,key);
selected_baggage_final.set(i,Text);
}
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});