Make my class take an argument

2019-08-30 00:21发布

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

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?

3条回答
萌系小妹纸
2楼-- · 2019-08-30 00:30

You can pass parameters with an Intent by adding extra's to it, something like the following:

Intent i = new Intent(this, MyActivity.class);
i.putExtra("paramName", "value");
startActivity(i);

In your activity you can use the getIntent() method to retrieve the Intent and extract your parameter(s) from it:

Intent i = getIntent();
Bundle extras = i.getExtras();
String param = extras.getString("paramName", "default value");

You can place all the different text and data in your Intent, but you can also decide based on the value of an Intent parameter which data and text to retrieve. If it is a lot of data or text you are probably better off using the second approach.

查看更多
一纸荒年 Trace。
3楼-- · 2019-08-30 00:33
If you want to pass object instead of simple data, you must use parcelable objects as it´s explained in: [http://developer.android.com/reference/android/os/Parcelable.html][1]

Reading and writing with parcelable objects is very similar to the way with a simple data

Intent intent = new Intent(this ,newthing.class);
Bundle b = new Bundle();
b.putParcelable("results", listOfResults);
intent.putExtras(b);
startActivityForResult(intent,0);

Intent i = getIntent();
Bundle extras = i.getExtras();
String param = extras.getParcelable("results");
查看更多
放我归山
4楼-- · 2019-08-30 00:40

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);

then you can extract the data from bundle by

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);

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");
   }
}

The same way you can pass other data types as String boolean etc. If this is not sufficient and you need to pass some more complex data, then use Parcelable interface.

查看更多
登录 后发表回答