[英]cant update listView (android)
i was trying to code something(the world will never know what :D) and when im trying to update listview i just get an error....我正在尝试编写一些代码(世界永远不会知道什么:D),当我尝试更新列表视图时,我只是收到一个错误....
code in theory: "when i click on item in NavDrawer the listView should get populated with specific data"理论上的代码:“当我点击 NavDrawer 中的项目时,listView 应该填充特定数据”
code in reality:实际代码:
public class MainActivity extends AppCompatActivity {
ArrayAdapter<String> adapter;
String[] values;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.listview);
values = new String[]{"Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7"};
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
listview.setAdapter(adapter);
}
public void update(){
adapter.clear();
adapter.notifyDataSetChanged();
}
that method is called from this method that exsists in NavDrawerFragment.java该方法是从存在于 NavDrawerFragment.java 中的这个方法调用的
@Override
public void itemClicked(View view, int position) {
Toast.makeText(getActivity(), "shit is going down", Toast.LENGTH_LONG).show();
new MainActivity().update();
}
i guess that it is happening because im creating a new object, but i just (in the middle of typing a post) passed adapter as a method parameter(u can inmagine it as a setter)and in itemClicked method i typed adapter.clear() and same shit happend我猜这是因为我创建了一个新对象,但我只是(在输入帖子的过程中)将适配器作为方法参数传递(你可以将它想象成一个 setter)并且在 itemClicked 方法中我输入了 adapter.clear( )和同样的事情发生了
okay, i changed values varible using setter好的,我使用 setter 更改了值变量
public void setValues(String[] values) {
this.values = values;
adapter.notifyDataSetChanged();
}
i called it like that from recyclerview(that exists in the drawer) adapter String values[] = {"lel", "uja"}; mainActivity.setValues(values);
我从 recyclerview(存在于抽屉中)适配器
String values[] = {"lel", "uja"}; mainActivity.setValues(values);
String values[] = {"lel", "uja"}; mainActivity.setValues(values);
so same crash, maybile som1 would like to get on skype call?同样的崩溃,maybile som1 想拨打 Skype 电话吗? i promise, i wont be annoying
我保证,我不会烦人
Apparently I was creating another object after all:显然我毕竟是在创建另一个对象:
MainActivity mainActivity = new MainActivity();
I was wondering why I can access that method like this:我想知道为什么我可以像这样访问该方法:
mainActivity.setValues(values);
So probably what I'm asking is: How to access method without creating activity?所以可能我要问的是:如何在不创建活动的情况下访问方法?
For an ArrayAdapter, notifyDataSetChanged only works if you use the add()
, insert()
, remove()
, and clear()
on the Adapter.对于 ArrayAdapter,notifyDataSetChanged 仅在您在适配器上使用
add()
、 insert()
、 remove()
和clear()
时才有效。
When an ArrayAdapter
is constructed, it holds the reference for the List
that was passed in. If you were to pass in a List that was a member of an Activity
, and change that Activity member later, the ArrayAdapter
is still holding a reference to the original List.当一个
ArrayAdapter
的构造,它保存了参考List
中传递。如果你是这是一个成员的名单传递Activity
,并更改活动成员后, ArrayAdapter
仍持有该参考原始列表。 The Adapter does not know you changed the List in the Activity.适配器不知道您更改了活动中的列表。
@Override
public void itemClicked(View view, int position) {
//filter your list i.e `values` based on on item click,clearing it and adding new values
adapter.notifyDataSetChanged();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.