我有一个名为ArrayAdapter
的{{1}},其SpinnerAdapter
作为其布局。此外,它还需要android.R.layout.simple_spinner_item
ArrayList
个ContactInfo
个对象才能显示。
然后我有一个名为Spinner
的{{1}},当选择""是;它从前面提到的selectionWheel
中提取ContactInfo
个对象,并将其添加到新的ArrayList
。
现在我第二次提起ArrayList
;我想检查两个Spinner
之间的匹配项,如果匹配则通过更改其背景颜色突出显示ArrayLists
中的相应项目。
以下是我的尝试:
Spinner
然而,这并没有达到我所描述的预期结果,而且我不确定从这里去哪里......
答案 0 :(得分:0)
这就是我所做的
package com.example.click;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity{
Spinner mySpinner;
ArrayList<String> check = new ArrayList<String>();
String[] myArray={"1","2","3","4","5"};
String[] newArray={};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySpinner = (Spinner)findViewById(R.id.spinner1);
mySpinner.setAdapter(new MyAdapter(this,android.R.layout.simple_spinner_item, myArray));
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(check.contains(myArray[arg2])){
check.remove(myArray[arg2]);
}
else{
check.add(myArray[arg2]);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt){
LayoutInflater inflater = getLayoutInflater();
View spinnerItem = inflater.inflate(android.R.layout.simple_spinner_item, null);
TextView mytext= (TextView)spinnerItem.findViewById(android.R.id.text1);
mytext.setText(myArray[position]);
if(check.contains(myArray[position])){
spinnerItem.setBackgroundColor(Color.GREEN);
}
return spinnerItem;
}
}
}