简体   繁体   English

我如何在整个活动中传递实例变量?

[英]how can i pass my instance variable througout my entire activity?

I am trying to make a custom list view adapter. 我正在尝试制作自定义列表视图适配器。 In my public void function I am trying to set the adapter.. (after make the list from the web 在我的public void函数中,我尝试设置适配器..(从网络上列出列表之后

This is in my onCreate(): 这是在我的onCreate()中:

rowItems = new ArrayList<RowItem>();
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,R.layout.activity_main, rowItems);
listView.setAdapter(adapter);

My question is: how can I make my custom listview adapter adapter variable available throughout my activity???? 我的问题是:如何在整个活动中使自定义listview适配器适配器变量可用?

Here is a snippet from when i set my names city and icon and i try to set my adapter 这是我设置名称城市和图标并尝试设置适配器时的摘录

item.setTitle(p.getString("name"));
item.setDesc(p.getString("city"));
item.setImageId(p.getString("icon"));
rowItems.add(item);

listView.setAdapter(adapter);

but eclipse can't find it. 但是蚀找不到它。 I even tried to recall the instance in the public void function with no luck.... 我什至没有运气就试图在公共无效函数中调用该实例。

Any suggestions? 有什么建议么?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new ReadWeatherJSONFeedTask().execute();

    rowItems = new ArrayList<RowItem>();
    listView = (ListView) findViewById(R.id.list);
    CustomListViewAdapter adapter =
        new CustomListViewAdapter(this,R.layout.activity_main, rowItems);
    listView.setAdapter(adapter);
}

Move the declaration into a class field, like this: 将声明移到类字段中,如下所示:

public class MyActivity extends Activity{

    CustomListViewAdapter adapter;

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new ReadWeatherJSONFeedTask().execute();

        rowItems = new ArrayList<RowItem>();

        listView = (ListView) findViewById(R.id.list);

        adapter = new CustomListViewAdapter(this,R.layout.activity_main, rowItems);

        listView.setAdapter(adapter);

    }


}

Class fields are accessible in all methods of a class. 类字段可在类的所有方法中访问。 It's all about "scope". 都是关于“范围”的。 Declaring the adapter inside onCreate limits it's scope (it's visibility) to that method and classes declared within it. onCreate内声明适配器将其范围(可见性)限制为该方法和在其中声明的类。

BTW, you don't say why you are trying to "reset" your adapter but usually, you should only set it once. 顺便说一句,您没有说为什么要“重置”适配器,但是通常,您应该只设置一次。

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

相关问题 当我的方法不在活动中时,如何将其传递给活动? - How can I pass my method an activity when it is not in an activity? 如何将 integer 变量传递给我的活动的 xml 文件? - How can I pass an integer variable to my activity's xml file? 如何将数据传递给我的应用程序中的活动? - How can I pass data to the activity in my app? 如何“更新”我的活动? - How can I “update” my activity? 如何从AlertDialog的onClickListener中访问Activity的实例变量? - How can I access my Activity's instance variables from within an AlertDialog's onClickListener? 如何将数据更新为活动? - How can I update my data into my activity? 如何将Paint或Color传递给使用活动中的onDraw()方法的新对象? - How can I pass a Paint or Color to my new object that is using the onDraw() method from an activity? 如何在我的全局变量中存储一个值并在活动中的任何方法中使用它 - How can I store a value in my global variable and use it inside any method anywhere in activity 如何从listview获取变量并传递给另一个活动? - How can i get variable from listview and pass to another activity? 我如何将整个ListView项目从一个Activity传递到android中的另一个项目 - How can I pass the entire ListView item from one Activity to another in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM