Я пытаюсь добавить Spinner в ListView для столбца State
. При работе старт должен быть выбран в качестве первого элемента из ArrayList в адаптере Spinner. После этого onItemSelected
- выбранный пункт. Когда я запускаю упражнение, блок прядильщика пуст и ничего не выбрано ... Попытка onItemSelected
также не устанавливает выбор ...
Пожалуйста, сообщите мне, что я делаю неправильно?
public class RepairListAdapter extends ArrayAdapter<Repair> {
private Context mContext;
private int mResource;
private int lastPosition = -1;
private static class ViewHolder {
TextView workIdTV;
TextView workerUserNameTV;
TextView workOrderTV;
TextView dateOfDamageTV;
TextView damageSpottedByTV;
TextView finishDateTV;
TextView finishedByTV;
TextView priotityTV;
Spinner statusSP;
}
public RepairListAdapter(Context context, int resource, ArrayList<Repair> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
@RequiresApi(api = Build.VERSION_CODES.N)
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//get the repairOrder information
int workId = getItem(position).getWorkId();
String workerUserName = getItem(position).getWorkerUserName();
String workOrder = getItem(position).getWorkOrder();
String dateOfDamage = getItem(position).getDateOfDamage();
String damageSpottedBy = getItem(position).getDamageSpottedBy();
String finishDate = getItem(position).getFinishDate();
String finishedBy = getItem(position).getFinishedBy();
String priority = getItem(position).getPriority();
String status = getItem(position).getStatus();
//create the view result for showing the animation
final View result;
//ViewHolder object
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
holder = new ViewHolder();
holder.workIdTV = (TextView) convertView.findViewById(R.id.workIdTextView);
holder.workerUserNameTV = (TextView) convertView.findViewById(R.id.workerUserNameTextView);
holder.workOrderTV = (TextView) convertView.findViewById(R.id.workOrderTextView);
holder.dateOfDamageTV = (TextView) convertView.findViewById(R.id.dateOfDamageTextView);
holder.damageSpottedByTV = (TextView) convertView.findViewById(R.id.damageSpottedByTextView);
holder.finishDateTV = (TextView) convertView.findViewById(R.id.finishDateTextView);
holder.finishedByTV = (TextView) convertView.findViewById(R.id.finishedByTextView);
holder.priotityTV = (TextView) convertView.findViewById(R.id.priorityTextView);
holder.statusSP = (Spinner) convertView.findViewById(R.id.statusSpinner);
result = convertView;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
result = convertView;
}
//set on click listener for each cell
convertView.findViewById(R.id.workIdTextView).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
//...on click for all cells, without the spinner cell
//animation
Animation animation = AnimationUtils.loadAnimation(mContext,
(position > lastPosition) ? R.anim.load_down_anim : R.anim.load_up_anim);
result.startAnimation(animation);
lastPosition = position;
//set String objects to text
holder.workIdTV.setText(String.valueOf(workId));
holder.workerUserNameTV.setText(workerUserName);
holder.workOrderTV.setText(workOrder);
holder.dateOfDamageTV.setText(dateOfDamage);
holder.damageSpottedByTV.setText(damageSpottedBy);
holder.finishDateTV.setText(finishDate);
holder.finishedByTV.setText(finishedBy);
holder.priotityTV.setText(priority);
//spinner adapter
ArrayAdapter<String> myAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_list_item_1,
//get arrayList
dbManagerRepairState.getNextPossibleState(status));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.statusSP.setAdapter(myAdapter);
holder.statusSP.setSelection(0);
holder.statusSP.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
holder.statusSP.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
return convertView;
}
}
Spinner загружает элементы в ArrayList, но setSelection не отображает выбранную позицию ...