让我带类的参数 [英] Make my class take an argument

查看:167
本文介绍了让我带类的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有5按钮的主屏幕。每次preSS一个按钮,我想去一个新的屏幕。到目前为止,我还有其他5类(每每个屏幕)和5 XML的。但是,荫肯定会有怎么一回事,因为这个屏幕有更好的方式和XML的是一样的,我想要做的是改变一些文本和一些数据我从数据库中读取。我相信,我可以ONY另一个类只有一个XML,然后传递,我想作为参数的值。 (试想一下,在它的最终状态我的应用程序必须有15个按键,所以我认为这是空间和不必要太浪费mych有15 .java文件和外观相同和图像的只有一些价值观和textviews变化15 XML文件) 。我的code的主要活动是:

Well I have a main Screen with 5 buttons. Each time a press a button I want to go a new screen. So far I have other 5 classes (each for each screen) and 5 xmls. But Iam sure that there will be a better way beacuse this screen and the xmls are the same, and what I want to do is change some texts and some data I fetch from a database. I am sure that I can ony another class and only one xml and then pass the values that I want as arguments. (Imagine that in its final state my app must have 15 buttons, so I think it is too mych waste of space and unnecessary to have 15 .java files and 15 xml files that look the same and only some values of images and textviews change). My code for main activity is:

public class MyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    main2Theme();}

private void main2Theme() {
    setContentView(R.layout.main_new);

    Button Button100 = (Button)findViewById(R.id.Button100);
Button113.setOnClickListener(new OnClickListener()
    {   
        public void onClick(View v)
        {
            Intent i = new Intent(this, OtherScreenName.class);
           startActivity(i);
        }
    }); //and so on for 15 buttons

我的OtherScreenName.class是:

My OtherScreenName.class is:

public class OtherScreenName extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Theme();
}

 @SuppressWarnings("null")
    private void Theme() {
        Log.d("SnowReportApp","Do first thing");
        setContentView(R.layout.otherscreenname); //THIS CHANGES IN EVERY CLASS DEPENDING ON THE ID OF THE BUTTON
        String result = "";
        InputStream is = null;
        StringBuilder sb=null;

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();//() before
        nameValuePairs.add(new BasicNameValuePair("ski_id","OtherScreenName"));
        TextView text1 = (TextView) findViewById(R.id.otherscreenname_1);
//THESE 2 CHANGE DEPERNDING ON THE BUTTON PRESSED
//perform action.....

//AND ALSO HERE I NEED THE ID OF THE BUTTON

 b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(OtherScreenName.this,MyActivity.class);
                startActivity(i);

        }
        }); 

任何人都可以提出如何给参数上我的课,什么应该是他们的类型?

Can anyone suggest how to give arguments to my class and what it should be the type of them?

推荐答案

如果我正确地理解你的问题,你想参数传递给活动。通常情况下,将通过类的构造函数来完成,但是,活动不能有用户定义的构造函数,只有默认的;他们只能通过间接创造的意图被实例化。如果你需要传递活动之间的数据,通过把群众演员包,例如做到这一点:

If I understand your question correctly, you want to pass arguments to the activity. Normally it would be done through the class constructor, however, activities can't have user defined constructors, only the default one; they can be instantiated only indirectly via intent creation. If you need to pass data between activities, do it by putting extras to bundles, for example:

bundle.putInt("intName",intValue);

那么你可以通过

int intValue = bundle.getInt("intName");

在开始活动之前把演员:

Put extras before starting the activity:

Intent i = new Intent(this, YourActivity.class);
Bundle b = new Bundle();

b.putInt("intName",intValue);
i.putExtras(b);
startActivity(i);

然后读取额外的onCreate方法:

and then read the extras in the onCreate method:

public void onCreate(Bundle savedInstanceState)
{
   super.onCreate(savedInstanceState);

   Bundle b = getIntent().getExtras();
   int intValue;

   if (b != null)
   {
      intValue= b.getInt("intName");
   }
}

同样的方式,您可以通过其他数据类型为String布尔等。如果这还不够,你需要通过一些更复杂的数据,然后使用的 Parcelable接口。

这篇关于让我带类的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