下拉以在RecyclerView中刷新

时间:2018-10-15 06:16:35

标签: android android-recyclerview swipe

懂我的英语

无论何时将任何数据添加到我的数据库中,那么刷新时都应反映出数据。

还有一件事可以帮我吗?关于添加任何新数据时如何创建通知

我正在使用:

1)Xampp for Apache

2)Microsoft SQL Server

这是我的RecyclerActivtiy 代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_engineer_recycler );
    Bundle bundle = getIntent().getExtras();
    service_id = bundle.getString( "empid" );
    type_id = bundle.getString( "type" );
    if(type_id.equals( "pending" )){
        this.setTitle( "Pending Complaint " );
    }else if(type_id.equals( "history" )){
        this.setTitle( "Complaint History" );
    }

    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat format = new SimpleDateFormat( "dd/MM/yyyy" );
    String time = format.format( calendar.getTime() );
    Log.e( TAG, "Time" +time );

    new AsyncLogin().execute(service_id,type_id, time);
}

private class AsyncLogin extends AsyncTask<String, String, String> {

    ProgressDialog pdloding = new ProgressDialog(EngineerRecycler.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 {
            serviceid = (String) strings[0];
            typeid = (String) strings[1];
            time1 = (String) strings[2];

            if(typeid.equals( "pending" )){
                url = new URL("http://localhost/players.php");
            }else if(typeid.equals( "history" )){
                url = new URL("http:/localhost/StudentDetails.php");

            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return e.toString();
        }
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            OutputStream outputStream = conn.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter( new OutputStreamWriter( outputStream, "UTF-8" ) );
            String post_data = URLEncoder.encode( "serviceid","UTF-8" ) + "=" + URLEncoder.encode( serviceid, "UTF-8" ) + "&"
                    +URLEncoder.encode( "time1","UTF-8" ) + "=" + URLEncoder.encode( time1, "UTF-8" );
            bufferedWriter.write( post_data );
            bufferedWriter.flush();
            bufferedWriter.close();
            Log.e( TAG, "POST DATA "+post_data  );

        } 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) {
                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<DataComplaint> data = new ArrayList<>(  );
        pdloding.dismiss();
        if(result.equals( "No complaint assgin null" )){
            Toast.makeText( EngineerRecycler.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);
                    DataComplaint fishData = new DataComplaint(
                            json_data.getString("comp_desc"),
                            json_data.getString("comp_reason"),
                            json_data.getString("ClientName"),
                            json_data.getString( "SiteName" ),
                            json_data.getString( "comp_ticketid" ));

                    data.add(fishData);
                }
                cAdapter.notifyDataSetChanged();
                // Setup and Handover data to recyclerview
                complaints = (RecyclerView)findViewById(R.id.complaintList);
                cAdapter = new complaintAdapter(EngineerRecycler.this, data,service_id, type_id);
                complaints.setAdapter(cAdapter);


                complaints.setLayoutManager(new LinearLayoutManager(EngineerRecycler.this));

            } catch (JSONException e) {
            }

        }
    }
}

这是我的适配器的代码 代码:

 @Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.container_complaint, viewGroup, false);
    return new MyHolder( v );
}

@Override
public void onBindViewHolder(@NonNull final MyHolder myHolder, final int i) {
    final DataComplaint current=data.get(i);

    myHolder.textcomplaint.setText(current.complaint);
    myHolder.textaddress.setText("Reason: " + current.getAddress());
    myHolder.textType.setText("Client   : " + current.getComplaint_type());
    myHolder.textplace.setText("Location: " + current.getLocation());
    myHolder.textticket.setText( current.getTicket());
    myHolder.linearLayout.setVisibility( View.GONE );
}

 @Override
public int getItemCount() {

    return data.size();

}
public class MyHolder extends RecyclerView.ViewHolder {
    TextView textcomplaint;
    TextView textaddress;
    TextView textType,textplace, textticket, textreso;
    Button btn, btn1, btn2;
    int value1;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    LinearLayout linearLayout;
    RelativeLayout relativeView;
    public MyHolder(@NonNull View itemView) {
        super( itemView );
        textcomplaint = (TextView) itemView.findViewById( R.id.textcomplaint );
        textaddress = (TextView) itemView.findViewById( R.id.textaddress );
        textType = (TextView) itemView.findViewById( R.id.textType );
        textticket = (TextView) itemView.findViewById( R.id.ticketid );
        textplace = (TextView) itemView.findViewById( R.id.textplace );
        btn = (Button) itemView.findViewById( R.id.enter );
        btn1 = (Button) itemView.findViewById( R.id.repositry );
        btn2 = (Button) itemView.findViewById( R.id.exit );
        linearLayout = (LinearLayout) itemView.findViewById( R.id.linear_layout );
        relativeView = (RelativeLayout) itemView.findViewById( R.id.view );
}

这里的问题是在我的recyclerview中如何实现下拉菜单以刷新功能。

以及在RecyclerActivity或Adapter中为其编写函数的位置。 谁能帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

您可以使用SwipeRefreshLayout将RecyclerView包裹起来,以进行刷新。

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/refreshView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>

并且您应该监听SwipeRefreshLayout的刷新事件:

refreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        // Load data to your RecyclerView
        refreshData();
    }
});