我在“我的活动”中选择了3个Spinner。每个Spinner都以相同的方式填充相同的数据。
让我用下面的例子解释一下。
我的主要列表包含以下数据:a,b,c,d,e等。
所以当我看到Spinner 1时,它应该选择值a(这个值不应该在第二个和第三个微调器中列出,反之亦然)。
E.g。 Spineer 1值:a,d,e(不是b和c) Spineer 2值:: b,d,e(不是a和c) Spineer 3值:: c,d,e(不是a和b)
我的问题是:
当我从微调器1中选择值时,它没有被选中,并且通过查看Android日志,它进入无限状态。 Spinner 2和Spinner 3也会出现同样的问题。
以下是我尝试过的代码。
public class SpinnerDemo extends Activity {
private Spinner sp1, sp2, sp3;
ArrayList<String> MAinArrayList;
ArrayList<String> TempList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final_sp);
sp1 = (Spinner) findViewById(R.id.spinner1);
sp2 = (Spinner) findViewById(R.id.spinner2);
sp3 = (Spinner) findViewById(R.id.spinner3);
TempList = new ArrayList<String>();
MAinArrayList = new ArrayList<String>();
MAinArrayList.add("a");
MAinArrayList.add("b");
MAinArrayList.add("c");
MAinArrayList.add("d");
MAinArrayList.add("e");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, MAinArrayList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// dataAdapter.notifyDataSetChanged();
sp1.setAdapter(dataAdapter);
final String strA = sp1.getSelectedItem().toString();
TempList.addAll(MAinArrayList);
for (int i = 0; i < TempList.size(); i++) {
if (TempList.get(i).equals(strA)) {
TempList.remove(strA);
break;
}
}
for (int i = 0; i < TempList.size(); i++) {
System.out.println("Templist " + (i) + "==>" + TempList.get(i));
}
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, TempList);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// dataAdapter.notifyDataSetChanged();
sp2.setAdapter(dataAdapter2);
// ArrayList<String> TempList1 = new ArrayList<>();
// TempList1.addAll(MAinArrayList);
// if (TempList.size() > 0) {
// TempList.clear();
// }
//TempList.addAll(MAinArrayList);
final String strB = sp2.getSelectedItem().toString();
final ArrayList<String> TempList1 = new ArrayList<>();
TempList1.addAll(TempList);
System.out.println("Templist1 ADDDED" + TempList1);
for (int i = 0; i < TempList1.size(); i++) {
if (TempList1.get(i).equals(strB)) {
TempList1.remove(strB);
break;
}
System.out.println("removed templist " + TempList1);
}
for (int i = 0; i < TempList1.size(); i++) {
System.out.println("TempList3 " + (i) + "==>" + TempList1.get(i));
}
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, TempList1);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// dataAdapter.notifyDataSetChanged();
sp3.setAdapter(dataAdapter3);
sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayList<String> TempSP2 = new ArrayList<String>();
TempSP2.addAll(MAinArrayList);
System.out.println("TempSP2 ==>" + TempSP2);
String strC = sp3.getSelectedItem().toString();
String strA = sp1.getSelectedItem().toString();
for (int i = 0; i < TempSP2.size(); i++) {
if (TempSP2.get(i).equals(strC)) {
TempSP2.remove(strC);
break ;
}
}
for (int i = 0; i < TempSP2.size(); i++) {
if (TempSP2.get(i).equals(strA)) {
TempSP2.remove(strA);
break;
}
}
System.out.println("removed TempSP2 strAC" + TempSP2);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SpinnerDemo.this,
android.R.layout.simple_spinner_item, TempSP2);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// dataAdapter.notifyDataSetChanged();
sp2.setAdapter(dataAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
sp3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayList<String> TempSP3 = new ArrayList<String>();
TempSP3.addAll(MAinArrayList);
System.out.println("TempSP3 ==>" + TempSP3);
String strA = sp1.getSelectedItem().toString();
String strB = sp2.getSelectedItem().toString();
for (int i = 0; i < TempSP3.size(); i++) {
if (TempSP3.get(i).equals(strA)) {
TempSP3.remove(strA);
break;
}
}
for (int i = 0; i < TempSP3.size(); i++) {
if (TempSP3.get(i).equals(strB)) {
TempSP3.remove(strB);
break;
}
}
System.out.println("removed TempSP3 strAb" + TempSP3);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SpinnerDemo.this,
android.R.layout.simple_spinner_item, TempSP3);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// dataAdapter.notifyDataSetChanged();
sp3.setAdapter(dataAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ArrayList<String> TempSP1 = new ArrayList<String>();
TempSP1.addAll(MAinArrayList);
System.out.println("TempSP1 ==>" + TempSP1);
String strB = sp2.getSelectedItem().toString();
String strC = sp3.getSelectedItem().toString();
for (int i = 0; i < TempSP1.size(); i++) {
if (TempSP1.get(i).equals(strB)) {
TempSP1.remove(strB);
break;
}
}
for (int i = 0; i < TempSP1.size(); i++) {
if (TempSP1.get(i).equals(strC)) {
TempSP1.remove(strC);
break;
}
}
System.out.println("removed TempSP1 strBC" + TempSP1);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SpinnerDemo.this, android.R.layout.simple_spinner_item, TempSP1); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// dataAdapter.notifyDataSetChanged();
sp1.setAdapter(dataAdapter);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}