请用我的英语
我的数据库中总共有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中显示所有条目时,应用就会崩溃,因为它无法一次加载所有项目。
在这种情况下,谁能帮助我。
答案 0 :(得分:0)
download the project here and follow below mention steps.
Movie.java->用您的ModelClass替换
PaginationAdapter->用您的适配器替换[包括左方法 在PaginationAdapter中(检查适配器)]
PaginationScrollListener->保持不变[不变]
在mainActivity.java中
loadFirstPage()->从您的计算机中获取数据后调用此方法 服务器连接到您的应用。
列出电影= Movie.createMovies(adapter.getItemCount());
您将在loadFirstPage()方法中找到此行代码。在 电影-> ModelClass&createMovies()->静态方法 一次给您10件商品。 您可以在循环中增加计数
xml
item_list.xml->您的recyclerview行xml
item_progress.xml->保持不变