如何隐藏当前在一个微调器中选择的其他微调器中的项目?
我尝试通过ArrayList中的字符串和ArrayAdapters删除项目,但是我注意到一旦从列表中删除它,选择就不再引用列表项(因为它不再存在)。
现在假设,我有4个动态创建的微调器,它们都具有与其资源相同的ArrayList,现在我想使用此适配器从1个微调器中获取所选项的位置,然后将其从3中隐藏其他纺纱厂。
for (int i = 0; i < numberOfStops; i++) {
AddStopView stopView = new AddStopView(getActivity());
stopView.setCallback(BaseBookingFragment.this);
stopView.setPassengerNames(extraPassengerNames);
stopViews.add(stopView);
parent.addView(stopView, viewPosition);
}
在上面的代码中,我动态创建停止视图,每个停止视图都有乘客名称微调器。这些所有的微调器都有与它们的资源相同的ArrayList。
来自AddStopView.java的代码
public AddStopView(Context context) {
super(context);
initialize();
}
public void setCallback(StopViewCallback callback) {
this.callback = callback;
}
public void setPassengerNames(List<String> passengerNames) {
this.passengerNames = passengerNames;
passengerAdapter.setNames(passengerNames);
}
private void initialize() {
inflate(getContext(), R.layout.view_stop, this);
passengerAdapter = new ExtraPassengerAdapter(getContext());
passengerAdapter.setNames(passengerNames);
nameSpinner.setAdapter(passengerAdapter);
nameSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (view == null) {
return;
}
passengerName = (String) view.getTag();
if (position != 0)
callback.updatePassengerList(AddStopView.this, (position - 1));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
nameSpinner.setOnItemSelectedListener
的回调代码@Override
public void updatePassengerList(AddStopView addStopView, int position) {
for (String passName : extraPassengerNames) {
if (addStopView.getPassengerName().equals(passName)) {
extraPassengerNames.remove(passName);
break;
}
}
for (AddStopView stopView : stopViews) {
if (!stopView.equals(addStopView))
stopView.setPassengerNames(extraPassengerNames);
}
}
来自ExtraPassengerAdapter.java的代码
public class ExtraPassengerAdapter extends BaseAdapter {
private List<String> names = new ArrayList<>();
private Context context;
public ExtraPassengerAdapter(Context context) {
this.context = context;
names.add(get0Position());
}
public void setNames(List<String> names) {
this.names.clear();
this.names.add(get0Position());
this.names.addAll(names);
notifyDataSetChanged();
}
@Override
public int getCount() {
return names.size();
}
@Override
public String getItem(int position) {
return names.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_stop, parent, false);
String name = getItem(position);
textView.setText(name);
textView.setTag(name);
return textView;
}
private String get0Position() {
return context.getString(R.string.passenger_name);
}
}
答案 0 :(得分:0)
我认为解决问题的正确方法是创建一个包含所有可能选项的列表,并为每个微调器创建4个包含4个适配器的列表。当您选择了某些内容时,您将根据您描述的逻辑更新每个列表,并为每个适配器调用adapter.notifyDataSetChanged()
。