我在这里使用swiperefersh布局和列表视图,并且在交换之后,我的列表中没有新数据。我已经尝试了很多次,但无法将其加点。请检查我下面的代码我做错了。我的api正在调用Onrefersh方法,但找不到新输入的数据。我在Google中搜索了很多报价和答案,但无法做到。请帮助我。
public class ChatActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
private SwipeRefreshLayout swipeRefreshLayout;
private static final String TAG = "ChatActivity";
String Api_Domain,chatListURL,sendMsgURL;
private ChatArrayApapter chatArrayAdapter;
private ListView listView;
private EditText chatText;
private ImageView buttonSend;
ProgressDialog progrss;
public String group;
public String groupId;
android.support.v7.app.ActionBar groupName;
JSONArray jsonArrayChat;
User user;
Session session;
private int pageCount = 1;
private int entriesPerPage = 20;
private int totalMsgs;
public static Activity reference;
NavigationView navigationView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
reference = this;// do not delete this line
// intialize the views and adapter
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(this);
chatArrayAdapter = new ChatArrayApapter(getApplicationContext(), R.layout.right);
listView.setAdapter(chatArrayAdapter);
chatText = (EditText) findViewById(R.id.msg);
chatText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
return sendChatMessage1();
}
return false;
}
});
listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
listView.setAdapter(chatArrayAdapter);
//listView.setOnScrollListener(onScrollListener());
//to scroll the list view to bottom on data change
chatArrayAdapter.registerDataSetObserver(new DataSetObserver () {
@Override
public void onChanged() {
super.onChanged();
listView.setSelection(chatArrayAdapter.getCount() - 1);
}
});
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
getChatList1();
}
}
);
}
// call chat list api
private void getChatList1(){
//if ((chatArrayAdapter.getCount() < totalMsgs && listView.getFirstVisiblePosition()== 0)|| (pageCount==1)) {
RequestQueue mrequestQueue = Volley.newRequestQueue (ChatActivity.this);
StringRequest stringRequest = new StringRequest (Request.Method.POST, chatListURL, new Response.Listener <String> ( ) {
@Override
public void onResponse(String res) {
swipeRefreshLayout.setRefreshing(false);
try {
JSONObject response = new JSONObject (res);
jsonArrayChat = response.getJSONArray("groupchatlist");
if(jsonArrayChat.length()>0){
pageCount++;
}
for (int i = 0; i < jsonArrayChat.length(); i++) {
JSONObject chatObj = (JSONObject) jsonArrayChat.get(i);
if (chatObj.getString("user_emailid").equals(user.email)) {
chatArrayAdapter.add(true,new ChatMessage(false, chatObj.getString("message"), chatObj.getString("username")));
} else {
chatArrayAdapter.add(true,new ChatMessage(true, chatObj.getString("message"), chatObj.getString("username")));
}
}
try {
JSONArray count = response.getJSONArray("totalcount");
JSONObject cnt = (JSONObject) count.get(0);
totalMsgs = cnt.getInt("count(id)");
} catch (JSONException e) {
e.printStackTrace();
totalMsgs = 500;
}
} catch (JSONException e) {
e.printStackTrace ( );
}
}
}, new Response.ErrorListener ( ) {
@Override
public void onErrorResponse(VolleyError error) {
Log.i ("Tag", "Response: " + error.toString ( ));
}
}
) {
@Override
protected Map<String, String> getParams() {
Map <String, String> params = new HashMap<> ( );
//params.put("group", "grp-1");
params.put ("group", groupId);
params.put("pageNo", String.valueOf(pageCount));
params.put("entriesPerPage", String.valueOf(entriesPerPage));
return params;
}
};
mrequestQueue.add (stringRequest);
/*}else{
swipeRefreshLayout.setRefreshing(false);
}*/
}
// refresh method
@Override
public void onRefresh() {
getChatList1();
}
答案 0 :(得分:1)
将所有对象添加到其中后,您必须调用chatArrayAdapter.notifyDataSetChanged()
:
for (int i = 0; i < jsonArrayChat.length(); i++) {
JSONObject chatObj = (JSONObject) jsonArrayChat.get(i);
if (chatObj.getString("user_emailid").equals(user.email)) {
chatArrayAdapter.add(true,new ChatMessage(false, chatObj.getString("message"), chatObj.getString("username")));
} else {
chatArrayAdapter.add(true,new ChatMessage(true, chatObj.getString("message"), chatObj.getString("username")));
}
}
chatArrayAdapter.notifyDataSetChanged() // <-- add this
答案 1 :(得分:0)
在将数据添加到适配器中后尝试此代码。
adpater.notifyDataSetChanged();
答案 2 :(得分:0)
Server会花一些响应时间来发送响应。因此,我建议您添加等待几毫秒的时间,然后再次单击Runnable。