如何解决为什么我的数据库列表一开始不显示,而是单击后才显示?
我使用“房间建筑”作为一个简单的数据库来显示城市名称列表。 数据库正在运行,但是我唯一的问题是,只有在点击“编辑”文本字段后,数据库列表才会显示。我已经附上了各自的代码和一些屏幕截图,以便于更好地理解。 我确定这是我做过的一些小逻辑错误。有人可以帮忙吗? 预先感谢。
位置信息活动
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()
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。