我正在动态地在活动布局中添加一个TextView。该活动使用startActivityForResult从另一个活动获取值,然后动态创建textView并向其添加特定值。但问题是,当我再次获得值并返回到负责显示textView的活动时,同一个活动有一个update按钮,它没有更新该textView,而是在前面的textView下面添加了另一个textView,在布局中,它继续添加而不进行更新。我知道我们可以将它的可见性设置为gone,但是它是否也从布局的子元素中删除了textView,或者还有另一种方法来破坏文本视图并在那个地方创建另一个呢?
My 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();
}
}
}发布于 2015-05-12 04:51:04
尝试检查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();
}
}
}发布于 2015-05-12 04:50:44
确实要删除文本视图而不更改其值吗?
更改TextView的文本比从布局中删除TextView并重新附加新的文本要便宜得多
但是,如果您想要删除TextView并附加一个新的,您可以首先使用布局的removeView方法删除旧的,然后使用布局的addView方法插入新的
可以通过调用布局的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);发布于 2015-05-12 06:18:11
您需要更新runOnUiThread中的文本视图。
runOnUiThread:在UI线程上运行指定的操作。如果当前线程是UI线程,则立即执行该操作。如果当前线程不是UI线程,则该操作将被发布到UI线程的事件队列中。
参考:http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
在您的代码中实现:
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的更多实现:code.php?s=runOnUiThread
https://stackoverflow.com/questions/30181977
复制相似问题