不首次在RecyclerView中显示数据

时间:2016-02-13 15:07:00

标签: android

我使用volley库来获取RcyclerView中的json数据但是当我转到活动时,不要第一次显示任何数据,我应该回到之前的活动并再次转到它以显示数据。我吗?

public class EventActivity extends AppCompatActivity {

    private String jsonResponseEventName;
    private String jsonResponseEventStartDate;
    private String  jsonResponseEventEndDate;
    private String jsonResponseEventTime;
    private String jsonResponseEventLocation;
    private String jsonResponseEventOwner;
    private String jsonResponseEventDes;
    private String jsonResponseEventPic;
    //--------------------------------
    private ProgressDialog pDialog;
    //-------------------------
    private MyDatabase db;
    //----------------------------

    private String urlJsonArray="http://kazeroon.mosbate16.com/event.php";
    private static String TAG = ListActivity.class.getSimpleName();
    static List<String> eventNameItems = new ArrayList<String>();
    static List<String> eventStartDateItems = new ArrayList<String>();
    static List<String> eventEndDateItems = new ArrayList<String >();
    static List<String> eventTimeItems = new ArrayList<String>();
    static List<String> eventLocationItems = new ArrayList<String>();
    static List<String> eventOwnerItems = new ArrayList<String>();
    static List<String> eventDesItems = new ArrayList<String>();
    static List<String> eventPicOItems = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

            }
        });
        //------------

        db=new MyDatabase(this);
        pDialog = new ProgressDialog(EventActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);


       new GetData().execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_event, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_send_event){
            Intent sendEventActivity =new Intent(EventActivity.this,SendEventActivity.class);
            startActivity(sendEventActivity);
            return true;
        }
        if (id == R.id.action_exit) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void setaddapter(){
        //tarif class adapter recyclview
        RecyclerView recyclerViewlist=(RecyclerView) findViewById(R.id.eventList);
        recyclerViewlist.setAdapter(new RVAdapterEvent(this, eventNameItems, eventStartDateItems, eventEndDateItems,
                eventTimeItems, eventLocationItems, eventOwnerItems, eventDesItems, eventPicOItems));
        //tarif layout maneger baraye tarif noe nemayesh
        recyclerViewlist.setLayoutManager(new LinearLayoutManager(this));
    }

private class GetData extends AsyncTask<String,Integer,String >{


    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        showpDialog();

    }

    @Override
    protected String doInBackground(String ... params) {
        JsonArrayRequest req = new JsonArrayRequest(urlJsonArray,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());

                        try {
                            // Parsing json array response
                            // loop through each json object
                            eventNameItems.clear();
                            eventStartDateItems.clear();
                            eventEndDateItems.clear();
                            eventTimeItems.clear();
                            eventLocationItems.clear();
                            eventOwnerItems.clear();
                            eventDesItems.clear();
                            eventPicOItems.clear();
                            db.Querry("DELETE FROM tbl_event");

                            for (int i = 0; i < response.length(); i++) {

                                JSONObject person = (JSONObject) response.get(i);

                                String name = person.getString("name");
                                String startdate = person.getString("startdate");
                                String enddate = person.getString("enddate");
                                String time = person.getString("time");
                                String location = person.getString("location");
                                String owner = person.getString("owner");
                                String des = person.getString("des");
                                String pic = person.getString("pic");



                                db.Querry("INSERT INTO tbl_event ( name , startdate , enddate , time , location , owner , des , pic ) " +
                                        "VALUES ('"+name+"','"+startdate+"','"+enddate+"','"+time+"','"+location+"','"+owner+"','"+des+"','"+pic+"' )");


                                // Toast.makeText(ListActivity.this,name,Toast.LENGTH_SHORT).show();
                                jsonResponseEventName =  name;
                                jsonResponseEventStartDate = startdate;
                                jsonResponseEventEndDate = enddate;
                                jsonResponseEventTime= time;
                                jsonResponseEventLocation=location;
                                jsonResponseEventOwner = owner;
                                jsonResponseEventDes = des;
                                jsonResponseEventPic = pic;

                                eventNameItems.add(jsonResponseEventName);
                                eventStartDateItems.add(jsonResponseEventStartDate);
                                eventEndDateItems.add(jsonResponseEventEndDate);
                                eventTimeItems.add(jsonResponseEventTime);
                                eventLocationItems.add(jsonResponseEventLocation);
                                eventOwnerItems.add(jsonResponseEventOwner);
                                eventDesItems.add(jsonResponseEventDes);
                                eventPicOItems.add(jsonResponseEventPic);



                            }




                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                            Toast.makeText(getApplicationContext(),
                                    "ارتباط با سرور برقرار نشد،آخرین اطلاعات دریافتی نمایش داده می شود",
                                    Toast.LENGTH_LONG).show();
                            //---------------------------------------------------------------------

                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(),
                        "ارتباط با سرور برقرار نشد،آخرین اطلاعات دریافتی نمایش داده می شود",
                        Toast.LENGTH_LONG).show();


            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(req);
        return null;


    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

            hidepDialog();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    setaddapter();
                }
            });

    }
}
    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}

1 个答案:

答案 0 :(得分:0)

由于您使用的是异步方法,因此必须通知数据集已更改。

只需在adapter.notifyDataSetChanged()方法中致电setAddapter()