用更新的TextView Android替换TextView

用更新的TextView Android替换TextView,android,view,Android,View,我正在活动布局中动态添加TextView。该活动使用startActivityForResult从另一个活动获取值,然后动态创建textView并向其添加特定值。 但问题是,当我再次获取值并返回到负责显示textView的活动时,同一个活动有一个更新按钮。该活动不更新该textView,并在布局中的上一个textView下方添加另一个textView,它继续添加而不更新。 我知道我们可以将其可见性设置为gone,但它是否也会从布局的子元素中删除textView,或者是否有其他方法来销毁该文本视图

我正在活动布局中动态添加TextView。该活动使用startActivityForResult从另一个活动获取值,然后动态创建textView并向其添加特定值。 但问题是,当我再次获取值并返回到负责显示textView的活动时,同一个活动有一个更新按钮。该活动不更新该textView,并在布局中的上一个textView下方添加另一个textView,它继续添加而不更新。 我知道我们可以将其可见性设置为gone,但它是否也会从布局的子元素中删除textView,或者是否有其他方法来销毁该文本视图并在该位置创建另一个文本视图

我的onActivityResult方法:

protected void onActivityResult(int requestCode, int resultCode, Intent intentData) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, intentData);
        if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){
            locationLat = intentData.getDoubleExtra("LocationLat", 0.0);
            locationLng = intentData.getDoubleExtra("LocationLang", 0.0);
            location = locationLat + " "+locationLng;
            if(location != ""){
                Toast.makeText(this, location, Toast.LENGTH_LONG).show();
                lblName = new TextView(this);
                lblName.setText(location);
                lblName.setId(9);
                lblName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                LinearLayout ll = (LinearLayout) findViewById(R.id.llOne);
                ll.addView(lblName, 5);
                Button btnSetLoc = (Button) findViewById(R.id.getLoc);
                btnSetLoc.setText("Change Location");
            }
            else{
                Toast.makeText(this, "location is not set", Toast.LENGTH_LONG).show();
            }
        }
    }

是否确实要删除textview而不更改其值

更改TextView的文本比从布局中删除TextView并重新附加新文本便宜得多

但是,如果要删除TextView并附加新的TextView,可以先使用Layout的removeView方法删除旧的TextView,然后使用Layout的addView方法插入新的TextView

您可以通过调用Layout的indexOfChild方法来获取视图的索引

但是,如果您只需在TextView中更改文本即可实现所尝试的功能,那么只需使用TextView的方法setText()

那么基本上这是什么呢

TextView oldText = (TextView) findViewById(R.id.oldText);
LinearLayout ll = (LinearLayout) findViewById(R.id.llOne);
int index = ll.indexOfChild(oldText);
ll.removeViewAt(index);
TextView newText = new TextView(context);
ll.addView(newText, index);

尝试检查lblName是否为空,如果为空,则创建它,否则更新它

  protected void onActivityResult(int requestCode, int resultCode, Intent intentData) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, intentData);
            if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){
                locationLat = intentData.getDoubleExtra("LocationLat", 0.0);
                locationLng = intentData.getDoubleExtra("LocationLang", 0.0);
                location = locationLat + " "+locationLng;
                if(location != ""){
                    Toast.makeText(this, location, Toast.LENGTH_LONG).show();
                    if(lblName==null) {
                        lblName = new TextView(this);
                        lblName.setId(9);
                        lblName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                        LinearLayout ll = (LinearLayout) findViewById(R.id.llOne);
                        ll.addView(lblName, 5);
                        Button btnSetLoc = (Button) findViewById(R.id.getLoc);
                        btnSetLoc.setText("Change Location");
                    }
                    lblName.setText(location);
                }
                else{
                    Toast.makeText(this, "location is not set", Toast.LENGTH_LONG).show();
                }
            }
        }

您需要在runOnUiThread中更新文本视图

runOnUiThread:在UI线程上运行指定的操作。如果当前线程是UI线程,则立即执行该操作。如果当前线程不是UI线程,则将操作发布到UI线程的事件队列

参考

代码中的实现:

protected void onActivityResult(int requestCode, int resultCode, Intent intentData) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, intentData);
        if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){
            locationLat = intentData.getDoubleExtra("LocationLat", 0.0);
            locationLng = intentData.getDoubleExtra("LocationLang", 0.0);
            location = locationLat + " "+locationLng;
            if(location != ""){
                 runOnUiThread(new  Runnable() { 
                public  void  run() { 
                   Toast.makeText(this, location, Toast.LENGTH_LONG).show();
                lblName = new TextView(this);
                lblName.setText(location);
                lblName.setId(9);
                lblName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                LinearLayout ll = (LinearLayout) findViewById(R.id.llOne);
                ll.addView(lblName, 5);
                Button btnSetLoc = (Button) findViewById(R.id.getLoc);
                btnSetLoc.setText("Change Location");
                } 
            }); 

            }
            else{
                Toast.makeText(this, "location is not set", Toast.LENGTH_LONG).show();
            }
        }
    }

有关runOnUiThread的更多实现:

sorry@Nilesh它认为应该是lblName==null而不是!=还有一部分需要更新:|是的,谢谢@FaisalNaseer,这是打字错误。。我已经更新了我的应答器BTNSETLOC,它也应该在if之外,这取决于您的条件,并根据您的需要进行修改