未在Arraylist中添加的项目

时间:2017-07-06 07:37:06

标签: java android api android-intent arraylist

这就是我试图将api中的“ids”添加到我的arraylist

int id;


 private List<HashMap<String, Object>> 
               getMovies(JSONArray jMovies) throws NullPointerException {


 int movieCount = jMovies.length();


 List<HashMap<String, Object>> movieList = new ArrayList<HashMap<String, Object>>();


 HashMap<String, Object> movie = null;

    //Taking each movie, parses and adds to list object
    for (int i = 0; i < movieCount; i++) {

        try {
            JSONObject getid=(JSONObject) jMovies.get(i);

            movie = getMovie(getid);
            movieList.add(movie);
            id =Integer.parseInt(getid.getString("id"));


        } catch (JSONException e) {

        }
        getNumberlist(i,id);
    }

    return movieList;
}


public ArrayList<Integer> ids=new ArrayList<Integer>();
public void getNumberlist(int i,int id)
{
    ids.add(i,id);

}

public List<Integer> getNumberslist()
{ return this.ids;
}

并尝试将其作为

的Itemclick侦听器
MovieJSONParser mj = new MovieJSONParser();


public void onItemClick(AdapterView<?> l, View v, int position, long id) {


   Intent intent = new Intent(this,next.class);

   if(position==0) {
       int id1 = mj.getNumberslist().get(0);
       intent.putExtra("id",id1);
       startActivity(intent);
   }
    if(position==1) {
        int id1 = mj.getNumberslist().get(1);
        intent.putExtra("id",id1);
        startActivity(intent);
    }
    if(position==2) {
        int id1 = mj.getNumberslist().get(2);
        intent.putExtra("id",id1);
        startActivity(intent);
    }
    if(position==3) {
        int id1 = mj.getNumberslist().get(3);
        intent.putExtra("id",id1);
        startActivity(intent);
    }

    }

1 个答案:

答案 0 :(得分:1)

首先,如果你正在捕获异常,至少打印堆栈跟踪,以便更容易调试代码。

使用jMovies.get(i)获取JSONObject时,您的api数据为空,因此您将获得索引超出绑定的异常。

     for (int i = 0; i < movieCount; i++) {

            try {
                JSONObject getid=(JSONObject) jMovies.get(i);

                movie = getMovie(getid);
                movieList.add(movie);
                id =Integer.parseInt(getid.getString("id"));


            } catch (JSONException e) {
              e.printStackTrace();
            }
            getNumberlist(i,id);
        }

检查您是否在jMovies中获取数据。