ListView没有显示任何内容

时间:2018-07-30 08:53:59

标签: android listview listadapter

我从database获取预设字段,并使用ListViewPresetsListAdapter中显示它们。

C 如下:

public class BasicAcActivity extends Activity implements OnCheckedChangeListener {

private static final String TAG = BasicAcActivity.class.getName();
public static volatile PresetsListAdapter presetsListAdapter;
public static volatile SwitchCompat basicEQToggle;
private ListView mListView;
private FlipBeatsDataManager mDataManager;
private PresetsManager mPrstManager;
private SharedPreferences mShrdPrefs;
private SharedPreferences mShrdPrefPresets;
private TextView tvPresets;

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

    String text = Utilities.getResourceValue(this, R.string.equalizer_presets);
    tvPresets.setText(text);

    // Get presets
    List<Preset> presets = generatePresets();
    if (presets != null && presets.size() > 0)
    {
        for (Preset preset : presets)
        {
            mDataManager.createPreset(preset);
        }
    }

    List<Preset> dbPresets = mDataManager.getMandotoryPresets();
    presetsListAdapter = new PresetsListAdapter(this, dbPresets);
    basicEQToggle.setChecked(true);
    mListView.setAdapter(presetsListAdapter);

    String onOrOff = mShrdPrefPresets.getString("Presets", "on");
    if (onOrOff.equalsIgnoreCase("on"))
    {
        basicEQToggle.setChecked(true);
    }
    else
    {
        basicEQToggle.setChecked(false);
    }

    basicEQToggle.setOnCheckedChangeListener(this);
}

private void initialize()
{
    tvPresets = (TextView) findViewById(R.id.lbl_presets);
    mListView = (ListView) findViewById(R.id.presets_list);
    basicEQToggle = (SwitchCompat) findViewById(R.id.basic_eq_toggle);

    tvPresets.setTypeface(CommonUtils.getTfRobotoRegFont());

    mPrstManager = PresetsManager.getInstance(getApplicationContext());
    mDataManager = FlipBeatsDataManager.getInstance(getApplicationContext());
    mShrdPrefs = getApplicationContext().getSharedPreferences("Prefs", Context.MODE_PRIVATE);
    mShrdPrefPresets = getApplicationContext().getSharedPreferences("Preset", Context.MODE_PRIVATE);
}

private List<Preset> generatePresets()
{
    // Default value is set if 'equalizer' is null
    short bands = 5;

    if (PlayerService.sEqualizer != null)
    {
        try
        {
            bands = PlayerService.sEqualizer.getNumberOfBands();
        }
        catch (Exception | Error e)
        {
            Log.w(TAG, "-- createPresets " + e.getMessage());
        }
    }

    // Create Preset Based on the Bands Level
    return mPrstManager.createPresets(bands);
}

@Override
protected void onResume()
{
    super.onResume();
    setLastUsedType();
    boolean isSelected = mDataManager.findMandatoryPresetSelected();

    if (!isSelected)
    {
        int preselect = mDataManager.getLastSelectedPresetId();
        mDataManager.updateSelectedToPro1(preselect);
    }
    presetsListAdapter.notifyDataSetChanged();

    Activity actv = getParent();
    if (actv != null && actv instanceof AudioConfigurationActivity)
    {
        AudioConfigurationActivity parent = (AudioConfigurationActivity) actv;
        // Sets default advance settings.
        parent.setDefaultAdvanceSettings();
        parent.setThroughBasicActivity();
    }

    setDefaultAdvancedSettings();
}

@Override
public boolean onKeyUp(int keyCode, @NonNull KeyEvent event)
{
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
    {
        if (SoundHealth.getSoundHealth(this))
        {
            Message.alertMsg(this, Utilities.getResourceValue(this, R.string.sound_health),
                Utilities.getResourceValue(this, R.string.sound_health_msg));
        }
        return true;
    }
    return super.onKeyUp(keyCode, event);
}

@Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked)
{
    if (isChecked)
    {
        basicEQToggle.setSelected(true);
        if (PlayerService.sEqualizer != null)
        {
            try
            {
                PlayerService.sEqualizer.setEnabled(true);
            }
            catch (Exception | Error e)
            {
                Log.w(TAG, "-- onCheckedChanged " + e.getMessage());
            }
        }
        getApplicationContext();
        mShrdPrefPresets.edit().putString("Presets", "on").apply();

        if (BasicAcActivity.presetsListAdapter != null)
        {
            BasicAcActivity.presetsListAdapter.mPresets = mDataManager.getMandotoryPresets();
        }
        presetsListAdapter.notifyDataSetChanged();
    }
    else
    {
        basicEQToggle.setSelected(false);

        if (PlayerService.sEqualizer != null)
        {
            try
            {
                PlayerService.sEqualizer.setEnabled(false);
            }
            catch (Exception | Error e)
            {
                Log.w(TAG, "-- onCheckedChanged " + e.getMessage());
            }
        }

        if (BasicAcActivity.presetsListAdapter != null)
        {
            BasicAcActivity.presetsListAdapter.setPreset(14);
        }

        mShrdPrefPresets.edit().putString("Presets", "off").commit();

        if (BasicAcActivity.presetsListAdapter != null)
        {
            BasicAcActivity.presetsListAdapter.mPresets = mDataManager.getMandotoryPresets();
        }

        presetsListAdapter.notifyDataSetChanged();
    }
}

