我想獲取數組列表中的一個項目,但它給了我錯誤java.lang.IndexOutOfBoundsException:索引1無效,大小爲0 我把這個listEvent.get(1 ).getTotal()其中listEvent是我的arraylist 我如何從arraylist檢索一個元素?索引超出界限時調用arraylist中的一個項目
這裏是我的代碼
private void getData() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
//Incrementing the request counter
if(requestCount <= **listEvent.get(1).getTotal()**) {
requestCount++;
}
}
這裏是我的全部代碼
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.anakacara.anakacara.App.AppConfig;
import com.anakacara.anakacara.App.Event;
import com.anakacara.anakacara.Helper.CardAdapter;
import com.anakacara.anakacara.R;
import com.anakacara.anakacara.activity.DetailEvent;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link EventFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link EventFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class EventFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
//Creating a List of event
private List<Event> listEvent;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
private int requestCount = 1;
private OnFragmentInteractionListener mListener;
private RequestQueue requestQueue;
private ProgressBar progressBar;
private SwipeRefreshLayout swipeRefreshLayout;
public EventFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment EventFragment.
*/
// TODO: Rename and change types and number of parameters
public static EventFragment newInstance(String param1, String param2) {
EventFragment fragment = new EventFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
private final RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) {
// code
if (isLastItemDisplaying(recyclerView)) {
//Calling the method getdata again
getData();
}
}
@Override
public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
// code
if (isLastItemDisplaying(recyclerView)) {
//Calling the method getdata again
getData();
}
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View x = inflater.inflate(R.layout.fragment_event,null);;
progressBar = (ProgressBar) x.findViewById(R.id.progressBar1);
recyclerView = (RecyclerView) x.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
swipeRefreshLayout = (SwipeRefreshLayout) x.findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setOnRefreshListener(this);
/**
* Showing Swipe Refresh animation on activity create
* As animation won't start on onCreate, post runnable is used
*/
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
getData();
}
}
);
//Initializing our event list
listEvent = new ArrayList<>();
requestQueue = Volley.newRequestQueue(getActivity());
//Calling method to get data
getData();
// swipeRefreshLayout= (SwipeRefreshLayout) x.findViewById(R.id.swipeRefreshLayout);
// swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
// {
// @Override
// public void onRefresh()
// {
// if (isLastItemDisplaying(recyclerView)) {
////Calling the method getdata again
// getData();
// progressBar.setVisibility(View.GONE);
// }
// }
// });
recyclerView.addOnScrollListener(rVOnScrollListener);
adapter = new CardAdapter(listEvent, getActivity());
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
itemAnimator.setAddDuration(1000);
itemAnimator.setRemoveDuration(1000);
recyclerView.setItemAnimator(itemAnimator);
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
Intent intent = new Intent(getActivity(), DetailEvent.class);
intent.putExtra("event_judul", listEvent.get(position).getJudul()); //you can name the keys whatever you like
intent.putExtra("event_deskripsi", listEvent.get(position).getDeskripsi()); //note that all these values have to be primitive
這裏是我的JSON代碼
0: {
"id": 5
"judul": "asdfasdfasdf"
"deskripsi": "asfasdfasdfqwer"
"duit": 123123
"persen": 65
"sisahari": 43
"gambar": "http://192.168.0.13/task_manager/image/not4.jpeg"
"total": 4
}-
1: {
"id": 4
"judul": "asdfasfasdfas"
"deskripsi": "dafasdfasdfasdfasfdasdf"
"duit": 12341234
"persen": 12
"sisahari": 31
"gambar": "http://192.168.0.13/task_manager/image/not3.jpeg"
"total": 4
}
我需要訪問總,但是因爲在陣列0和1它是同樣我只需要訪問其中之一 我想使用這樣的代碼 if(requestCount < = 4){request_ount ++; } 這是4是頁面限制,但我想從數組中獲得它,所以它將是動態的,而不是像這樣的手動
但它會使它崩潰。我們不能在以前檢查尺寸嗎?如果我檢查這樣的大小if(listEvent.size()> = 1 && requestCount <= listEvent.get(1).getTotal()它會讓我在我的回收視圖中加載雙重數據 –
我想要的實際頁面限制json。這是getTotal中的頁面限制,所以每個項目都有頁面限制,但我只想使用1項 –
請檢查我的更新,並讓我知道它是否需要 –