我正在尝试将 View 从一个 Activity 传递到另一个 Activity 。
在我的第一个 Activity onButtonClick 中,我使用 Intent 导航到另一个 Activity 。
我在第一个 Activity 中写了这一行 setContentView(R.layout.main); 并且还声明了一个 graphView。
现在的问题是,我想在第二个 Activity 中填充 graphView,但它是引用,即 mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);出现在第一个 Activity 中。
那么如何在第二个 Activity 中访问 mySimpleXYPlot?
如果我用
setContentView(R.layout.main);
mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
在第二个 Activity 中,整个布局重新启动,这是我不想发生的:(
我们将不胜感激!
最佳答案
如果你想使用声明为 static 和 public 的同一个实例,那么你也可以在另一个类或 Activity 中使用
编辑...
在第一个 Activity 中这样做
public class MyActivity1 extends Activity{
public static XYPlot mySimpleXYPlot;
public onCreated(Bundle b){
setContentView(R.layout.main);
mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
// start you 2nd activity from button
}
}
在Activity2中这样使用
public class MyActivity2 extends Activity{
private XYPlot mySimpleXYPlot;
public onCreated(Bundle b){
setContentView(R.layout.main);
mySimpleXYPlot = MyActivity2.mySimpleXYPlot;
// use mySimpleXYPlot as per your requirement
}
}
我不确定这个工作是否完美,但尝试这种方式并说出这会发生什么
编辑2
不要在 oncreate 时将组件添加到 xml 布局文件中
我的布局文件是这样的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/main_linear"
>
<Button android:id="@+id/btn1" android:text="Click" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
我的第一个 Activity
public static EditText edittext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
//edittext = (EditText) findViewById(R.id.edittext);
final LinearLayout ll = (LinearLayout) findViewById(R.id.main_linear);
edittext = new EditText(getApplicationContext());
edittext.setId(1);
edittext.setText("text change");
((Button)findViewById(R.id.btn1)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
edittext.setText(edittext.getText().toString());
ll.removeView(edittext);
startActivity(new Intent(TestLinear.this,TestClass.class));
}
});
ll.addView(edittext);
}
您需要先从您添加为子组件的布局中删除该组件
private static EditText edittext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
LinearLayout ll = (LinearLayout) findViewById(R.id.main_linear);
getEdit();
((Button)findViewById(R.id.btn1)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("test", edittext.getText().toString());
}
});
ll.addView(edittext);
}
static void getEdit(){
edittext = TestLinear.edittext;
}
我知道这不是完美的方式。另一种方法是您可以将它的值存储在自定义类中,如 setter/getter 并在 Activity 中使用
关于java - 将 View 从一项 Activity 传递到另一项 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8573820/
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht