繁体   English   中英

在RecyclerView中显示有限的物品

[英]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中显示所有条目时,应用程序就会崩溃,因为它无法一次加载所有项目。

在这种情况下,谁能帮助我。

在此处下载项目,然后按照以下提及的步骤操作。

  1. Movie.java->用您的ModelClass替换

  2. PaginationAdapter->用您的适配器替换[在PaginationAdapter中包括左侧方法(检查适配器)]

  3. PaginationScrollListener->保持不变[不变]

在mainActivity.java中

  1. 从服务器获取数据到应用程序后,loadFirstPage()->调用此方法。

  2. 列出电影= Movie.createMovies(adapter.getItemCount());

    您将在loadFirstPage()方法中找到此行代码。 在此方法中,Movie-> ModelClass&createMovies()->静态方法可以一次提供10个项目。 您可以在for循环中增加计数

XML

  1. item_list.xml->您的recyclerview行xml

  2. item_progress.xml->保持不变

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM