【问题标题】:How do I get Android Spinner Text to Update?如何获取要更新的 Android 微调器文本?
【发布时间】:2020-10-11 09:55:44
【问题描述】:

我的活动中有一个微调器。

顶部的微调栏用作标题,然后在其下方有一个导航栏,在下半部分有一个片段容器。

微调器包含它通过的所有练习的列表。

单击微调器下拉列表中的一项后,我希望更新微调器文本,但没有发生任何变化...

我收到以下消息:

W/e.exerciseappv: Accessing hidden field Landroid/widget/AbsListView;->mIsChildViewEnabled:Z (greylist, reflection, allowed)

D/OpenGLRenderer: endAllActiveAnimators on 0xc6707410 (DropDownListView) with handle 0xc6e4a5f0

我怎样才能让我的基本微调器工作(或者有人有解决方法)?

活动



import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import com.example.exerciseappv5.Fragments.ExerciseGraphFragment;
import com.example.exerciseappv5.Fragments.RecordExerciseFragment;
import com.example.exerciseappv5.Fragments.RecordExerciseHistoryFragment;
import com.example.exerciseappv5.ViewModels.ChildExerciseViewModel;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class RecordExerciseActivity2 extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    List<String> allChildExerciseNames = new ArrayList<>();
    public static final String PARENT_EXERCISE_ID = "-999";
    public static final String EXTRA_DATE = "com.example.exerciseappv4.EXTRA_DATE";
    public static final String EXTRA_WEEK_DATES = "1";
    public static String EXTRA_JUNCTIONID = "EXERCISE_JUNCTION_ID";
    int parentExerciseID;
    private ChildExerciseViewModel childExerciseViewModel;
    String firstExerciseName;
    String selectedExercise;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_exercise);

        getSupportActionBar().hide();

        Intent intent = getIntent();
        if (intent.hasExtra(PARENT_EXERCISE_ID)) {
            parentExerciseID = Integer.parseInt(intent.getStringExtra(PARENT_EXERCISE_ID));
        }

        BottomNavigationView bottomNav = findViewById(R.id.top_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListener);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container2, new RecordExerciseFragment()).commit();

        //getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_back);
        childExerciseViewModel = ViewModelProviders.of(this).get(ChildExerciseViewModel.class);
        childExerciseViewModel.getChildExerciseNameFromParentID(parentExerciseID).observe(this, this::setChildExerciseName);
        childExerciseViewModel.getAllchildExercisesFromParentID(parentExerciseID).observe(this, this::getAllChildExercisesFromParentID);


        Spinner spinner =  findViewById(R.id.spinner1);
        ArrayList<String> spinnerStringArray = new ArrayList<>();
        //Add your data to your array
        spinnerStringArray.addAll(allChildExerciseNames);
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
               android.R.layout.simple_spinner_dropdown_item, allChildExerciseNames);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerAdapter);
        spinner.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        selectedExercise = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), selectedExercise, Toast.LENGTH_SHORT).show();
        Log.i("spinner item clicked ", selectedExercise);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;

                    switch (item.getItemId()) {
                        case R.id.nav_track:
                            selectedFragment = new RecordExerciseFragment();
                            break;
                        case R.id.nav_history:
                            selectedFragment = new RecordExerciseHistoryFragment();
                            break;
                        case R.id.nav_exercise_list:
                            selectedFragment = new ExerciseGraphFragment();
                            break;
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container2, selectedFragment).commit();
                    return true;
                }
            };

    private void setChildExerciseName(String childExerciseName) {
        firstExerciseName = childExerciseName;
    }

    public String getSelectedExercise() {
        return selectedExercise;
    }

    private void getAllChildExercisesFromParentID(List<String> allChildExercisesReceived) {
        allChildExerciseNames.addAll(allChildExercisesReceived);
    }
}


活动布局 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <View style="@style/Full_Divider"
        android:id="@+id/divider9"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_below="@+id/spinner1"/>

    <View style="@style/Full_Divider"
        android:id="@+id/divider10"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_below="@+id/top_navigation" />


    <FrameLayout
        android:id="@+id/fragment_container2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/divider10" />

    <androidx.appcompat.widget.AppCompatSpinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"

        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:gravity="right"
        android:padding="8dp" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/top_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner1"
        app:itemTextColor="#ffffff"
        app:itemIconTint="#ffffff"
        app:menu="@menu/top_navigation"
        android:background="#292929" />

</RelativeLayout>

build.Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.exerciseappv5"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8

        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

    def lifecycle_version = "1.1.1"
    def room_version = "1.1.1"

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0-rc01'
    implementation 'androidx.cardview:cardview:1.0.0'




    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'android.arch.lifecycle:extensions:1.0.0-alpha1'
    annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'

    implementation 'android.arch.persistence.room:runtime:1.0.0-alpha1'
    annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-alpha1'
}

