[英]Firebase data not displaying in Recycler view. How do I display user specific data from firebase in recyclerview?
[英]How do I display my API JSON data into a recycler view
我剛剛完成將我的 API 數據放入 JSONObject,現在我試圖在回收器視圖中可視化數據。 我將如何去做,這是我的代碼:
評論說傳遞給適配器的地方,我認為這就是回收者視圖的去向。 對不起,意大利面代碼,我對 JAVA 和面向對象很陌生。 任何幫助將不勝感激。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCompanyInput = findViewById(R.id.companyInput);
log = findViewById(R.id.log);
mDescriptionText = findViewById(R.id.descriptionText);
mTitleText = findViewById(R.id.titleText);
save = findViewById(R.id.searchButton);
realm = Realm.getDefaultInstance();
save.setOnClickListener(this);
}
public void onClick(View view) {
//pull string from search query
String companyInput = mCompanyInput.getText().toString();
okHttpClient = new OkHttpClient();
request = new Request.Builder().url(url + companyInput).header("Authorization", "k6DNRbTp-AnQWn51JBz5VuPiTl8jv4_etdzoMyhf").method("GET", null).build();
Log.d(TAG, "onClick:" + url);
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i(TAG, e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
JSONObject object = new JSONObject (response.body().string());
JSONArray array = object.getJSONArray("items");
// object.get("start_index");
Log.i(TAG, "onResponse:"+ array.length());
for (int i = 0; i < array.length(); i++){
JSONObject company = (JSONObject) array.get(i);
Log.i(TAG, "onResponse: " + company);
}
//pass to adapter here than feed to recycler view
} catch (JSONException e) {
e.printStackTrace();
}
}
});
// writeToDB(mCompanyInput.getText().toString().trim(), (mDescriptionText.getText().toString().trim()));
showData();
}
public void showData() {
RealmResults<Company> guests = realm.where(Company.class).findAll();
// Use an iterator to invite all guests
String op = "";
for (Company guest : guests) {
op += guest.getName();
op += guest.getAppointments();
}
log.setText(op);
}
public void writeToDB(final String mTitleText, final String mDescriptionText) {
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
Company user = new Company(mTitleText, mDescriptionText);
bgRealm.insert(user);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
// Transaction was a success.
Log.v("Database", "Data Inserted");
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
// Transaction failed and was automatically canceled.
Log.e("Database", error.getMessage());
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
這是我的 API 在日志貓中吐出的內容。 D/MainActivity: onClick: https://api.companieshouse.gov.uk/search/officers?q= I/MainActivity: onResponse:1 I/MainActivity: onResponse: {"links":{"self":"/officers /gPAqCDv52XAYJON4Z9SHCQOG-hU/appointments"},"description":"約會總數 1","address_snippet":"27 Old Gloucester Street, London, United Kingdom, WC1N 3AX","description_identifiers":["appointment-count" ],"appointment_count":1,"snippet":"","kind":"searchresults#officer","address":{"country":"WC1N 3AX","address_line_1":"老格洛斯特街","前提":"27","locality":"倫敦"},"matches":{"snippet":[],"title":[1,8]},"title":"FACEBOOK ADS SRL"}
您可以找到許多示例來展示您正在尋找的內容。
https://medium.com/@sontbv/retrofit-2-for-beginners-creating-an-android-api-client-3c4370e1118
還有很多很多其他的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.