You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

选中Spinner项后无法设置EditText tehsil值问题求助

Hey there! Let's troubleshoot why your Spinner's selected value isn't populating the tehsil EditText. Since the dropdown menu works fine, the issue likely lies in how you're handling the selection listener or referencing your views. Here are the most common fixes to try:

1. Ensure you've set up the OnItemSelectedListener correctly

This is the most frequent culprit—either you forgot to attach the listener to your Spinner, or the logic inside it has gaps. Make sure you're properly overriding the onItemSelected method to grab the selected value and pass it to the EditText.

Example code snippet:

// Get references to your views (make sure this runs after the layout is inflated!)
Spinner yourSpinner = findViewById(R.id.your_spinner_id);
EditText tehsil = findViewById(R.id.tehsil);

// Attach the selection listener
yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // Fetch the selected text from the Spinner
        String selectedValue = parent.getItemAtPosition(position).toString();
        // Set it to the EditText
        tehsil.setText(selectedValue);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // Optional: Handle the case where no item is selected (e.g., clear the EditText)
        tehsil.setText("");
    }
});

2. Double-check your view references

It's easy to make a typo here! Confirm that:

  • The ID you use in findViewById(R.id.tehsil) matches exactly with the android:id="@+id/tehsil" in your XML layout.
  • You're calling findViewById after the layout is inflated (usually inside onCreate for an Activity, or onViewCreated for a Fragment). If you call it too early, the view will be null, and setText won't do anything.

3. Verify your Spinner adapter is working as expected

Even though the dropdown shows items, ensure your adapter's getItem method returns valid text:

  • If using an ArrayAdapter, confirm your data source (like a list of strings) is correctly passed in.
  • For a custom adapter, check that getItem(int position) doesn't return null and returns the text you want to display.

Example ArrayAdapter setup:

List<String> tehsilOptions = Arrays.asList("Option A", "Option B", "Option C");
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, 
    android.R.layout.simple_spinner_item, tehsilOptions);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
yourSpinner.setAdapter(adapter);

4. Check if the EditText is disabled/read-only

If your tehsil EditText has android:enabled="false" or android:focusable="false" set in XML, calling setText won't update its display. To make it show text but prevent user editing, use android:focusableInTouchMode="false" instead (this keeps the text visible but blocks manual input).

内容的提问来源于stack exchange,提问作者AngelCause

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:59:00