Android java.lang.IndexOutOfBoundsException:索引9无效,大小为1

Android java.lang.IndexOutOfBoundsException:索引9无效,大小为1,android,android-listview,pull-to-refresh,Android,Android Listview,Pull To Refresh,我正在使用androidPull to refreshlistview。工作正常,但在刷新listview时,我试图向下滚动列表。我遇到以下错误: 这是我的密码: public void Callpagedetails() { try { URL = http://history_thread.php?token_id=" + Token + "&p=" + page_count;

我正在使用android
Pull to refresh
listview。工作正常,但在刷新listview时,我试图向下滚动列表。我遇到以下错误:

这是我的密码:

public void Callpagedetails() {

        try
            {


         URL = http://history_thread.php?token_id=" + Token + "&p=" +                       page_count;                   
                Log.d("TAG", "API URL IS " + URL);

                HttpClient mHttpClient = new DefaultHttpClient();

                HttpGet mGetMethod = new HttpGet(URL);

                HttpResponse mReponseMessage = mHttpClient.execute(mGetMethod);

                String Response = EntityUtils.toString(mReponseMessage.getEntity());

                Log.d("TAG", "O/P Response is " + Response);

                JSONArray responseObject = new JSONArray(Response);

                          for(int i=0; i < responseObject.length(); i++)

                    {
                        obj = responseObject.getJSONObject(i);

                        listhashmap = new HashMap<String, Object>();

                        listhashmap.put("text",obj.getString("text"));

                here i am storing the item in arraylist arraylistitems


                    }
}
catch(Exception e)
{
System.out.println(e);
}

    }

谁能帮帮我吗。谢谢。

您不应该在非UI线程中触摸适配器的数据

您的
doInBackground()
似乎将适配器可能使用的计数设置为零,并猜测
Callpagedetails()
直接修改适配器使用的数据


而是在UI线程中修改适配器的数据
onPostExecute()
是一个很好的地方。

在您的Callpagedetails中,在instanced responseObject之后查看for循环

您保留初始列表HashMap。这就是为什么listhashmap大小为1。将新的HashMap替换为外部for循环

for (int i = 0; i < responseObject.length(); i++) {
    obj = r esponseObject.getJSONObject(i);
    listhashmap = new HashMap < String, Object > ();  // problems code
    listhashmap.put("text", obj.getString("text"));
}
for(int i=0;i();//问题代码
listhashmap.put(“text”,obj.getString(“text”);
}

受保护的类刷新任务扩展AsyncTask>>{

        @Override
        protected ArrayList<HashMap<String, Object>> doInBackground(Void... params) {
            // TODO Auto-generated method stub
            page_count = 0;
            ArrayList<HashMap<String, Object>> data=Callpagedetails();
            return data;

        }

        protected void onPostExecute(ArrayList<HashMap<String, Object>> result) {
            super.onPostExecute(result);
        if(result!=null)
        {
             adapterfirst = new Questionadapter(historylist.this,result);
             listviewfirst = (RefeshListView) findViewById(R.id.listquestion);
             listviewfirst.setAdapter(adapterfirst);
                adapterfirst.notifyDataSetChanged();
        }
         yourlistView.postDelayed(new Runnable() {

            @Override
            public void run() {
                 new RefreshTask().execute();  
            }
        }, 1000);

}
        }
    }
@覆盖
受保护的ArrayList doInBackground(无效…参数){
//TODO自动生成的方法存根
页数=0;
ArrayList data=Callpagedetails();
返回数据;
}
受保护的void onPostExecute(ArrayList结果){
super.onPostExecute(结果);
如果(结果!=null)
{
adapterfirst=新问题适配器(historylist.this,result);
listviewfirst=(RefeshListView)findViewById(R.id.listquestion);
setAdapter(adapterfirst);
adapterfirst.notifyDataSetChanged();
}
yourlistView.postDelayed(新的Runnable(){
@凌驾
公开募捐{
新建刷新任务().execute();
}
}, 1000);
}
}
}

嗨,普里亚!试试这个程序!将对象作为参数传递给onpostExecute总是一种好方法!希望这能解决你的问题

这可能不准确。但是试着做相应的修改

 public void Callpagedetails() {

    try
        {
     URL = http://history_thread.php?token_id=" + Token + "&p=" +                       page_count;                   
            Log.d("TAG", "API URL IS " + URL);

            HttpClient mHttpClient = new DefaultHttpClient();

            HttpGet mGetMethod = new HttpGet(URL);

            HttpResponse mReponseMessage = mHttpClient.execute(mGetMethod);

            String Response = EntityUtils.toString(mReponseMessage.getEntity());

            Log.d("TAG", "O/P Response is " + Response);

            JSONArray responseObject = new JSONArray(Response);

                      for(int i=0; i < responseObject.length(); i++)

                {
                    obj = responseObject.getJSONObject(i);


                }
  }
catch(Exception e)
{
 System.out.println(e);
}

}
以及

