繁体   English   中英

为什么我得到的是“mergeCursor”对象而不是“字符串”?

[英]Why am I getting a "mergeCursor" object instead of a "string"?

当我执行下面的代码时,我想得到text="All" ,因为它的可见值是那个,但我一直都在得到

String text = "android.database.MergeCursor@88a4aaa"

编码:

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)
            {
            }
        });

不,“文本”不再被使用,所以它的价值没有改变,我也试过

spinnerType.getSelectedItem().toString();

但得到了相同的结果。

我想要做的就是获取 Spinner 上文本的字符串值,并将其与R.string.myvaluetext.equals(getString(R.string.myvalue))

你的变量类型是什么?

你可以这样试试吗

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))
            }

        }

    }
}

解决方案:由于问题是MergeCursor (我需要它来向我的 Cursor 添加一行),我结束时没有为微调器使用一个simple cursor adapter ,而是一个ArrayAdapter ,之前添加了带有“所有”文本的自定义行,并且最近将 Cursor 行一一添加到 ArrayList 中,这样我就没有遇到任何问题。

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();
        }
)};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM