开发者

wanted to access a variable from main class to other class...

my program is as follow:

public class HelloGridViewActivity extends Activity {

    public int pos;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                pos = position;
                Toast.makeText(HelloGridViewActivity.this, "" + pos, Toast.LENGTH_SHORT).show();
                Intent secondActivity = new Intent(getBaseContext(), seconactivity.class);
                startActivity(secondActivity);
            }
        });
    }
}

this is my main class file and my other java file is as follow

public clas开发者_JS百科s seconactivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        HelloGridViewActivity position1 = new HelloGridViewActivity();
        int p = position1.pos;
        if (p == 0) {
            setContentView(R.layout.ashtavinayak);
        } else {
            setContentView(R.layout.ayadhya);
        }
    }
}

nw i wanted to access pos variable defined in the main class to the seconactivity class..can any1 tell me how can i do this???


The easiest way is to pass it in the intent, either as data or as an extra (look at Intent.setData() and Intent.putExtra.

In the second activity, you can call getIntent(), and then read its data and extras.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