为什么我的数据库列表一开始不显示,而是单击后才显示?

问题描述

我使用“房间建筑”作为一个简单的数据库来显示城市名称列表。 数据库正在运行,但是我唯一的问题是,只有在点击“编辑”文本字段后,数据库列表才会显示。我已经附上了各自的代码和一些屏幕截图,以便于更好地理解。 我确定这是我做过的一些小逻辑错误。有人可以帮忙吗? 预先感谢。

位置信息活动

package com.example.smweather;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

import java.util.List;

public class LocationsActivity extends AppCompatActivity {

    private LocationViewModel locationViewModel;

    AutoCompleteTextView trialedCity;
    String[] trialCityNames;

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

        setTitle("Locations");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        trialedCity = findViewById(R.id.aedCity);
        trialCityNames = getResources().getStringArray(R.array.sample_city_names);
        ArrayAdapter<String> sampleCityListAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,trialCityNames);
        trialedCity.setAdapter(sampleCityListAdapter);

        trialedCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                trialedCity.setCursorVisible(true);
            }
        });

        RecyclerView locationRecyclerView = findViewById(R.id.location_recycler_view);
        locationRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        locationRecyclerView.setHasFixedSize(true);

        final LocationAdapter adapter = new LocationAdapter();
        locationRecyclerView.setAdapter(adapter);

        locationViewModel = new ViewModelProvider(this,ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication()))
                .get(LocationViewModel.class);
        locationViewModel.getAllLocations().observe(this,new Observer<List<Location>>() {
            @Override
            public void onChanged(List<Location> locations) {
                adapter.submitList(locations);
            }
        });

        adapter.setOnLocationClickListener(new LocationAdapter.OnLocationClickListener(){
            @Override
            public void onLocationClick(Location location) {

                String trialvalue = location.getStrlocation();
                Intent resultIntent = new Intent();
                resultIntent.putExtra("trialValue",trialvalue);
                setResult(RESULT_OK,resultIntent);
                finish();
            }
        });

        // TODO: 12-08-2020 swipe gesture to delete
        new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.LEFT) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView,@NonNull                         
                 RecyclerView.ViewHolder viewHolder,@NonNull RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder,int direction) {
                locationViewModel.delete(adapter.getLocationAt(viewHolder.getAdapterPosition()));
                Toast.makeText(LocationsActivity.this,"Location deleted",Toast.LENGTH_SHORT).show();
            }
        }).attachToRecyclerView(locationRecyclerView);

    }

    public void trialdata(View view) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(myApi.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        myApi trialapi = retrofit.create(myApi.class);

        Call<Example> exampleCall = 
        trialapi.getinfo(trialedCity.getText().toString().trim(),MainActivity.openWeatherapiKey);
        exampleCall.enqueue(new Callback<Example>() {
            @Override
            public void onResponse(Call<Example> call,Response<Example> response) {
                if (response.code() == 404) {
                    Toast.makeText(LocationsActivity.this,"Invalid City",Toast.LENGTH_SHORT).show();
                } else if (response.code() == 429) {
                    Toast.makeText(LocationsActivity.this,"Your account is temporary blocked due to 
                  exceeding of requests limitation of your subscription type.",Toast.LENGTH_SHORT).show();
                } else if (!response.isSuccessful()) {
                    Toast.makeText(LocationsActivity.this,"Error code: " + response.code(),Toast.LENGTH_SHORT).show();
                } else {
                    String trialvalue = trialedCity.getText().toString().trim();
                    Intent resultIntent = new Intent();
                    resultIntent.putExtra("trialValue",trialvalue);

                    Location location = new Location(trialvalue);
                    locationViewModel.insert(location);
                    setResult(RESULT_OK,resultIntent);
                    finish();
                }
            }

            @Override
            public void onFailure(Call<Example> call,Throwable t) {
                Toast.makeText(LocationsActivity.this,t.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }
}

ViewModel类

package com.example.smweather;

import android.app.Application;

import java.util.List; 

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

public class LocationViewModel extends AndroidViewModel {
private LocationRepository repository;
private LiveData<List<Location>> allLocations;

public LocationViewModel(@NonNull Application application) {
    super(application);
    repository = new LocationRepository(application);
    allLocations = repository.getAllLocations();
}

public void insert(Location location) {
    repository.insert(location);
}

public void update(Location location) {
    repository.update(location);
}

public void delete(Location location) {
    repository.delete(location);
}

public void deleteAllLocations() {
    repository.deleteAllLocations();
}

public LiveData<List<Location>> getAllLocations() {
    return allLocations;
}
}

In the first image The list of database should show below the search button

The list only shows when the editText field is clicked

请参阅上面的图片以更好地理解。

解决方法

我不确定,但是也许您应该在观察员中致电adapter.notifyDataSetChanged()

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...