我希望显示按页面分隔的数据列表,首先我要显示5个详细信息,然后当用户向下滚动时再通过再次调用api加载5个以上我已经能够显示前5个细节但是当滚动它取代之前的5个新的5个细节,它不是添加数据而是替换它。
我想要显示数据的MainFile
package com.example.vinod.lcoportal;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class StbDetailsFragment extends Fragment {
RecyclerView recycler_view_stb_details;
ArrayList<StbDetails> listitems_stb = new ArrayList<>();
String[] name = new String[50];
String[] address = new String[100];
String[] vc_stb = new String[100];
String[] city = new String[100];
String[] current_plan = new String[200];
String[] status = new String[50];
String[] csi = new String[300];
int currentpage = 0;
int pagesize = 5;
ProgressDialog pDialog;
JSONArray ja;
JSONObject jo;
String custcsi, custcity, custname, currentplan, custstatus, vc_no;
String stb_url = "http://lco.denonline.in/wapp/Service1.svc/Dashboard";
public StbDetailsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_stb_details, container, false);
currentpage = currentpage + 1;
GetStb getStb= new GetStb(currentpage, pagesize);
getStb.execute(stb_url);
return view;
}
public class MyAdapterStb extends RecyclerView.Adapter<MyAdapterStb.MyViewHolderStb> {
private ArrayList<StbDetails> list;
public MyAdapterStb(ArrayList<StbDetails> Data) {
list = Data;
}
@Override
public MyViewHolderStb onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.stb_details_cardview, parent, false);
MyViewHolderStb holder = new MyViewHolderStb(view);
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolderStb holder, int position) {
holder.cust_name.setText(list.get(position).getName());
holder.cust_address.setText(list.get(position).getAddress());
holder.cust_vc.setText(list.get(position).getVc_stb_no());
holder.cust_city.setText(list.get(position).getCity());
holder.cust_plan.setText(list.get(position).getCurrentPlan());
holder.cust_status.setText(list.get(position).getStatus());
// holder.events_imageview.setImageResource(list.get(position).getImageResourceId());
// holder.events_imageview.setTag(list.get(position).getImageResourceId());
// Glide.with(getActivity()).load(list.get(position).getImageResourceId_notices()).into(holder.notices_imageview);
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolderStb extends RecyclerView.ViewHolder {
TextView cust_name, cust_address, cust_vc, cust_city, cust_plan, cust_status ;
public MyViewHolderStb(View v) {
super(v);
cust_name = (TextView) v.findViewById(R.id.name_stb_details);
cust_address = (TextView) v.findViewById(R.id.address_stb_details);
cust_vc = (TextView) v.findViewById(R.id.vc_stb_no_stb_details);
cust_city = (TextView) v.findViewById(R.id.city_stb_details);
cust_plan = (TextView) v.findViewById(R.id.current_plan_stb_details);
cust_status = (TextView) v.findViewById(R.id.status_stb_details);
// v.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Intent i = new Intent(getActivity(),NoticesViewActivity.class);
// i.putExtra("notice_url",pdf[getAdapterPosition()]);
// i.putExtra("notice_title",title_notices[getAdapterPosition()]);
// startActivity(i);
// }
// });
}
}
}
public void initializeList() {
listitems_stb.clear();
for (int i = 0; i < ja.length(); i++) {
StbDetails item = new StbDetails();
item.setName(name[i]);
item.setAddress(address[i]);
item.setVc_stb_no(vc_stb[i]);
item.setCity(city[i]);
item.setCurrentPlan(current_plan[i]);
item.setStatus(status[i]);
listitems_stb.add(item);
}
}
public class GetStb extends AsyncTask<String,String,String> {
HttpURLConnection httpURLConnection;
int CurrentPage, PageSize;
public GetStb(int currentpage, int pagesize) {
CurrentPage = currentpage;
PageSize = pagesize;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Fetching Data...Please Wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//pDialog.show();
// progress_dialog.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(params[0]);
Log.d("DoInBackground:URL", url.toString());
//Send Post Data request
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.addRequestProperty("api_key", "XBoGycClZkJrXDVphgpN5c9Bb82fcKQ4");
httpURLConnection.addRequestProperty("gulco", "fe96632f-5173-e611-942d-005056bb1e58");
httpURLConnection.addRequestProperty("currentpage", String.valueOf(CurrentPage));
httpURLConnection.addRequestProperty("pagesize", String.valueOf(PageSize));
//)httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoInput(true);
httpURLConnection.connect();
//Get the Server Response
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
response += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
Log.d("DoInBackground", "Response:" + response);
return response;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String response) {
GridLayoutManager MyLayoutManager = new GridLayoutManager(getActivity(),1);
MyLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
recycler_view_stb_details = (RecyclerView) getActivity().findViewById(R.id.recycler_view_stb_details);
recycler_view_stb_details.setHasFixedSize(true);
try {
ja = new JSONArray(response);
//ja = jo.getJSONArray("notices");
//images = new String[ja.length()];
//title = new String[ja.length()];
for (int i=0;i<ja.length();i++) {
JSONObject json = ja.getJSONObject(i);
custcsi = json.getString("CSI");
custcity = json.getString("City");
custname = json.getString("CustName");
currentplan = json.getString("Plan");
custstatus = json.getString("Status");
vc_no = json.getString("VCNO");
name[i] = custname;
csi[i] = custcsi;
city[i] = custcity;
current_plan[i] = currentplan;
vc_stb[i] = vc_no;
status[i] = custstatus;
initializeList();
}
} catch (Exception e) {
e.printStackTrace();
}
//progress_dialog.setVisibility(View.GONE);
//pDialog.dismiss();
if (listitems_stb.size() > 0 & recycler_view_stb_details != null) {
recycler_view_stb_details.setAdapter(new MyAdapterStb(listitems_stb));
}
recycler_view_stb_details.setLayoutManager(MyLayoutManager);
//pDialog.dismiss();
}
}
}
答案 0 :(得分:0)
我认为您的问题是每次public void initializeList()
首先调用listitems_stb.clear();
,所以每次都会清除您的列表,并且您将丢失以前的所有数据。
因此,如果您想在列表中添加其他元素,则不应该清除它。只是尝试删除listitems_stb.clear();
。
然后分页看到这个答案here。
希望这有帮助