【问题标题】:Make my class take an argument让我的班级争论
【发布时间】:2012-01-10 21:44:48
【问题描述】:

我有一个带有 5 个按钮的主屏幕。每次按一个按钮,我都想进入一个新屏幕。到目前为止,我还有其他 5 个类(每个用于每个屏幕)和 5 个 xml。但我相信会有更好的方法,因为这个屏幕和 xml 是相同的,我想要做的是更改一些文本和我从数据库中获取的一些数据。我确信我可以只使用另一个类和一个 xml,然后将我想要的值作为参数传递。 (想象一下,在最终状态下,我的应用程序必须有 15 个按钮,所以我认为太浪费空间了,没有必要拥有 15 个看起来相同的 .java 文件和 15 个 xml 文件,并且只有一些图像和文本视图的值发生变化) .我的主要活动代码是:

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 是:

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

        }
        }); 

谁能建议如何为我的班级提供论据以及它们的类型应该是什么?

【问题讨论】:

    标签: java android class


    【解决方案1】:

    如果我正确理解您的问题,您希望将参数传递给活动。通常它会通过类构造函数来完成,但是,活动不能有用户定义的构造函数,只有默认的;它们只能通过意图创建间接实例化。如果您需要在 Activity 之间传递数据,请通过在 bundles 中添加额外内容来实现,例如:

    bundle.putInt("intName",intValue);
    

    然后你可以从bundle中提取数据

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

    在活动开始前添加额外内容:

    Intent i = new Intent(this, YourActivity.class);
    Bundle b = new Bundle();
    
    b.putInt("intName",intValue);
    i.putExtras(b);
    startActivity(i);
    

    然后读取 onCreate 方法中的附加内容:

    public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
    
       Bundle b = getIntent().getExtras();
       int intValue;
    
       if (b != null)
       {
          intValue= b.getInt("intName");
       }
    }
    

    同样的方式你可以传递其他数据类型如 String boolean 等。如果这还不够,你需要传递一些更复杂的数据,那么使用Parcelable interface

    【讨论】:

    • 是这样的。因此,尽管我想传递类型的数量和参数,但我创建了一个包,然后说 b.putInt,b.putInt 用于第二个等等? (或每个参数的不同 Bundle?)非常感谢!
    • 是的,完全正确。当然,如果你有很多参数要传递,最好将它们包装到实现 Parcelable 接口的类中,然后通过 'b.putExtra(String name, Parcelable value)' 将整个类放到 Extras 中
    【解决方案2】:

    您可以通过添加额外的 Intent 来传递参数,如下所示:

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

    在您的活动中,您可以使用 getIntent() 方法来检索 Intent 并从中提取您的参数:

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

    您可以在 Intent 中放置所有不同的文本和数据,但您也可以根据 Intent 参数的值来决定要检索哪些数据和文本。如果是大量数据或文本,您最好使用第二种方法。

    【讨论】:

      【解决方案3】:
      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");
      

      【讨论】:

        猜你喜欢
        • 2015-03-09
        • 2016-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-20
        • 1970-01-01
        相关资源
        最近更新 更多