我想刷新我的片段以显示当前数据但是更改数据太慢了。所以我的问题是如何在获取数据的同时更快地更改它,更改应该是快速的,片段应该在它的第一个实例上,或者你可以在第一个片段上说
public class PlazaDetailsActivity extends AppCompatActivity {
public static ArrayList<String> plazaList;
ViewPager vpPager;
ProgressDialog dialog;
final Calendar calendar = Calendar.getInstance();
TextView actionBarTitle,actionBarDate;
DatePickerDialog datePickerDialog;
int year,month,day;
public static class MyPagerAdapter extends FragmentStatePagerAdapter {
private final ArrayList<String> plazaList;
public MyPagerAdapter(FragmentManager fragmentManager, ArrayList<String> plazaList) {
super(fragmentManager);
this.plazaList=plazaList;
Log.e("PLazalist",plazaList.toString());
}
// Returns total number of pages
@Override
public int getCount() {
return plazaList.size();
}
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
return PlazaListDetailsFragment.newInstance(0,plazaList.get(position));
}
}
MyPagerAdapter adapterViewPager;
PlazaDetails plazaDetails;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plaza_details);
plazaDetails = getIntent().getParcelableExtra("PLAZA");
plazaList = new ArrayList<>();
Toolbar toolbar = (Toolbar) findViewById(R.id.tollConnectPlazaDetailsToolBar);
actionBarTitle = (TextView)findViewById(R.id.plazaName) ;
actionBarDate = (TextView)findViewById(R.id.dateText) ;
ImageButton calendarImageButton = (ImageButton) findViewById(R.id.calanderImage) ;
ImageButton backArrowButton = (ImageButton) findViewById(R.id.backArrowButton) ;
//calendarDilog view...///
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
String stringMonth = calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
String date = String.format("%d %s,%d",day,stringMonth,year);
Log.e(" Todays Date :",date);
actionBarDate.setText(date);
actionBarTitle.setText(plazaDetails.getPlazaName());
//plazaList = new ArrayList<>();
String url = TollConnectLiteApplication.userPrfrences.getString(TollConnectLiteConstants.BASE_URL,null)+"Plaza/GetGraph?PlazaID="+plazaDetails.getPlazaId()+"&Shift_Date="+day+"/"+month+"/"+year;
Log.e("Url",url);
new PlazaListAsyncTask().execute(url);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
vpPager = (ViewPager) findViewById(R.id.vpPager);
adapterViewPager = new MyPagerAdapter(getSupportFragmentManager(),plazaList);
vpPager.setAdapter(adapterViewPager);
calendarImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
datepicker();
}
});
backArrowButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
// handling Datepicker event................
public void datepicker(){
final Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
Log.e("DatePicker","INside calander button click");
datePickerDialog = new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
final Calendar calendar = Calendar.getInstance();
calendar.setTime( new Date(year,monthOfYear,dayOfMonth));
String month = calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
String selectedDate = String.format("%d %s,%d",dayOfMonth,month,year);
Log.e(" Todays Date :",selectedDate);
actionBarDate.setText(selectedDate);
String url = TollConnectLiteApplication.userPrfrences.getString(TollConnectLiteConstants.BASE_URL,null)+"Plaza/GetGraph?PlazaID="+plazaDetails.getPlazaId()+"&Shift_Date="+dayOfMonth+"/"+monthOfYear+"/"+year;
Log.e("Url",url);
new PlazaListAsyncTask().execute(url);
onResume();
}
},year,month,day);
datePickerDialog.show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// API 5+ solution
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onResume() {
super.onResume();
adapterViewPager.notifyDataSetChanged();
}
class PlazaListAsyncTask extends AsyncTask<String,Void,InputStream>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(PlazaDetailsActivity.this);
dialog.setTitle("Loading...");
dialog.show();
}
@Override
protected InputStream doInBackground(String... params) {
Log.e("Text","Inside doInBackground");
try {
String stringUrl = params[0];
URL url = new URL(stringUrl);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
return inputStream;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(InputStream inputStream) {
super.onPostExecute(inputStream);
Log.e("Text","Inside doInBackground");
InputStreamReader reader = new InputStreamReader(inputStream);
BufferedReader Rreader = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = Rreader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
String jsonResponse = sb.toString();
jsonResponse = jsonResponse.replaceAll("\\\\","");
Log.e("json",jsonResponse);
jsonResponse = jsonResponse.replace("\"{","{");
Log.e("json",jsonResponse);
jsonResponse = jsonResponse.replace("}\"","}");
Log.e("json",jsonResponse);
plazaList.clear();
if (plazaList.isEmpty()){
try {
JSONObject jsonObject = new JSONObject(jsonResponse);
JSONArray jsonArray = jsonObject.getJSONArray("Section");
for (int i=0 ;i<jsonArray.length();i++) {
DetailsActivityPlazaObject object = new DetailsActivityPlazaObject();
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
plazaList.add(jsonObject1.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.e("PLazalist",plazaList.toString());
adapterViewPager.notifyDataSetChanged();
onResume();
dialog.hide();
}
}
}