![](/img/trans.png)
[英]I want my recyclerview to show limited cards through condition but all items keep getting shown
[英]Show limited items in RecyclerView
請用我的英語
我的數據庫中總共有3000個條目,我想在recyclerView中顯示所有這些條目。
我的recyclerView的代碼:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_complaint_reg );
swipe = (SwipeRefreshLayout) findViewById( R.id.swipe_refresh_complaint );
swipe.setOnRefreshListener(complaintReg.this );
swipe.post( new Runnable() {
@Override
public void run() {
swipe.setRefreshing( true );
fetchdata();
}
} );
}
public void fetchdata(){
swipe.setRefreshing( true );
new AsyncLogin().execute();
swipe.setRefreshing( false );
}
@Override
public void onRefresh() {
fetchdata();
}
private class AsyncLogin extends AsyncTask<String,Void,String> {
ProgressDialog pdloding = new ProgressDialog(complaintReg.this);
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute(){
super.onPreExecute();
pdloding.setMessage( "\tLoading.." );
pdloding.setCancelable( false );
pdloding.show();
}
@Override
protected String doInBackground(String... strings) {
try {
url = new URL("http://localhost/SearchComp.php");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return ("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result){
Log.e( TAG,"result"+result );
pdloding.dismiss();
List<dataRegComplaint> data = new ArrayList<>( );
pdloding.dismiss();
if(result.equals( "No complaint assgin" )){
Toast.makeText( complaintReg.this, "No Complaint Assign", Toast.LENGTH_SHORT ).show();
}else{
try {
JSONArray jArray = new JSONArray(result);
// Extract data from json and store into ArrayList as class objects
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
dataRegComplaint fishData = new dataRegComplaint(
json_data.getString("cust_name"),
json_data.getString("CatName"),
json_data.getString("site_Name"),
json_data.getString( "SrcName" ));
data.add(fishData);
Log.e( TAG, "DATA reesult :"+data );
}
// cAdapter.notifyDataSetChanged();
// Setup and Handover data to recyclerview
Register_complaints = (RecyclerView)findViewById(R.id.Reg_Complaint_List);
Reg_Adapter = new Register_Adapter(complaintReg.this,data);
Register_complaints.setAdapter(Reg_Adapter);
Register_complaints.setLayoutManager(new LinearLayoutManager(complaintReg.this));
} catch (JSONException e) {
}
}
}
}
我的適配器的代碼:
public class Register_Adapter extends RecyclerView.Adapter<Register_Adapter.MyHolder> {
private Context context;
List<dataRegComplaint> data = Collections.emptyList();
static int total;
View v;
public Register_Adapter(complaintReg complaintReg,List<dataRegComplaint> data) {
this.context = complaintReg;
this.data = data;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.container_register, viewGroup, false);
return new MyHolder( v );
}
@Override
public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
final dataRegComplaint current=data.get(i);
myHolder.client.setText(current.getClientName());
myHolder.location.setText("Reason: " + current.getAddress());
myHolder.product.setText("Client : " + current.getProduct());
myHolder.category.setText("Location: " + current.getCategory());
}
@Override
public int getItemCount() {
return data.size();
}
public class MyHolder extends RecyclerView.ViewHolder{
TextView client,location,product,category;
Button complaint_register;
public MyHolder(@NonNull View itemView) {
super( itemView );
client = (TextView) itemView.findViewById( R.id.textclient );
location = (TextView) itemView.findViewById( R.id.textlocation );
product = (TextView) itemView.findViewById( R.id.textproduct );
category = (TextView) itemView.findViewById( R.id.textcategory );
complaint_register = (Button) itemView.findViewById( R.id.button_register );
}
}
}
因此,每當我嘗試在recyclerview中顯示所有條目時,應用程序就會崩潰,因為它無法一次加載所有項目。
在這種情況下,誰能幫助我。
Movie.java->用您的ModelClass替換
PaginationAdapter->用您的適配器替換[在PaginationAdapter中包括左側方法(檢查適配器)]
PaginationScrollListener->保持不變[不變]
在mainActivity.java中
從服務器獲取數據到應用程序后,loadFirstPage()->調用此方法。
列出電影= Movie.createMovies(adapter.getItemCount());
您將在loadFirstPage()方法中找到此行代碼。 在此方法中,Movie-> ModelClass&createMovies()->靜態方法可以一次提供10個項目。 您可以在for循環中增加計數
XML
item_list.xml->您的recyclerview行xml
item_progress.xml->保持不變
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.