让我的班级参加辩论

时间:2012-01-10 21:44:48

标签: java android class

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

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

        }
        }); 

任何人都可以建议如何为我的班级提供参数以及它们的类型应该是什么?

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

您可以通过向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参数的值来决定要检索的数据和文本。如果它是大量的数据或文本,你可能最好使用第二种方法。

答案 2 :(得分:1)

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");
相关问题