【问题讨论】:

  • 带有所选练习的 Toast 是否出现?
  • 没有toast没有出现,Log也没有
  • 请在 RecordExerciseActivity2 中添加依赖项 (build.gradle) 和导入 - 还有一个问题:您的设备/模拟器有哪个 Android 版本?
  • 我已添加您的请求。我的设备是 Nexus 6P API 30
  • 这太奇怪了...我设法重现了您的错误:对于 API 级别 30 的设备,选择一个项目对 android.widget.Spinner 没有影响。然后我在布局中添加了另一个 Spinner,这个时间androidx.appcompat.widget.AppCompatSpinner。现在 两个 Spinners 工作,我不能让它们再次“不工作”......即使我删除了 android-Spinner 并清理项目或执行“文件-> 使缓存无效并重新启动”在 AS 上)...我不知道为什么,但我认为在 xml 中从 android.widget.Spinner 切换到 androidx.appcompat.widget.AppCompatSpinner 将解决问题。

标签: java android xml android-layout android-spinner


【解决方案1】:

onItemSelected 中,您可以在日志语句之后执行类似的操作。检查视图是否为微调器类型,如果是,则将选择设置为您单击的位置的索引。

if(view instanceof Spinner) {
  (Spinner)view.setSelection(position);
}

如果您的onItemSelected 仅由您的微调器使用,您可以从onItemSelected 中取出所有代码并直接为您的微调器设置onClickListener,这样就不需要instanceof 检查。

【讨论】:

  • onItemSelectedListener 仅适用于微调器。我的日志没有返回任何内容,toast 也没有返回,所以我认为问题与其他问题有关。
【解决方案2】:

它应该开箱即用。我认为模拟器存在问题,或者项目中的旧依赖项存在问题。也在真实设备上验证。 可能有替代品,我也不喜欢!

例如

// .....
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    selectedExercise = parent.getItemAtPosition(position).toString();
    Toast.makeText(parent.getContext(), selectedExercise, Toast.LENGTH_SHORT).show();
    Log.i("spinner item clicked ", selectedExercise);
    updateAdapter(selectedExercise)
}

// ....
private void updateAdapter(@Nullable final String selected) {
   final List<String> array = new ArrayList<>(allChildExerciseNames);

   if (!TextUtils.isEmpty(selected)) {
       array.remove(selected)
       array.set(0, selected)
}
    
   final Spinner spinner =  findViewById(R.id.spinner1);
   final ArrayAdapter<String> spinnerAdapter = new 
         ArrayAdapter<String>(this,  android.R.layout.simple_spinner_dropdown_item, array);
   

   spinnerAdapter.setDropDownViewResource(
       android.R.layout.simple_spinner_dropdown_item);
   spinner.setAdapter(spinnerAdapter, array);
   spinner.setOnItemSelectedListener(this);
}
//...

附言。您可以通过将它们放置为Activity 类状态来摆脱上面的一些变量。此解决方案是根据您的要求添加的,作为替代方案。至少你可以使用它,直到我们找出真正的问题。

【讨论】:

  • 非常感谢您的回复。我已经在两个模拟器上运行了该应用程序,并且都遇到了同样的问题。即使在进行了推荐的替代更改之后,它仍然是一样的。我已将所有依赖项更新为最新版本,但我仍然遇到同样的问题。你知道我可以尝试的其他任何事情吗?
  • 我也在我真正的华为 P20 Pro 上运行过这个,同样的错误发生了。
  • @JoshBrett 上述解决方法应该可以肯定。请仔细检查更新的代码。
【解决方案3】:

我从上面复制并粘贴了您的活动和 XML,编译并在像素 3xl 模拟器上运行,它对我有用。

注意 - 我只评论和更新了我没有的部分,例如框架、模型和样式(在 XML 中)。请检查微调器的样式是否有问题。

您可以为微调器提供自定义 UI,如下所示。

 ArrayAdapter<String> spinnerGenderAdapter = new ArrayAdapter<String>(getActivity(),
            R.layout.spinner_item, sp_gender);
    spinnerGenderAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mGenderSpinner.setAdapter(spinnerGenderAdapter);
    mGenderSpinner.setSelection(0);

spinner_item.xml

'<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:textColor="@color/somecolor"
android:textSize="16sp" />'

