When I execute the code below, I would like to get text="All"
, as its visible value is that, but all the time I'm getting
String text = "android.database.MergeCursor@88a4aaa"
The code:
String[] from = new String[]{"type"};
int[] to = new int[]{android.R.id.text1};
Cursor cursorTypes = coctelsOpenHelper.getTypes();
//Add "All" option to the spinner
MatrixCursor matrixCursor = new MatrixCursor(new String[] { "_id", "type"});
matrixCursor.addRow(new Object[] { "0", getString(R.string.title_all_drinks) });
MergeCursor mergeCursor = new MergeCursor(new Cursor[] { matrixCursor, cursorTypes });
spinnerType = view.findViewById(R.id.spinnerDrinkType);
adapter = new SimpleCursorAdapter(getContext(), android.R.layout.simple_spinner_item, mergeCursor, from, to,0);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerType.setAdapter(adapter);
spinnerType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//When I try to debug, the text value is the given above, a MergeCursor
text = ((String)spinnerType.getSelectedItem().toString());
selectedItem = position;
addSelection();
//This ones don't have anything in common with the adapter from the spinner, are for a RecyclerView
initializeData();
initializeAdapter();
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
And no, "text" is not being used anymore, so it's value hasn't changed, and I've also tried with
spinnerType.getSelectedItem().toString();
but got same result.
All I wanted to do, is get the string value of the text on the Spinner, and compare it to R.string.myvalue
with text.equals(getString(R.string.myvalue))
你的变量类型是什么?
Can you try like this
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val adapter =
ArrayAdapter(this, android.R.layout.simple_spinner_item, listOf("1", "2", "3"))
spinner.adapter = adapter
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
logDebug(spinner.getItemAtPosition(position))
}
}
}
}
Solution: As the problem was the MergeCursor
(I needed it for adding a line to my Cursor), I ended not using a simple cursor adapter
for the spinner, but a ArrayAdapter
, previously adding my custom row with the "all" text, and lately adding the Cursor lines one by one to the ArrayList, and that way I didn't get any problems.
spinnerType = view.findViewById(R.id.spinnerDrinkType);
//Get the different types of drinks
Cursor cursorTypes = coctelsOpenHelper.getTypes();
//Add "All" field to the spinner, for that "all" field is added first to the ArrayList,
// and later each item of the cursor
ArrayList<String> spinnerArray = new ArrayList<>();
spinnerArray.add(getString(R.string.title_all_drinks));
for(cursorTypes.moveToFirst(); !cursorTypes.isAfterLast(); cursorTypes.moveToNext()) {
spinnerArray.add(cursorTypes.getString(1));
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(
getContext(), android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerType.setAdapter(adapter);
spinnerType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
text = spinnerType.getSelectedItem().toString();
addSelection();
initializeData();
initializeAdapter();
}
)};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.