您好我想合并先前的记录以使用先前加载消息按钮与cardview中的当前数据合并。所有工作正常但数据被压缩。新数据是最高顺序,第一个旧数据属于新数据,第二个较旧的数据属于第一个较旧的数据,依此类推。我想以相反的顺序加载适配器。这是我的代码。每页包含30条记录。
multipleRowRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (linearLayoutManager.findFirstVisibleItemPosition() == 0) {
load.setVisibility(View.VISIBLE);
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t=t+1;
newdata(t);
load.setVisibility(View.GONE);
}
});
} else {
load.setVisibility(View.GONE);
}
}
});
public void newdata(int t) {
page= String.valueOf(t);
// Toast.makeText(getApplicationContext(),""+page,Toast.LENGTH_SHORT).show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();
try {
JSONObject homeobj = new JSONObject(response);
JSONObject insteller = homeobj.getJSONObject("INSTELLAR");
status = insteller.getString("STATUS");
if(status.equals("success")) {
JSONArray data = insteller.getJSONArray("DATA");
for(int i=data.length()-1; i >=0; i--) {
JSONObject jsonObject = data.getJSONObject(i);
// msg = jsonObject.getString("MSG").toString();
typee=jsonObject.getString("TYPE").toString();
if(typee.equals("1"))
{
type = AppConstant.FIRST_ROW;
content = jsonObject.getString("MSG").toString();
date = jsonObject.getString("DATE").toString();
img = jsonObject.getString("IMAGE").toString();
}
else {
type = AppConstant.OTHER_ROW;
content = jsonObject.getString("MSG").toString();
date = jsonObject.getString("DATE").toString();
img = jsonObject.getString("IMAGE").toString();
}
multipleRowRecyclerView.setAdapter(multipleRowAdapter);
multipleRowModelList.add(new MultipleRowModel(type, content, date, img));
multipleRowRecyclerView.scrollToPosition(multipleRowModelList.size()-1);
}
multipleRowRecyclerView.scrollToPosition(multipleRowModelList.size()-30);
}
else
{
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Error in Json", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("clientid", KEY_ClientID);
params.put("adminid", KEY_ADMINID);
params.put("userid", KEY_USERID);
params.put("pageno", page);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
答案 0 :(得分:0)
你的MultipleRowModel类怎么样?
我认为您可以使用Queue并显示此队列中的记录。 像这样,
import java.util.LinkedList;
class GenQueue<E> {
private LinkedList<E> list = new LinkedList<E>();
public void enqueue(E item) {
list.addLast(item);
}
public E dequeue() {
return list.poll();
}
public boolean hasItems() {
return !list.isEmpty();
}
public int size() {
return list.size();
}
public void addItems(GenQueue<? extends E> q) {
while (q.hasItems())
list.addLast(q.dequeue());
}
}
答案 1 :(得分:0)
public void newdata(int t) {
page= String.valueOf(t);
pos=+30;
// Toast.makeText(getApplicationContext(),""+page,Toast.LENGTH_SHORT).show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();
try {
JSONObject homeobj = new JSONObject(response);
JSONObject insteller = homeobj.getJSONObject("INSTELLAR");
status = insteller.getString("STATUS");
if(status.equals("success")) {
JSONArray data = insteller.getJSONArray("DATA");
for(int i=data.length()-1; i >=0; i--) {
JSONObject jsonObject = data.getJSONObject(i);
// msg = jsonObject.getString("MSG").toString();
typee=jsonObject.getString("TYPE").toString();
if(typee.equals("1"))
{
type = AppConstant.FIRST_ROW;
content = jsonObject.getString("MSG").toString();
date = jsonObject.getString("DATE").toString();
img = jsonObject.getString("IMAGE").toString();
}
else {
type = AppConstant.OTHER_ROW;
content = jsonObject.getString("MSG").toString();
date = jsonObject.getString("DATE").toString();
img = jsonObject.getString("IMAGE").toString();
}
multipleRowRecyclerView.setAdapter(multipleRowAdapter);
multipleRowModelList.add(0,new MultipleRowModel(type, content, date, img));
}
multipleRowRecyclerView.scrollToPosition(multipleRowModelList.size()-pos);
}
else
{
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Error in Json", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("clientid", KEY_ClientID);
params.put("adminid", KEY_ADMINID);
params.put("userid", KEY_USERID);
params.put("pageno", page);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}