简体   繁体   English

有没有办法从内部方法更改 Class 变量

[英]Is there a way to Change Class variable from inner Method

I have a ListView below with an ArrayAdapter to make a simple List view with corresponding Html elements added to assets folder.我在下面有一个带有 ArrayAdapter 的 ListView 来制作一个简单的列表视图,并将相应的 Html 元素添加到assets文件夹中。

import android.app.ListActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;

import android.content.Intent;


public class MainActivity extends ListActivity

{


String indx;

    String[] values = { "index", "Prooo", "My Android Examples","My Android Tutorials","My Android Pages", "My Android Favorites"};



    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);



        // parameters are (Context, layout for the row, and the array of data)

        setListAdapter(new ArrayAdapter<String>(this,

                                                android.R.layout.simple_list_item_1, values));

    }

    
    @Override

    public void onListItemClick(ListView l, View v, int position, long id)

    {
        indx = (String) getListAdapter().getItem(position);
        Toast.makeText(this,indx,Toast.LENGTH_SHORT).show();
        Intent i=new Intent(MainActivity.this,WebViewActivity.class);
        startActivity(i);

    }
    
    
    



}

Everything else works fine but the aim is to change the String indx value with the item Position returned by onListItemClick as each Html document's name is the same as that of each String values item.其他一切正常,但目的是使用onListItemClick返回的项目Position更改字符串索引值,因为每个 Html 文档的名称与每个字符串项目的名称相同。

I tired to access indx from a class WebViewActivity to display the Html but the String indx value is still Null .我厌倦了从 class WebViewActivity访问索引来显示Html但字符串索引值仍然是Null

I would've specify something like Public indx if it was Python please is there any way to change Indx like that in Java and I can't use return because void cannot return如果它是 Python,我会指定类似Public indx 的东西,请问有什么方法可以像 Java 那样更改Indx并且我不能使用return因为void不能返回

For Accessing indx in WebViewActivity.用于访问 WebViewActivity 中的索引。 You need to pass the value through intent like below,您需要通过如下意图传递值,

 Intent i=new Intent(MainActivity.this,WebViewActivity.class);
i.putExtra("index",indx);      
startActivity(i);

For getting your string in WebViewActivity use below code in WebViewActivity, add below code in Oncreate of WebViewActivity要在 WebViewActivity 中获取您的字符串,请在 WebViewActivity 中使用以下代码,在 WebViewActivity 的 Oncreate 中添加以下代码

String index=getIntent().getStringExtra("index");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM