我是非常新的Android,我想在用户点击图片时交换微调器值。请有人帮助我。
这是我的代码:
public class SearchFragment extends Fragment {
ImageView swap;
Spinner source, destination;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.search, container,
false);
swap = (ImageView)getActivity().findViewById(R.id.imgSwap);
source = (Spinner)getActivity().findViewById(R.id.from);
destination = (Spinner)getActivity().findViewById(R.id.to);
swap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
-----> \\Here want to code
}
});
return view;
}
}
答案 0 :(得分:0)
如果要交换两个Spinners的内容,可以使用此
ArrayList<String> list1 = new ArrayList<>();
ArrayList<String> list2 = new ArrayList<>();
list1.add("A");
list1.add("B");
list1.add("C");
list2.add("1");
list2.add("2");
list2.add("3");
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,list1);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,list2);
spinner1.setAdapter(adapter1);
spinner2.setAdapter(adapter2);
yourImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayAdapter<String> a1 = (ArrayAdapter) spinner1.getAdapter();
ArrayAdapter<String> a2 = (ArrayAdapter) spinner2.getAdapter();
spinner1.setAdapter(a2);
spinner2.setAdapter(a1);
}
});
或者,如果您只想在微调器上交换选定的值,可以在按钮中单击
yourImagebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position1 = spinner1.getSelectedItemPosition();
int position2 = spinner2.getSelectedItemPosition();
String item1 = list1.get(position1);
String item2 = list2.get(position2);
list1.remove(position1);
list1.add(position1, item2);
list2.remove(position2);
list2.add(position2, item1);
adapter1.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
}
});