无法动态地附加TextView与现有的TextView

时间:2015-04-02 07:14:47

标签: android web-services sqlite

朋友我正在为Android构建一个应用程序并且在显示动态添加TextViewer时面临一些问题我的方案是这样的。 我正在使用ResulReceiver来接收notification并在TextView中动态添加此结果以显示。当我的活动处于活动状态I.e The application is keep open时,它才能正常运行。 当我的活动不在Active模式时,我将其存储到DB (SQLLITE),但当我再次回到我的活动时,我可以从数据库中获取数据并执行相同的操作之前,直到现在都没有问题。 但它开始,因为我当时收到新的通知无法动态地附加现有的新TextView,但是从调试目的来看,我使用Toast它正常显示。 你能帮帮我吗? 提前致谢。 这是我的代码

 @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()+"=@@=="+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));
            }

    }    

2 个答案:

答案 0 :(得分:1)

我认为你需要停止现有的运行服务。

答案 1 :(得分:0)

将现有TextView保留在LinearLayout中,然后生成TextView并添加到LinearLayout。见this