问题
Currently am working on listview with spinner.MY requirement is update the listview item based on spinner selection which is in listview item itself.but while performing the selection in spinner it update the all listview items.I think my problem is with in
OnItemSelectedListener
.And also i figure out while loading listview the spinner ItemSelectedListener executes no of times.I don't understand want i have done on this.Please help me to find the solution for it.
Spinner OnItemSelected Listener code is
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) {
}
});
EDIT 1: My Adapter class is
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;
}
}
回答1:
Make your array-list public (to access it from main class) in base adapter class then add String
to list on button click,and call notifyDataSetChanged();
I have shown it below .Please change variable according to your convenience...
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) {
}
});
Adapter class :
public class ListViewAdapter extends BaseAdapter {
public ArrayList<SeatConfigCustomObjects> datas;
//-- rest of code--- ///
}
来源:https://stackoverflow.com/questions/32132998/update-the-listview-item-based-on-spinner-selection-in-android