【讨论】:

  • 我正在添加微调器项目。 private void getAllChildExercises() { allChildExerciseNames.add("选择练习"); allChildExerciseNames.add("3 秒深蹲"); allChildExerciseNames.add("Plank"); allChildExerciseNames.add("PushUps"); allChildExerciseNames.add("慢跑"); allChildExerciseNames.add("运行"); }
  • 如果您能找到根本问题,请告诉我。
  • 你确定你已经在使用 API 30 的设备上测试过
  • 我已经在 2 个虚拟设备以及我自己的真手机上运行了该应用程序,但同样的问题正在发生......这可能与我正在获取值的事实有关吗?在我的 ROOM 数据库 (liveData) 的微调器中?
  • 在 Android R API30 上验证了这一点......这在上面的 GIF 中显示。
【解决方案4】:

我能够在 API 29 中以编程方式重现您的场景。我相信通过这种方式您可以获得默认的微调器行为。
为了重现您的场景,我所做的是覆盖ArrayAdapter 对象的默认getView() 实现。(我使用自定义simple_spinner_item_custom 布局进行了此操作,但它也应与android.R.layout.simple_spinner_item 一起使用):

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
                R.layout.simple_spinner_item_custom, spinnerStringArray) {
            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                // get a reference to the textview from the item layout
                TextView tv = view.findViewById(R.id.custom_text_1);
                // I manually set the text to empty like this to reproduce :D
                // tv.setText("");
                // the fix I would like to suggest
                tv.setText(spinnerStringArray[position]);
                return view;
            }
        };  

我也相信你现在可以在这个view 对象中设置onClickListener() 以获得onItemSelectedListener() 类似的行为。

【讨论】:

    【解决方案5】:

    目前您正在为onCreate() 中的Spinner 进行整个设置:您使用(可能为空的)数据列表实例化适配器并调用spinner.setAdapter(spinnerAdapter);

    之后,您只需更改数据列表:每次调用 getAllChildExercisesFromParentID(List&lt;String&gt;) 时,您都会将几个 Strings 添加到您传递给适配器的数据列表中。但是你永远不会在适配器上调用notifyDatasetChanged(),我认为这就是事情开始出错的地方。

    运行时不会抛出 Exception (如果我没记错的话,ListView 而不是 Spinner 就会出现这种情况) - 你只会看到一个空的(!)Spinner 和一个非空的下拉列表,选择一个项目没有任何效果。

    因此,要解决您的问题,您可以在调用 getAllChildExercisesFromParentID(List&lt;String&gt;) 时为 Spinner 重新创建一个新适配器。在这种情况下,您需要将Spinner 设为Activity 的字段,以便可以通过此方法访问它:

    private Spinner spinner;
    

    onCreate() 中初始化它但不要设置适配器:

    spinner =  findViewById(R.id.spinner1);
    spinner.setOnItemSelectedListener(this);
    

    当您收到数据时,为Spinner 创建一个新的适配器实例:

    private void getAllChildExercisesFromParentID(List<String> allChildExercisesReceived){
        // maybe removing old data is a good idea?
        allChildExerciseNames.clear();
    
        allChildExerciseNames.addAll(allChildExercisesReceived);
     
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item, allChildExerciseNames);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerAdapter);
    
    }
    

    或者你让你的Activity有一个字段private ArrayAdapter&lt;String&gt; spinnerAdapter;

    onCreate()中完全初始化Spinner

    Spinner spinner =  findViewById(R.id.spinner1);
    spinnerAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item, allChildExerciseNames);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerAdapter);
    
    spinner.setOnItemSelectedListener(this);
    

    ...并在您收到数据时更新数据列表(以及随之而来的适配器):

    private void getAllChildExercisesFromParentID(List<String> allChildExercisesReceived){
        // remove old data 
        allChildExerciseNames.clear();
    
        allChildExerciseNames.addAll(allChildExercisesReceived);
        spinnerAdapter.notifyDataSetChanged();
    }
    

    我更喜欢第二个版本,因为它不需要在每次刷新数据时都创建一个新对象——它可以帮助提高性能(但在这种情况下,对现代设备的影响几乎不会被注意到,所以更重要的是代码风格问题)

    顺便说一句,你当然可以而且应该在你的布局中保留Spinner - AppCompatSpinner 只是我试图摆脱刚刚弹出的奇怪的“Landroid/widget/AbsListView...”消息与Spinner 问题同时出现。

    【讨论】:

    • @Josh Brett - 在您的 cmets 中某处您提到了 Room 和 LiveData。在那之前,我只是在 onCreate() 中修补了一些东西,并注释掉了大部分与 ViewModel 相关的代码。今天我尝试了一个临时的 VewModel,所以我偶然发现了丢失的notifyDatasetChanged()(如第二个版本)。而且我相当确定这是导致不当行为的原因,因为现在我可以通过注释掉 notifyDatasetChanged() 再次使其“不起作用”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    相关资源
    最近更新 更多