Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can i go to the last fragment added in my Viewpager from a notification?
    primarykey
    data
    text
    <p>I have a thread that once a new elemnt is added to my databse i get a notification, the notification is shown and it's all ok but my problem is how can i go to the last fragment added in my Viewpager from this notification?</p> <p>public class AllProductsActivity extends BaseActivity implements OnTaskFinishedListener{</p> <pre><code>public AllProductsActivity() { super(1); } private ProgressDialog pDialog; private JSONParser jParser = new JSONParser(); private static final String TAG_SUCCESS = "success"; private static final String TAG_PRODUCTS = "ordinateur"; private static final String TAG_PRICE = "prix_ordi"; private static final String TAG_NAME = "nom_ordi"; private static final String TAG_MARK = "marque_ordi"; private static final String TAG_PROC = "proc_ordi"; private static final String TAG_DISK = "dd_ordi"; private static final String TAG_RAM = "ram_ordi"; private static final String TAG_GRAPHICS = "gra_ordi"; private static final String TAG_DESC = "des_ordi"; private static final String TAG_PATH = "path_ordi"; private MyPagerAdapter mPagerAdapter; private JSONArray products = null; private ArrayList &lt;Ordinateur&gt; ordinateurList = new ArrayList&lt;Ordinateur&gt;(); private List&lt;Fragment&gt; fragments = new Vector&lt;Fragment&gt;(); private int new_row, initial_row; private static final int ID_NOTIFICATION = 1990; private boolean condition = false; private ViewPager vp; private ArrayList&lt;Fragment&gt; mFragments; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); vp = new ViewPager(this); vp.setId(R.id.pager); new LoadAllProducts(this).execute(url_all_products); setContentView(vp); ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5); scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { public void run() { new LongRunningGetIO().execute(); } }, 0, 5, TimeUnit.SECONDS); vp.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int position) { switch (position) { case 0: getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); break; default: getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN); break; } } }); vp.setCurrentItem(0); getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); } @Override public void onTaskFinishedOrdinateur() { vp.setAdapter(new OrdinateurPagerAdapter(getSupportFragmentManager())); } @Override public void onTaskFinishedLastOrdinateur(Ordinateur ordinateur) { vp.setAdapter(new OrdinateurPagerAdapter(getSupportFragmentManager())); createNotify(ordinateur.getName()); } class LoadAllProducts extends AsyncTask&lt;String, String, String&gt; { OnTaskFinishedListener onTaskFinishedListener; public LoadAllProducts(OnTaskFinishedListener _onTaskFinishedListener) { onTaskFinishedListener = _onTaskFinishedListener; } @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(AllProductsActivity.this); pDialog.setMessage("Chargement des produits, , veuillez patienter..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); pDialog.show(); } protected String doInBackground(String... args) { List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); JSONObject json = jParser.makeHttpRequest(args[0], "GET", params); Log.d("json", json.toString()); try { int success = json.getInt(TAG_SUCCESS); if (success == 1) { products = json.getJSONArray(TAG_PRODUCTS); for (int i = 0; i &lt; products.length(); i++) { JSONObject c = products.getJSONObject(i); String price = c.getString(TAG_PRICE); String name = c.getString(TAG_NAME); String ram = c.getString(TAG_RAM); String dd = c.getString(TAG_DISK); String gra = c.getString(TAG_GRAPHICS); String proc = c.getString(TAG_PROC); String mark = c.getString(TAG_MARK); String desc = c.getString(TAG_DESC); String path = c.getString(TAG_PATH); Ordinateur ordinateur = new Ordinateur(); ordinateur.setPrice(price); ordinateur.setName(name); ordinateur.setRam(ram); ordinateur.setDd(dd); ordinateur.setGra(gra); ordinateur.setProc(proc); ordinateur.setMark(mark); ordinateur.setDesc(desc); ordinateur.setPath(path); ordinateurList.add(ordinateur); } } else { // no products found } } catch (JSONException e) { e.printStackTrace(); } return null; } protected void onPostExecute(String file_url) { pDialog.dismiss(); onTaskFinishedListener.onTaskFinishedOrdinateur(); } } class LoadLastProducts extends AsyncTask&lt;String, String, String&gt; { OnTaskFinishedListener onTaskFinishedListener; Ordinateur ordinateur = new Ordinateur(); public LoadLastProducts(OnTaskFinishedListener _onTaskFinishedListener) { onTaskFinishedListener = _onTaskFinishedListener; } @Override protected void onPreExecute() { super.onPreExecute(); } protected String doInBackground(String... args) { List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); JSONObject json = jParser.makeHttpRequest(args[0], "GET", params); Log.d("json", json.toString()); try { int success = json.getInt(TAG_SUCCESS); if (success == 1) { products = json.getJSONArray(TAG_PRODUCTS); for (int i = 0; i &lt; products.length(); i++) { JSONObject c = products.getJSONObject(i); String price = c.getString(TAG_PRICE); String name = c.getString(TAG_NAME); String ram = c.getString(TAG_RAM); String dd = c.getString(TAG_DISK); String gra = c.getString(TAG_GRAPHICS); String proc = c.getString(TAG_PROC); String mark = c.getString(TAG_MARK); String desc = c.getString(TAG_DESC); String path = c.getString(TAG_PATH); ordinateur.setPrice(price); ordinateur.setName(name); ordinateur.setRam(ram); ordinateur.setDd(dd); ordinateur.setGra(gra); ordinateur.setProc(proc); ordinateur.setMark(mark); ordinateur.setDesc(desc); ordinateur.setPath(path); ordinateurList.add(ordinateur); } } else { // no products found } } catch (JSONException e) { e.printStackTrace(); } return null; } protected void onPostExecute(String file_url) { onTaskFinishedListener.onTaskFinishedLastOrdinateur(ordinateur); } } class LongRunningGetIO extends AsyncTask &lt;Void, Void, String&gt; { protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException { InputStream in = entity.getContent(); StringBuffer out = new StringBuffer(); int n = 1; while (n&gt;0) { byte[] b = new byte[4096]; n = in.read(b); if (n&gt;0) out.append(new String(b, 0, n)); } return out.toString(); } @Override protected void onPreExecute() { new_row = initial_row; } @Override protected String doInBackground(Void... params) { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet(url_row); String text = null; try { HttpResponse response = httpClient.execute(httpGet, localContext); HttpEntity entity = response.getEntity(); text = getASCIIContentFromEntity(entity); } catch (Exception e) { return e.getLocalizedMessage(); } return text; } protected void onPostExecute(String results) { initial_row = Integer.parseInt(results); if (initial_row &gt; new_row) { if (new_row &gt; 0){ Log.d("added", "added"); new LoadLastProducts(AllProductsActivity.this).execute(url_last_product); } } } } public class OrdinateurPagerAdapter extends FragmentPagerAdapter { public OrdinateurPagerAdapter(FragmentManager fm) { super(fm); mFragments = new ArrayList&lt;Fragment&gt;(); for (Ordinateur ordinateur : ordinateurList) mFragments.add(new OrdinateurFragment(ordinateur)); } @Override public int getCount() { return mFragments.size(); } @Override public Fragment getItem(int position) { return mFragments.get(position); } } public void createNotify(String texteNotification){ NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Promo", System.currentTimeMillis()); notification.flags = Notification.FLAG_ONGOING_EVENT; Intent intent = new Intent(this, Main.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0); String titreNotification = "Promo Agora"; notification.setLatestEventInfo(this, titreNotification, texteNotification, pendingIntent); notification.vibrate = new long[] {0,200,100,200,100,200}; notificationManager.notify(ID_NOTIFICATION, notification); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload