如何刷新ListView内容的所有时间?
我正在做一个android应用程序,我从数据库everymoment在线的球员。每个球员在ListView中占据一个位置,是一个项目列表。列表刷新如我所料,但是当我点击一个项目列表我的应用程序崩溃。我在android监视器中看到IllegalStateException,我不知道为什么。我认为这是因为我在更改内容和同时按下按钮时使用了listview。可以帮助我某人还是建议其他内容? 在下面的代码中,我从数据库中下载数据,然后将其放入列表中。如何刷新ListView内容的所有时间?
public void onclickRefreshfunction() { num = -1;
runOnUiThread(new Runnable() {
@Override
public void run() {
if (lock == 1) {
lock = 0;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(OnLinePlayerActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, list_of_onlineplayer);
adapter.clear();
adapter.notifyDataSetChanged();
list_of_levels.clear();
list_of_onlineplayer.clear();
list_of_usernames.clear();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(final String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
JSONArray usernamearray = jsonResponse.getJSONArray("list_of_usernames");
JSONArray levelarray = jsonResponse.getJSONArray("list_of_levels");
for (int i = 0; i < usernamearray.length(); i++) {
String name = usernamearray.getString(i);
if (name.equals(username)) {
num = i;
continue;
}
list_of_usernames.add(name);
}
for (int i = 0; i < levelarray.length(); i++) {
String level = levelarray.getString(i);
if (i == num) {
continue;
}
list_of_levels.add(level);
}
for (int i = 0; i < list_of_usernames.size(); i++) {
list_of_onlineplayer.add(" Παίκτης "+list_of_usernames.get(i) + " επίπεδο " + list_of_levels.get(i));
}
listView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(OnLinePlayerActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, list_of_onlineplayer);
listView.setAdapter(adapter);
lock = 1;
} catch (JSONException e) {
e.printStackTrace();
}
}
};
Response.ErrorListener error = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.w("error with volley", "error");
onclickRefreshfunction();
}
};
OnlineRequest onlineRequest = new OnlineRequest(responseListener , error);
// RequestQueue queue = Volley.newRequestQueue(OnLinePlayerActivity.this);
// queue.add(onlineRequest);
Mysingleton.getmInstance(getApplicationContext()).addToRequestque(onlineRequest);
}
}
});
}
下面是Android的显示器
12-03 10:50:46.371 17346-17346/? E/MessageQueue-JNI: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131558534, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)] at android.widget.ListView.layoutChildren(ListView.java:1566)
at android.widget.AbsListView.onTouchUp(AbsListView.java:4814)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:4610)
at android.view.View.dispatchTouchEvent(View.java:8135)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2149)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2295)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1622)
at android.app.Activity.dispatchTouchEvent(Activity.java:2565)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2243)
at android.view.View.dispatchPointerEvent(View.java:8343)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4769)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4635)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4193)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4247)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4216)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4327)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4224)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4384)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4193)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4247)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4216)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4224)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4193)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6567)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6484)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6455)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6420)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6647)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java
12-03 10:50:46.381 17346-17346/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sakis.loginregister, PID: 17346
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131558534, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1566)
at android.widget.AbsListView.onTouchUp(AbsListView.java:4814)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:4610)
at android.view.View.dispatchTouchEvent(View.java:8135)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2149)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2431)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2164)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2295)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1622)
at android.app.Activity.dispatchTouchEvent(Activity.java:2565)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2243)
at android.view.View.dispatchPointerEvent(View.java:8343)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4769)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4635)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4193)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4247)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4216)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4327)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4224)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4384)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4193)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4247)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4216)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4224)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4193)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6567)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6484)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6455)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6420)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6647)
at a
at a
我想,也许是至关重要的部分我是不是有问题的消息sure.So我需要你的帮助,以了解如何列表视图的工作。
回答:
首先,你正在被runOnUIThread(),响应听众可能被要求在不同的线程,这取决于你Mysingleton的OnlineRequest分心内runOnUiThread处理程序。 其次,您在代码的顶部行创建一个未使用的ArrayAdapter。 第三,你不需要每次都创建一个新的ArrayAdapter,只是改变列表内容,并调用notifyDataSetChanged()
我认为下面的代码应该解决您的问题:
public void onclickRefreshfunction() { num = -1;
if (lock == 1) {
lock = 0;
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(final String response) {
list_of_levels.clear();
list_of_onlineplayer.clear();
list_of_usernames.clear();
try {
JSONObject jsonResponse = new JSONObject(response);
JSONArray usernamearray = jsonResponse.getJSONArray("list_of_usernames");
JSONArray levelarray = jsonResponse.getJSONArray("list_of_levels");
for (int i = 0; i < usernamearray.length(); i++) {
String name = usernamearray.getString(i);
if (name.equals(username)) {
num = i;
continue;
}
list_of_usernames.add(name);
}
for (int i = 0; i < levelarray.length(); i++) {
String level = levelarray.getString(i);
if (i == num) {
continue;
}
list_of_levels.add(level);
}
for (int i = 0; i < list_of_usernames.size(); i++) {
list_of_onlineplayer.add(" Παίκτης "+list_of_usernames.get(i) + " επίπεδο " + list_of_levels.get(i));
}
runOnUiThread(new Runnable() {
@Override
public void run() {
listView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(OnLinePlayerActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, list_of_onlineplayer);
listView.setAdapter(adapter);
}
});
lock = 1;
} catch (JSONException e) {
e.printStackTrace();
}
}
};
Response.ErrorListener error = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.w("error with volley", "error");
onclickRefreshfunction();
}
};
OnlineRequest onlineRequest = new OnlineRequest(responseListener , error);
// RequestQueue queue = Volley.newRequestQueue(OnLinePlayerActivity.this);
// queue.add(onlineRequest);
Mysingleton.getmInstance(getApplicationContext()).addToRequestque(onlineRequest);
}
}
回答:
一旦你通过删除或编辑的内容修改的列表,即你需要调用“notifyDataSetChanged()”的适配器上,通知您的修改适配器。在你的代码中,你在响应方法外调用notifyDataSetChanged。
回答:
把你的回应伦斯特,你设置列表视图和适配器
以上是 如何刷新ListView内容的所有时间? 的全部内容, 来源链接: utcz.com/qa/260197.html