0

I have a main application with a button defined in layout.xml.

When I click on the button, I want to call a method located in another class, and when I try to create a TextView in that class, I must provide an argument to the new TextView(???) command, and I don't know what to do.

I reckon that this is a 2 seconds question for you folks, and for me, newbiiie as it is, it is a tough one.

Just in case, here are the relevant sections of code:

The section of the main class that is applicable:

public class MainActivity extends Activity {

    public DateAndTime cur_datetime = new DateAndTime();
    public LongLat cur_longlat = new LongLat();
    public int current_location_number = 0;
    public ArrayList<LocationInfo> locations = null;
     Button doSunButton;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenersForButtons();
            dosomething();
    }

public void addListenersForButtons()
{
    doSunButton = (Button) findViewById(R.id.dosun_button_id);
    doSunButton.setOnClickListener( new OnClickListener()
 {

@Override
  public void onClick(View arg0) 
  {
DoSun myDoSun = new DoSun();
Log.v("button", "Am I really calling from the button function...");
myDoSun.doSun2(locations, current_location_number);
} // end of dosun on click on dosun_id button
                        ); // end of define listener

  } // end of addListenersForButtons(0) method
}

The class whose method is called:

package com.example.sunandmoon;

import java.util.ArrayList;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class DoSun extends Activity{

    public void doSun2(ArrayList<LocationInfo> locations, int current_location_number)
    {
        //Log.v("doSun", "Am I really there!");
        TextView textViewsunrise = new TextView(??????);
        textViewsunrise = (TextView) findViewById(R.id.sunrise_id);
        ((TextView)textViewsunrise).setText("From DoSun2! " + locations.get(current_location_number).getnameGiven());
    } // end of doSun(0) method

}

And by the way, I also wonder how I could avoid passing the two parameters current_location_numberv and ArrayList locations to the doSun2 method, since they "should be" globals (you can see that I come from C...).

Thank you for your help.

And to you flamers of all kinds, yes, I have tried to find an answer to this...

4

1 回答 1

2

您的代码中有多个问题。

  1. 不要将扩展自的类Activity用于活动以外的任何东西!AnActivity表示用户与之交互的单个以任务为中心的对象。松散地,将应用程序中的每个屏幕视为一个Activity.

  2. “应该是全球性的”。不,他们不应该。在 Android 中偶尔会出现全局变量有意义的情况。这不是其中的一个。您不应该避免将参数传递给,doSun2()因为它的行为根据这些参数。使用全局变量完全是反模式。

  3. 您要正确创建的TextView属于MainActivity. 它应该负责创建和管理它。为此,让 DoSun 中的方法接受参数(位置、currentLocationNumber)并让它返回某种结构,其中包含您TextViewMainActivity. 您可以创建一个辅助方法,MainActivity其中将返回的结构doSun2()作为参数并返回一个新的TextView准备添加到Activity Layout.

  4. 通常,只有活动应该创建和管理任何 UI 元素。

  5. 如果DoSun真的应该是一个 Activity,那么不要尝试通过它的构造函数创建它的实例。相反,创建一个Intent并使用startActivity来创建它。

综上所述,你应该养成描述你想要达到的目标的习惯,因为你的方法(我已经回应过)可能不是正确的方法。

祝你好运!

于 2013-04-17T15:15:21.323 回答