目前正在使用spinner处理listview.MY要求是根据listview项目本身中的微调器选择更新listview项目。但是在spinner中执行选择时它会更新所有listview项目。我认为我的问题在于
OnItemSelectedListener
。而且我在加载listview时也弄明白,微调器ItemSelectedListener没有执行任何时间。我不明白我是否已经完成了这个。请帮我找到解决方案。
Spinner OnItemSelected侦听器代码是
viewHolder.description.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//here the position represents the listview item position
Log.e("Position of Data is ", Integer.toString(position));
//here the fullListData.get(pos) get the data from spinner based on selection
datas.set(position, fullSeatList.get(pos));
//here am update the listview item data with selected spinner data
datas.get(position).getDescription());
Log.e("Data in lisr", datas.get(position).getPartNo());
notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
编辑1: 我的适配器类是
public class ListViewAdapter extends BaseAdapter {
Context context;
ArrayList<SeatConfigCustomObjects> datas;
ArrayList<SeatConfigCustomObjects> fullSeatList;
ArrayAdapter<SeatConfigCustomObjects> adapter;
public ListViewAdapter(Context context, ArrayList<SeatConfigCustomObjects> datas, ArrayList<SeatConfigCustomObjects> fullSeatList) {
this.context = context;
this.datas = datas;
this.fullSeatList=fullSeatList;
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView==null){
convertView=((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.seat_config_listview_item,null);
viewHolder=new ViewHolder();
viewHolder.partNo=(TextView)convertView.findViewById(R.id.seat_config_listview_partno);
viewHolder.description=(Spinner)convertView.findViewById(R.id.seat_config_listview_description_spinner);
viewHolder.quantity=(TextView)convertView.findViewById(R.id.seat_config_listview_quantity);
convertView.setTag(viewHolder);
}
else{
viewHolder=(ViewHolder)convertView.getTag();
}
viewHolder.partNo.setText(datas.get(position).getPartNo());
adapter=new ArrayAdapter<SeatConfigCustomObjects>(context,android.R.layout.simple_dropdown_item_1line,fullSeatList);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
viewHolder.description.setAdapter(adapter);
viewHolder.description.setSelection(0, false);
viewHolder.description.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Log.e("Position of Data is ", Integer.toString(position));
datas.set(position, fullSeatList.get(pos));
Log.e("Data in description", datas.get(position).getDescription());
Log.e("Data in lisr", datas.get(position).getPartNo());
notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
viewHolder.quantity.setText(Integer.toString(datas.get(position).getQunatity()));
return convertView;
}
public class ViewHolder{
TextView partNo;
Spinner description;
TextView quantity;
}
}
答案 0 :(得分:0)
在基本适配器类中公开您的数组列表(从主类访问它),然后在按钮单击时将String
添加到列表,然后调用notifyDataSetChanged();
我有如下所示。请根据您的方便改变变量......
viewHolder.description.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
ListViewAdapter <object>; //Adapter class object
<object>.datas.add(""); //-----string,position---//
notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
适配器类:
public class ListViewAdapter extends BaseAdapter {
public ArrayList<SeatConfigCustomObjects> datas;
//-- rest of code--- ///
}