Моя проблема связана с андроидом ListView. Я разработал приложение для Android с фрагментом. В фрагменте есть ListView, который отображает некоторые данные. Эти данные загружаются с сервера AsyncTask. Когда я запускаю приложение в первый раз (приложение даже не установлено на устройстве), ListView не отображает какой-либо элемент. Затем, когда, например, я минимизирую приложение и максимизирую его, или переключаюсь между экранами приложений, элементы появились. Я изучил исходный код в течение многих часов, но не смог исправить проблему. Любая помощь будет высоко оценена. Ниже приведен код приложения.Android ListView не отображает данные при первом запуске приложения
Класс MainActivity.java.
public class MainActivity extends FragmentActivity implements RateRefreshListener {
private CurrencyConstants.RATE_TYPES currentTab;
private int darkColor;
private int lightColor;
private String currentCurrency;
private CurrencyConstants.SORTING currentSorting;
private RateFragment currentFragment;
private RateFragment banksFragment;
private RateFragment exchangesFragment;
private UserPreferences userPreferences;
TextView bankOrExchange;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bankOrExchange = (TextView )findViewById(R.id.bank_or_exchange);
userPreferences = UserPreferences.sharedUserPreferences(this);
darkColor = getResources().getColor(R.color.dark);
lightColor = getResources().getColor(R.color.light);
currentTab = CurrencyConstants.RATE_TYPES.EXCHANGE;
currentCurrency = userPreferences.getCurrencyCode();
currentSorting = userPreferences.getSortingCode();
LinearLayout currencyLayout = (LinearLayout) findViewById(R.id.currency);
for (int index = 0 ; index < currencyLayout.getChildCount() ; index++) {
TextView currency = (TextView) currencyLayout.getChildAt(index);
currency.setClickable(true);
currency.setOnClickListener(currencyClickListener);
if (userPreferences.getCurrencyCode().equals(CurrencyConstants.CURRENCIES[index])) {
currency.setBackgroundColor(darkColor);
currency.setTextColor(lightColor);
} else {
currency.setBackgroundResource(R.drawable.light_rect_outlined);
currency.setTextColor(darkColor);
}
}
LinearLayout sortingLayout = (LinearLayout) findViewById(R.id.sorting);
for (int index = 0 ; index < sortingLayout.getChildCount() ; index++) {
TextView sorting = (TextView) sortingLayout.getChildAt(index);
sorting.setClickable(true);
sorting.setOnClickListener(sortingClickListener);
if (userPreferences.getSortingCode().equals(CurrencyConstants.SORTING.forIndex(index))) {
sorting.setBackgroundColor(darkColor);
sorting.setTextColor(lightColor);
} else {
sorting.setBackgroundResource(R.drawable.light_rect_outlined);
sorting.setTextColor(darkColor);
}
}
showBanks(null);
APITalker.sharedTalker().refresh(DBTalker.sharedTalker(this), this);
}
public void showBanks(View view) {
if (currentTab.equals(CurrencyConstants.RATE_TYPES.BANK)) {
return;
}
currentTab = CurrencyConstants.RATE_TYPES/**/.BANK;
setupTabs(lightColor, darkColor);
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.from_left_slide_in, R.anim.to_right_slide_out);
if (banksFragment == null) {
banksFragment= RateFragment.newInstance(currentCurrency, currentSorting, currentTab);
fragmentTransaction.add(R.id.rate_fragment_container, banksFragment);
} else {
banksFragment.setPreferences(currentCurrency, currentSorting);
bankOrExchange.setText(R.string.bank);
fragmentTransaction.replace(R.id.rate_fragment_container, banksFragment);
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
currentFragment = banksFragment;
}
@Override
public void rateRefreshSucceed() {
userPreferences.setLastUpdate(new Date().getTime());
currentFragment.setPreferences(currentCurrency, currentSorting);
}
@Override
public void rateRefreshFail(String error) {
}
}
Класс APITalker.java, который отвечает за взаимодействие с сервером.
public class APITalker {
private final String BASE_URL = "SOME URL";
private final String INDEX_URL = BASE_URL + "/index";
private static APITalker sharedTalker;
private AsyncHttpClient client;
public static APITalker sharedTalker() {
if (sharedTalker == null) {
sharedTalker = new APITalker();
}
return sharedTalker;
}
private APITalker() {
client = new AsyncHttpClient();
}
public void refresh(final DBTalker dbTalker, final RateRefreshListener listener) {
client.get(INDEX_URL, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
dbTalker.store(response);
listener.rateRefreshSucceed();
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
super.onFailure(statusCode, headers, responseString, throwable);
}
});
}
Фрагмент, содержащий ListView.
package com.flycode.restmobile.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.flycode.restmobile.R;
import com.flycode.restmobile.activity.MapActivity;
import com.flycode.restmobile.adapter.RateListAdapter;
import com.flycode.restmobile.constant.CurrencyConstants;
import com.flycode.restmobile.database.DBTalker;
import com.flycode.restmobile.model.Rate;
import java.util.ArrayList;
public class RateFragment extends Fragment implements ListView.OnItemClickListener {
private static final String CURRENCY_CODE = "currencyCode";
private static final String SORTING_CRITERIA = "sortingCriteria";
private static final String RATE_TYPE = "rateType";
private RateListAdapter rateListAdapter;
private CurrencyConstants.RATE_TYPES rateType;
public static RateFragment newInstance(String currencyCode, CurrencyConstants.SORTING sortingCriteria, CurrencyConstants.RATE_TYPES rateType) {
RateFragment fragment = new RateFragment();
Bundle args = new Bundle();
args.putString(CURRENCY_CODE, currencyCode);
args.putSerializable(SORTING_CRITERIA, sortingCriteria);
args.putSerializable(RATE_TYPE, rateType);
fragment.setArguments(args);
return fragment;
}
public RateFragment() {
super();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rate, container, false);
String currencyCode = getArguments().getString(CURRENCY_CODE);
CurrencyConstants.SORTING sortingCriteria = (CurrencyConstants.SORTING) getArguments().getSerializable(SORTING_CRITERIA);
rateType = (CurrencyConstants.RATE_TYPES) getArguments().getSerializable(RATE_TYPE);
ArrayList<Rate> rates = DBTalker.sharedTalker(getActivity()).getRates(currencyCode, sortingCriteria, rateType);
rateListAdapter = new RateListAdapter(getActivity(), rates);
ListView ratesList = (ListView) view.findViewById(R.id.rate_list);
ratesList.setAdapter(rateListAdapter);
ratesList.setOnItemClickListener(this);
return view;
}
public void setPreferences(String currencyCode, CurrencyConstants.SORTING sortingCriteria) {
getArguments().putString(CURRENCY_CODE, currencyCode);
getArguments().putSerializable(SORTING_CRITERIA, sortingCriteria);
ArrayList<Rate> rates = DBTalker.sharedTalker(getActivity()).getRates(currencyCode, sortingCriteria, rateType);
rateListAdapter.setRates(rates);
rateListAdapter.notifyDataSetChanged();
}
/**
* OnItemClickListener Methods
*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Rate rate = rateListAdapter.getItem(position);
Intent mapIntent = new Intent(getActivity(), MapActivity.class);
mapIntent.putExtra(MapActivity.RATE, rate);
getActivity().startActivity(mapIntent);
}
}
Кроме того, моя Logcat предупреждение/ошибка вывода
07-07 13:34:29.357 15256-15256/com.flycode.restmobile E/ION﹕ ION_IOC_CUSTOM_GET_CONFIG ioctl Failed. Use default
07-07 13:34:29.417 15256-15256/com.flycode.restmobile W/JsonHttpResponseHandler﹕ onSuccess(int, Header[], JSONObject) was not overriden, but callback was received
Мне нужен ваш код, когда вы выполнили асинтекс – Sheychan
Привет, Шейчан. Спасибо за ответ. AsyncTask выполняется путем вызова APITalker.sharedTalker(). Refresh (DBTalker.sharedTalker (this), this); в последней строке onCreate(). Взаимодействие с сервером находится в функции refresh() класса APITalker. – Victor
Последнее, что я хочу показать вам предупреждения/ошибки на logcat – Sheychan