2017-02-03 10 views
0

すべてが正常に動作し、リフレッシュされます。更新後に新しいレコードがクリックされると、アプリケーションがクラッシュします。私asynctaskクラスの私のメインクラスjava.lang.IndexOutOfBoundsException:インデックス:7、サイズ:リフレッシュ後のサイズ:7

mPullToRefreshView = (PullToRefreshView)view.findViewById(R.id.pull_to_refresh); 
     mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       mPullToRefreshView.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         new RetrieveWarehouseSalesTask(getActivity()).execute(); 
        } 
       }, 10); 
      } 
     }); 

class RetrieveWarehouseSalesTask extends AsyncTask<Void,Void,Void> { 
    private String TAG = RetrieveWarehouseSalesTask.class.getSimpleName(); 
    private String TAG_PID = "pid"; 
    public ProgressDialog pDialog; 
    private Context context; 
    //URL to get JSON details 
    private String url = "http://localhost/abc.php"; 
    ArrayList<HashMap<String,String>> sales_details; 
    JSONObject jsonObj; 
    String jsonStr; 
    JSONArray sales; 

    //for recycler view 
    private RecyclerView warehouse_recycler; 
    private AdapterRecycler mAdapter; 
    List<WarehouseSalesDetails> data = new ArrayList<>(); 

    public RetrieveWarehouseSalesTask(Context context){ 
     this.context = context; 
     sales_details = new ArrayList<>(); 

    } 



    @Override 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(context); 
     pDialog.setMessage("Getting you the best warehouse sales..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... arg0){ 
     HttpHandler sh = new HttpHandler(); 
     //making a request to URL and getting response 
     jsonStr = sh.makeServiceCall(url); 
     Log.e(TAG, "Response from url: " + jsonStr); 


     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result){ 
     super.onPostExecute(result); 
     if(pDialog.isShowing()){ 
      pDialog.dismiss(); 
     } 
     if(jsonStr != null){ 
      try{ 
       jsonObj = new JSONObject(jsonStr); 
       //Getting JSON Array Node 
       sales = jsonObj.getJSONArray("Result"); 
       //looping through all results 
       for(int i = 0; i<sales.length();i++){ 
        JSONObject s = sales.getJSONObject(i); 
        WarehouseSalesDetails wsd = new WarehouseSalesDetails(); 
        wsd.expiry_date = s.getString("expiry_date"); 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
        Date actual_date = sdf.parse(wsd.expiry_date); 
        if(new Date().before(actual_date)){ 
         wsd.id = s.getString("id"); 
         wsd.company_name = s.getString("company_name"); 
         wsd.promotion_image= s.getString("promotion_image"); 
         wsd.title = s.getString("title"); 
         wsd.promotional_period = s.getString("promotional_period"); 
         wsd.viewCount = s.getString("view_count"); 
         data.add(wsd); 
        } 
       } 
       Log.d("TAG",sales_details.toString()); 
      }catch(final JSONException e){ 
       Log.e(TAG, "JSON parsing error: " + e.getMessage()); 
      } catch (ParseException e) { 
       e.printStackTrace(); 
      } 
     }else{ 
      Log.e(TAG,"Couldn't get json from server"); 
     } 
     //update RecyclerView 
     warehouse_recycler = (RecyclerView)((AppCompatActivity) context).findViewById(R.id.recyclerView); 
     mAdapter = new AdapterRecycler(context, data); 
     final LinearLayoutManager layoutManager = new LinearLayoutManager(context); 
     layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
     warehouse_recycler.setLayoutManager(layoutManager); 
     warehouse_recycler.setAdapter(mAdapter); 
     warehouse_recycler.addOnItemTouchListener(
       new RecyclerItemClickListener(context,warehouse_recycler,new RecyclerItemClickListener.OnItemClickListener() { 
        @Override 
        public void onItemClick(View view, int position) { 
         WarehouseSalesDetails wsd = data.get(position); 
         // Toast.makeText(context,"ID is " + wsd.id, 
           //Toast.LENGTH_SHORT).show(); 
         String pid = wsd.id; 
         Intent in = new Intent(context,RetrieveIndividualWarehouseSales.class); 
         in.putExtra("pid",pid); 
         startActivity(in); 
        } 
        @Override 
        public void onLongItemClick(View view, int position){ 
         //do whatever 
        } 
       })); 
     mPullToRefreshView.setRefreshing(false); 
    } 

} 

Logcatポイントの行にエラー

WarehouseSalesDetails wsd = data.get(position); 

Logcatメッセージ

java.lang.IndexOutOfBoundsException: Index: 7, Size: 7 
                        at java.util.ArrayList.get(ArrayList.java:411) 

更新された後、最新のレコードを格納できないarraylistによる可能性があります。この問題についてどうすればいいですか?

+1

-1を実装することによって解決されますか? Androidはpositionのために1の最初の整数を開始し、ArrayListsは0を持つので、すべての整数が同じであればプログラマチックに簡単に問題を解決することができます。 – Nicholas

+0

@Nicholas、項目の詳細を位置1に表示します。問題を実際には解決しません。 – kylas

+0

それはあなたのアダプタが8個のアイテムを持っていることを意味しますが、あなたのデータリストにはわずか7個しかありません。アダプタにはいくつのアイテムがありますか? –

答えて

0

これは、位置にアダプタクラスでのonClick方法自体

itemView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        WarehouseSalesDetails wsd = data.get(getAdapterPosition()); 
        String pid = wsd.id; 
        Intent in = new Intent(context,RetrieveIndividualWarehouseSales.class); 
        in.putExtra("pid",pid); 
        context.startActivity(in); 
       } 
      }); 
関連する問題