java - 如何获取要更新的 Android 微调器文本?
问题描述
我的活动中有一个微调器。
顶部微调栏用作标题,然后在其下方有一个导航栏,在下半部分有一个片段容器。
微调器包含它通过的所有练习的列表。
单击微调器下拉列表中的一项后,我希望更新微调器文本,但是没有发生任何更改...
我收到以下消息:
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>
构建.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'
}
解决方案
Spinner
目前您正在为in进行整个设置onCreate()
:您使用(可能为空的)数据列表实例化适配器并调用spinner.setAdapter(spinnerAdapter);
之后您只需更改数据列表:每次getAllChildExercisesFromParentID(List<String>)
调用时,您都会将几个String
s 添加到您传递给适配器的数据列表中。但是你永远不会调用notifyDatasetChanged()
适配器,我认为这就是事情开始出错的地方。
运行时不会抛出一个(如果我没记错的话Exception
,就会出现这种情况)——你只会看到一个带有非空下拉列表的空(!),并且ListView
选择一个项目没有任何效果。Spinner
Spinner
因此,要解决您的问题,您可以在调用Spinner
时重新创建一个新适配器。getAllChildExercisesFromParentID(List<String>)
在这种情况下,您需要创建您的Spinner
a 字段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<String> spinnerAdapter;
完全初始化Spinner
in onCreate()
:
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
问题同时弹出。
推荐阅读
- azure - SonarQube Azure AD SAML 身份验证
- ios - 核心数据崩溃“外部数据引用找不到基础文件。”
- java - 设置 Spring Boot Web 应用并将其与 MySQL 数据库链接
- git - 同一项目的同一服务器上的裸存储库和非裸存储库
- javascript - 嵌套的 bootstrap 5 手风琴不会折叠打开的面板
- git - 是否可以在不更改历史记录的情况下从 Git 中删除旧文件?
- python - 如果我全局使用 bind_all,我可以为某些小部件设置例外吗?
- javascript - 网络核心 Webapi/SignalR 应用程序被 CORS 策略阻止(带有网络框架 4.7.2 的网络核心 5。FE 消费者)
- javascript - 检测是否使用外部 JavaScript 文件创建了 html 元素
- vba - For...下一个语句范围