onPostExecute(){
if(yourListView!=null){
listhashmap=新建HashMap();
listhashmap.put(“text”,obj.getString(“text”);
在这里,您将在arraylist arraylistitems中存储该项
&&还设置,,
yourAdapter.notifyDataSetChanged();
}
.....
.....
}

*->你自己去尝试吧。

我不想尝试,但我相信这会对你有所帮助

                JSONArray responseObject = new JSONArray(Response);
                listhashmap = new HashMap<String, Object>();
                for(int i=0; i < responseObject.length(); i++)
                {
                    obj = responseObject.getJSONObject(i);
                    listhashmap.put("text",obj.getString("text"));

                }
JSONArray responseObject=新的JSONArray(响应);
listhashmap=新建HashMap();
对于(int i=0;i
arraylist保留对象引用,这样列表只保留最后一个值,因为您是同一内存位置上的新对象

for (int i = 0; i < responseObject.length(); i++) {
    obj = r esponseObject.getJSONObject(i);
    listhashmap = new HashMap < String, Object > ();  // problems code
    listhashmap.put("text", obj.getString("text"));
}
for(int i=0;i();//问题代码
listhashmap.put(“text”,obj.getString(“text”);
}
取代

for (int i = 0; i < responseObject.length(); i++) {
    obj = r esponseObject.getJSONObject(i);
 **HashMap<String, Object> listhashmap = new HashMap<String, Object>();**

                        listhashmap.put("text",obj.getString("text"));

                here i am storing the item in arraylist arraylistitems
                         rraylistitem.put(listhashmap);
}
for(int i=0;i
如果没有代码,我们如何提供帮助?????请发布刷新方法code检查您在向下滚动时是否更改了适配器。您何时调用了RefreshTask.execute()?您应该确保添加适配器扩展的所有代码,这是导致其失败的关键。谢谢,但我正在onPostExecute()中修改适配器的数据只有您发布的代码没有修改
onPostExecute()
中的底层数据,它实际上只是调用
notifyDataSetChanged()
。所以我猜您正在修改
doInBackground()
中的数据。但是在Callpagedetails()中,我正在检索数据并仅存储在数组中。。。我假设适配器同时使用同一个数组。将数据收集到另一个数组中,并将其交换到UI线程中的适配器中。你的意思是我必须在for loopya之外调用listhashmap,在循环外引用listhashmap。然后,只有第一个项目存储在listview中我尝试过,但在刷新时,如果尝试滚动listview,我会遇到错误。为什么要在您自己的风险下尝试相同的问题…并且只有一个数组是receivingOk,这就是为什么不确切,但尝试在“EndlessAdapters”和“listview+ScrollView”上进行搜索是的,祝您好运。是的,获取最后一项,因为您使用的是静态“文本”值。。在Hashmap中应该分配不同的键。它与仅显示一条记录(最后一条)的密钥传递相同。所以传递不同的键值。比如listhashmap.put(obj.getString(“uniqueKey”)、obj.getString(“text”);在uniqueKey中传递您的id字段。您可以通过这个idalso pass listhashmap.put(i,obj.getString(“text”))获取记录;您可以解决您的问题我已经尝试过这样做,但我只获取最后一项请确保您的键值不同。请在插入ListHasMetr.PoT(key,value)之前检查日志。String PUT I作为一个键,因为如果字符串值与HASMAP中只考虑一个项目相同。
                JSONArray responseObject = new JSONArray(Response);
                listhashmap = new HashMap<String, Object>();
                for(int i=0; i < responseObject.length(); i++)
                {
                    obj = responseObject.getJSONObject(i);
                    listhashmap.put("text",obj.getString("text"));

                }
for (int i = 0; i < responseObject.length(); i++) {
    obj = r esponseObject.getJSONObject(i);
    listhashmap = new HashMap < String, Object > ();  // problems code
    listhashmap.put("text", obj.getString("text"));
}
for (int i = 0; i < responseObject.length(); i++) {
    obj = r esponseObject.getJSONObject(i);
 **HashMap<String, Object> listhashmap = new HashMap<String, Object>();**

                        listhashmap.put("text",obj.getString("text"));

                here i am storing the item in arraylist arraylistitems
                         rraylistitem.put(listhashmap);
}