我正在尝试在视图模式下设置选定的微调器项,但.equals方法每次都返回false,即使两个字符串相等。
我试过了:
spinner.getItemAtPosition(ⅰ).getItemAtPosition(ⅰ).equals(myString的)
spinner.getItemAtPosition(ⅰ).equalsIgnoreCase(myString的)
spinner.getItemAtPosition(ⅰ)的ToString()。equalsIgnoreCase(myString的)
spinner.getItemAtPosition(ⅰ)的ToString()。修剪()。equalsIgnoreCase(myString的)
spinner.getItemAtPosition(ⅰ)的ToString()。修剪()。equalsIgnoreCase(myString.trim())
spinner.getItemAtPosition(ⅰ)的ToString()。修剪()。equalsIgnoreCase(myString.toString()。修剪())
在所有情况下,它只返回假..我哪里出错了?
这是将所选项目设置为微调器的代码:
my_spinner.setSelection(getSpinnerIndex(my_spinner, "myString"));
这是我的获取Spinner指数方法:
private int getSpinnerIndex(Spinner spinner, String myString) {
int index = 0;
for (int i = 0; i < spinner.getCount(); i++) {
if (spinner.getItemAtPosition(i).toString().trim().equalsIgnoreCase(myString.trim())) {
index = i;
break;
}
}
return index;
}
当我检查线路时:
spinner.getItemAtPosition(ⅰ)的ToString()。修剪()
结果是= myString
当我检查线路时:
的myString
结果是= myString
但是这一行:
if(spinner.getItemAtPosition(i).toString()。trim()。equalsIgnoreCase(myString.toString()。trim())){
返回false ??
编辑:
这是我的String Array:
String[] allStrings = {"myString", "string", "myNewString"};
我的适配器代码:
class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<MyDomain> myList = new ArrayList<>();
MyAdapter(List<MyDomain> myList) {
NeedRepairActivity.this.myList = myList;
mInflater = (LayoutInflater.from(NeedRepairActivity.this));
}
@Override
public int getCount() {
return myList.size();
}
@Override
public MyDomain getItem(int position) {
return myList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder viewHolder = new ViewHolder();
if (view == null) {
view = mInflater.inflate(R.layout.each_item, null);
// getting all view here
}
return view;
}
private class ViewHolder {
ImageView myImage;
TextView myComment;
TextView myAmount;
TextView myName;
TextView myChoice;
}
}
这里我试图在自定义对话框中显示所选项目,如:
// on click on list item
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
MyDomain domain = (MyDomain) myListView.getAdapter().getItem(position);
dialog = new MyCustomDialog(MyActivity.this, domain);
dialog.show();
}
});
这是我的自定义对话框类,用于在视图模式下显示选定的微调器项目:
public class MyCustomDialog extends Dialog implements View.OnClickListener {
Context context;
MyDomain MyDomain1 = null;
public MyCustomDialog(Context context) {
super(context);
this.context = context;
}
public MyCustomDialog(Context context, MyDomain myDomain) {
super(context);
this.context = context;
this.myDomain1 = myDomain;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_layout);
ArrayAdapter<String> adp = new ArrayAdapter<>(MyActivity.this, android.R.layout.simple_spinner_item, allStrings);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adp);
// set selected items if comes from edit mode
if (myDomain1 != null) {
if (!TextUtils.isEmpty(myDomain1.subType)) {
mySpinner.setSelection(getSpinnerIndex(mySpinner, MyDomain1.subType));
subCatName = MyDomain1.subType;
setSpinnerText(mySpinner, myDomain1.subType); // assume this is "myString" here
}
}
}
/* private void setSpinnerText(final Spinner spin, String selectedText) {
try {
for (int i = 0; i < spin.getAdapter().getCount(); i++) {
if (spin.getAdapter().getItem(i).toString().equals(selectedText)) { // here getting selectedText as myString and spin.getAdapter().getItem(i).toString() as myString but //returning false
spin.setSelection(i);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}*/
private int getSpinnerIndex(Spinner spinner, String myString) {
int index = 0;
try {
for (int i = 0; i < spinner.getCount(); i++) {
String spinnerItem = spinner.getItemAtPosition(i).toString();
if (spinnerItem.equalsIgnoreCase(myString)) { // here getting myString as "myString" and spin.getAdapter().getItem(i).toString() as "myString" but returning false
index = i;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return index;
}
}
提前致谢。