2015-04-02 9 views
0

amis Je construis une application pour Android et face à un problème en montrant dynamiquement ajouté TextViewer mon scénario est comme ça. J'utilise ResulReceiver pour recevoir notification et en ajoutant ce résultat dynamiquement dans TextView pour afficher.Quand mon activité est active I.e The application is keep open jusqu'à ce que cela fonctionne correctement. Lorsque mon activité ne se trouve pas dans le mode Active, je le stocke à DB (SQLLITE), mais lorsque je reviens à mon activité, je peux récupérer les données de la base de données et faire la même chose qu'avant, jusqu'à présent, il n'y a pas de problème. Mais il commence comme je reçois une nouvelle notification à ce moment-là n'est pas en mesure d'ajouter le nouveau TextView dynamiquement avec le existant, Mais lorsque le but de débogage j'utilise Toast, il montre correctement. Pourriez-vous s'il vous plaît m'aider. Merci d'avance. Ceci est mon codeImpossible d'ajouter TextView avec exixting un dynamiquement

@Override 
    public void onReceiveResult(int resultCode, Bundle resultData) { 
     // TODO Auto-generated method stub 
     String result = resultData.getString("notifyService"); 
     CustomActionBar.controlNotifyImage(this,mActionBar); 
     ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE); 
     List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1); 
     ComponentName componentInfo = taskInfo.get(0).topActivity; 
     if(!".activity.CatagoryChooserActivity".equals(componentInfo.getShortClassName())){ 
      notifyInfo = new NotifyInfo(); 
      notifyInfo.setNotifyPk(100000+new Long(CreateOrderService.notifyCount.get())); 
      notifyInfo.setNotifyTxt(result); 
      notifyInfo.setNotifyDate(new Date().toString()); 
      notifyInfo.setNotifyTime(new Date().toGMTString()); 
      SellerNotifyDB sellerNotifydb = SellerNotifyDB.getInstance(context); 
      sellerNotifydb.addNotification(notifyInfo); 
      Toast.makeText(this,"Notify :: "+CreateOrderService.notifyCount.get(),Toast.LENGTH_LONG).show(); 
     } 
     else { 
      runOnUiThread(new UpdateUI(result)); 
     } 
    } 
    class UpdateUI implements Runnable 
    { 
     String updateString; 

     public UpdateUI(String updateString) { 
      this.updateString = updateString; 
     } 
     public void run() { 
      // txtview.setText(updateString); 
      createNotifyLatout(updateString); 
     } 
     private void createNotifyLatout(String result) { 
      //final ArrayList<TextView> addrTextList = new ArrayList<TextView>(); 

      addrLinearLayout = (LinearLayout) findViewById(R.id.dashboardLayout); 
      TextView addrTextView = new TextView(context); 
      LinearLayout addrLayout = new LinearLayout(context); 
      addrLayout.setPadding(2, 0, 2, 0); 
      addrLayout.setOrientation(LinearLayout.HORIZONTAL); 
      ImageView editImgBtn = new ImageView(context); 
      editImgBtn.setId(700 + CreateOrderService.notifyCount.get()); 
      editImgBtn.setImageDrawable(getResources().getDrawable(R.drawable.takeorder)); 
      editImgBtn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

       } 
      }); 
      editImgBtn.setLayoutParams((new LinearLayout.LayoutParams(100, 
        40, 1.0f))); 
      ImageView deltImgBtn = new ImageView(context); 
      deltImgBtn.setId(8000 + CreateOrderService.notifyCount.get()); 
      deltImgBtn.setLayoutParams((new LinearLayout.LayoutParams(100, 
        50, 1.0f))); 
      deltImgBtn.setPadding(0, 0, 0, 10); 
      deltImgBtn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

       } 
      }); 
      deltImgBtn.setImageDrawable(getResources().getDrawable(R.drawable.cancelorder)); 
      addrTextView.setPadding(2, 5, 0, 0); 

      addrTextView.setText(Html.fromHtml("<br><br>" + result + "<br><br>")); 
      addrTextView.setLayoutParams((new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT))); 
      addrTextView.setMaxEms(10); 
      // registerForContextMenu(addrTextView); 
      addrLayout.setBackgroundColor(getResources().getColor(R.color.banarColor)); 
      addrLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE); 
      addrLayout.setDividerDrawable(getResources().getDrawable(R.drawable.divider)); 
      addrTextView.setTextColor(getResources().getColor(R.color.backgroundColor)); 
      addrTextView.setBackgroundDrawable(getResources().getDrawable(R.drawable.backg_txt)); 

      addrLayout.addView(editImgBtn); 
      addrLayout.addView(deltImgBtn); 
      // addrTextList.add(addrTextView); 
      addrLinearLayout.addView(addrTextView); 
      addrLinearLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.border_img)); 
      addrLinearLayout.addView(addrLayout); 
      Toast.makeText(context,"Notify Open :: "+CreateOrderService.notifyCount.get()+"[email protected]@=="+result,Toast.LENGTH_LONG).show(); 
     } 
    } 
@Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     CustomActionBar.controlNotifyImage(this,mActionBar); 
     //Toast.makeText(context,"#####",Toast.LENGTH_LONG).show(); 
     SellerNotifyDB sellerNotifydb = SellerNotifyDB.getInstance(context); 
     sellerNotifydb.deleteNotification(); 
    } 
    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     CustomActionBar.controlNotifyImage(this,mActionBar); 
     SellerNotifyDB sellerNotifydb = SellerNotifyDB.getInstance(context); 
     ArrayList notifyInfoList = sellerNotifydb.getNotification(); 
     if(notifyInfoList instanceof ArrayList && notifyInfoList.size() > 0) 
      for (int i=0;i<notifyInfoList.size();i++){ 
       //createNotifyLatout(((NotifyInfo)notifyInfoList.get(i)).getNotifyTxt()); 
       test = ((NotifyInfo)notifyInfoList.get(i)).getNotifyTxt(); 
       runOnUiThread(new UpdateUI(test)); 
      } 

    }  
+0

Pouvez-vous s'il vous plaît poster un code –

+0

n'utilisez pas textView.setText() il détruira le texte précédent et définira le nouveau texte. Utilisez textView.append ("quel que soit"); pour une meilleure compréhension de la question poster du code! –

+0

Vous voulez ajouter 'text' ou voulez-vous ajouter un nouveau' textview'? – Kunu

Répondre

1

Je pense que vous devez arrêter votre service existant.

+0

lequel un l'ancien ou le nouveau ?? –

+1

Vieux un.means qui est déjà en cours d'exécution. Vous devez arrêter ceci d'abord et encore besoin de créer un nouveau –

+0

laissez-moi vérifier merci pour votre conseil –

0

Gardez votre TextView existant dans un LinearLayout puis générer un TextView Et ajouter à LinearLayout. Voir this