private void setLastUsedType()
{
    mShrdPrefs.edit().putString(AudioConfigurationActivity.AU_SET_TYPE_KEY,
        AudioConfigurationActivity.AU_SET_TYPE_BASIC);
}

/*
 * Sets default advanced sound settings
 */
private void setDefaultAdvancedSettings()
{
    try
    {
        if (PlayerService.sAudioDefaultValues != null)
        {
            AudioDefaultValues def = PlayerService.sAudioDefaultValues;
            PlayerService.sBaseBooster.setStrength(def.getBassBoost());
            PlayerService.sSurroundSound.setStrength(def.getSurroundSound());
            PlayerService.sPresetReverb.setPreset(def.getRoomSize());
            /** EnvironmentalReverb attributes */
            PlayerService.sEnvironmentalReverb.setDecayHFRatio(def.getDecayHfRatio());
            PlayerService.sEnvironmentalReverb.setDecayTime(def.getDecayTime());
            PlayerService.sEnvironmentalReverb.setDensity(def.getDensity());
            PlayerService.sEnvironmentalReverb.setDiffusion(def.getDiffusion());
            PlayerService.sEnvironmentalReverb.setReflectionsDelay(def.getReflectionsDelay());
            PlayerService.sEnvironmentalReverb.setReflectionsLevel(def.getReflectionsLevel());
            PlayerService.sEnvironmentalReverb.setReverbDelay(def.getReverbDelay());
            PlayerService.sEnvironmentalReverb.setReverbLevel(def.getReverbLevel());
            PlayerService.sEnvironmentalReverb.setRoomHFLevel(def.getRoomHfLevel());
            PlayerService.sEnvironmentalReverb.setRoomLevel(def.getRoomLevel());
        }
    }
    catch (Exception e)
    {
        Log.w(TAG, "--- setDefaultAdvancedSettings, " + e.getMessage());
    }
  }

}

适配器类:

abstract class BeatsBaseAdapter extends BaseAdapter {
final ThemeManager mThemeManager;
Context mContext;
Activity mActivity;
ImageLoader mImageLoader;
int mCategory;
FlipBeatsDataManager mDataManager;
int txtColorNumber;
int colorPrimaryText;
int colorSecondaryText;
int colorListBg;
int seperatorBg;
int listDrawbleBg;
int flipCardBg;
int dashboardCardBg;

/**
 * Base Constructor
 *
 * @param context Application Context
 */
FlipBeatsBaseAdapter(Context context) {
    mContext = context;
    mDataManager = FlipBeatsDataManager.getInstance(mContext);
    int sysThemeId = ThemeUtils.getTheme(mContext);
    mThemeManager = ThemeManager.getInstance(mContext, sysThemeId);
}

/**
 * Base Constructor
 *
 * @param activity Current Activity
 */
FlipBeatsBaseAdapter(Activity activity) {
    if (activity != null) {
        mContext = activity.getApplicationContext();
        mActivity = activity;
        mDataManager = FlipBeatsDataManager.getInstance(mContext);
        int sysThemeId = ThemeUtils.getTheme(mContext);
        mThemeManager = ThemeManager.getInstance(mContext, sysThemeId);

        if (FlipBeatsGlobals.isBlackEditionActive) {
            txtColorNumber = mContext.getResources().getColor(R.color.color_app_txt_numbers_dark);
            colorPrimaryText = mContext.getResources().getColor(R.color.color_app_text_color_dark);
            colorSecondaryText = mContext.getResources().getColor(R.color.color_app_secondary_text_color_dark);
            colorListBg = mContext.getResources().getColor(R.color.color_black);

            seperatorBg = mContext.getResources().getColor(R.color.color_app_disable_color_dark);
            listDrawbleBg = R.drawable.list_front_bg_dark;
            flipCardBg = mContext.getResources().getColor(R.color.color_frame_menu_dark);
            dashboardCardBg = mContext.getResources().getColor(R.color.color_app_txt_disable_dark);
        } else {
            txtColorNumber = mContext.getResources().getColor(R.color.color_app_txt_numbers);
            colorPrimaryText = mContext.getResources().getColor(R.color.color_app_text_color);
            colorSecondaryText = mContext.getResources().getColor(R.color.color_app_secondary_text_color);
            colorListBg = mContext.getResources().getColor(R.color.color_white);
            seperatorBg = mContext.getResources().getColor(R.color.color_app_disable_color);
            listDrawbleBg = R.drawable.list_front_bg;
            flipCardBg = mContext.getResources().getColor(R.color.color_frame_menu);
            dashboardCardBg = mContext.getResources().getColor(R.color.color_app_txt_disable);
        }
    } else {
        mThemeManager = null;
    }

}

}

此功能在 API级别25 之前可以正常使用,但是对于Android Oreo version不会显示ListView,仅在选中每个项目时才会显示。这里可能出了什么问题? 提前致谢。

编辑:实际上没有为这些设备设置colorListBg。这就是为什么列表不显示的原因,因为文本颜色设置为白色

0 个答案:

没有答案