1

我有一个线程,一旦将新元素添加到我的数据库中,我就会收到通知,显示通知并且一切正常,但我的问题是如何从该通知转到 Viewpager 中添加的最后一个片段?

公共类 AllProductsActivity 扩展 BaseActivity 实现 OnTaskFinishedListener{

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 <Ordinateur> ordinateurList = new ArrayList<Ordinateur>();
private List<Fragment> fragments = new Vector<Fragment>();

private int new_row, initial_row;
private static final int ID_NOTIFICATION = 1990;
private boolean condition = false;
private ViewPager vp;
private ArrayList<Fragment> 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<String, String, String> {

    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<NameValuePair> params = new ArrayList<NameValuePair>();
        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 < 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<String, String, String> {

    OnTaskFinishedListener onTaskFinishedListener;
    Ordinateur ordinateur = new Ordinateur();

    public LoadLastProducts(OnTaskFinishedListener _onTaskFinishedListener)
    {
        onTaskFinishedListener = _onTaskFinishedListener;
    }

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

    protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        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 < 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 <Void, Void, String> {

    protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
        InputStream in = entity.getContent();
        StringBuffer out = new StringBuffer();
        int n = 1;
        while (n>0) {
            byte[] b = new byte[4096];
            n =  in.read(b);
            if (n>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 > new_row) {
            if (new_row > 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<Fragment>();
        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);
}
}
4

1 回答 1

1

您可以通过调用更改 ViewPager 中显示的项目setCurrentItem(int)。如果这是您可以调用的最后一项:

mViewPager.setCurrentItem(mViewPager.getCount() - 1);

于 2013-01-10T12:29:51.733 回答