1

Bear my English

Whenever any data is added in my database then on refresh the data should be reflected.

And one more thing can you help me Regarding how do I create notification if any new data is Added

I am using:

1) Xampp for Apache

2) Microsoft SQL Server

This is my RecyclerActivtiy Code:

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) {
            }

        }
    }
}

This the code for my Adapter Code:

 @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 );
}

Here the problem is how implement pull down to refresh function in my recyclerview.

And Where to write function for it whether in RecyclerActivity or in Adapter. Can anyone help me out regarding this problem.

bunbun
  • 2,595
  • 3
  • 34
  • 52
Nikhil Lohar
  • 127
  • 1
  • 9

1 Answers1

5

You can wrap you RecyclerView with the SwipeRefreshLayout for making a pull to refresh.

<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>

And you should listen to refresh events for the SwipeRefreshLayout:

refreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        // Load data to your RecyclerView
        refreshData();
    }
});
Metehan Toksoy
  • 1,885
  • 3
  • 22
  • 39