即使在一个方法中接收3个元素,下一个类中的变量 adapter 也会继续为空:
public class SaveListFrag extends Fragment implements RequestCallBack<Savings> {
private ListView saveListview;
private FloatingActionButton floatIcon;
public List<Saving> mItemArray;
private DragListView mDragListView;
private boolean isDeviceOnline;
SavingsAdapter adapter;
Savings data;
SQLiteDB bd;
int test = 0;
rtn pretest;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_savelist, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init(view);
floatIcon = (FloatingActionButton) view.findViewById(R.id.floatIcon);
addFloatButton();
}
private void init(View view) {
mDragListView = (DragListView) view.findViewById(R.id.drag_list_view);
mDragListView.getRecyclerView().setVerticalScrollBarEnabled(true);
mDragListView.getRecyclerView().setHorizontalScrollBarEnabled(false);
mDragListView.setDragListListener(new DragListView.DragListListenerAdapter() {
@Override
public void onItemDragStarted(int position) {
}
@Override
public void onItemDragEnded(int fromPosition, int toPosition) {
if (fromPosition != toPosition) {
}
}
});
isDeviceOnline = isInternetConnected();
if (isDeviceOnline) {
mItemArray = new ArrayList<>();
makeRequest();
}
setupListRecyclerView();
}
private void addFloatButton() {
floatIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ActivityChooseSaving.class);
startActivity(intent);
}
});
}
private void setupListRecyclerView() {
mDragListView.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new SavingsAdapter(mItemArray, R.layout.item_save_list, R.id.arrowlayout, false, getActivity());
Log.d("Contenido item: ", mItemArray.toString());
mDragListView.setAdapter(adapter, true);
mDragListView.setCanDragHorizontally(false);
mDragListView.setCustomDragItem(new MyDragItem(getActivity(), R.layout.item_save_list));
}
@Override
public void onRequestSuccess(Savings response) {
if (mDragListView != null) {
mDragListView.setScrollingEnabled(true);
}
for (int i = 0; i < response.data.size(); i++) {
mItemArray.add(response.data.get(i));
}
adapter.notifyDataSetChanged();
Log.d("Item ", String.valueOf(mItemArray.get(0)));
isDeviceOnline = true;
}
@Override
public void onRequestFail(String message) {
if (mDragListView != null) {
mDragListView.setScrollingEnabled(false);
Toast.makeText(this.getActivity(), getString(R.string.NO_internet), Toast.LENGTH_LONG).show();
isDeviceOnline = false;
}
}
@Override
public void onJSONException(String message) {
}
private boolean isInternetConnected() {
ConnectivityManager cm = ((ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE));
if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}
private void makeRequest() {
if (mDragListView != null) {
mDragListView.setScrollingEnabled(true);
}
ArrayList<BasicNameValuePair> params = new ArrayList<>();
User user = CacheUtils.getUser(this.getActivity());
params.add(new BasicNameValuePair("user_id", "3371"));
Request<Savings> request = new Request<Savings>(this.getActivity(), CommonUtils.SERVICES.FETCH_ALL_SAVINGS, this, Savings.class, params);
request.execute();
}
private void createOfflineDatabase(List<Saving> alstAccounts) {
Intent syncServiceIntent = new Intent(getActivity(), SyncDatabaseService.class);
syncServiceIntent.putExtra("accountsList", (java.io.Serializable) alstAccounts);
syncServiceIntent.putExtra(CommonUtils.ACTION_TYPE, CommonUtils.ACTION_CREATE_OFFLINE_DATABASE);
getActivity().startService(syncServiceIntent);
}
@SuppressWarnings("deprecation")
private static class MyDragItem extends DragItem {
public MyDragItem(Context context, int layoutId) {
super(context, layoutId);
}
@Override
public void onBindDragView(View clickedView, View dragView) {
dragView.setBackgroundColor(dragView.getResources().getColor(R.color.list_item_background));
}
}
}
正如您所看到的,我打印第一个“mItemArray”位置的值来检查内容,并且它存在,但由于某种原因,mItemArray在主适配器中设置为空。
答案 0 :(得分:0)
问题出在SavingsAdapter类中,构造函数中的列表无法正确使用来检索数据。