Android 如何在自定义适配器类之外调用NotifyDataSetChanged()?
我在使用自定义适配器类填充listView的活动中有一个ArrayList。自定义适配器类代码位于活动中,数据库适配器代码位于自定义适配器类之外的同一活动中。在更新数据库以刷新listView之后,我似乎不知道如何从活动中的自定义适配器类外部调用NotifyDataSetChanged()。我知道我需要在活动中以某种方式创建引用,以便在自定义listView适配器类上调用NotifyDataSetChanged(),但几天后,我仍然无法做到这一点。相信我,我已经阅读并尝试了这里的每一个解决方案,但我仍然无法解决这个问题。任何帮助都将不胜感激Android 如何在自定义适配器类之外调用NotifyDataSetChanged()?,android,listview,Android,Listview,我在使用自定义适配器类填充listView的活动中有一个ArrayList。自定义适配器类代码位于活动中,数据库适配器代码位于自定义适配器类之外的同一活动中。在更新数据库以刷新listView之后,我似乎不知道如何从活动中的自定义适配器类外部调用NotifyDataSetChanged()。我知道我需要在活动中以某种方式创建引用,以便在自定义listView适配器类上调用NotifyDataSetChanged(),但几天后,我仍然无法做到这一点。相信我,我已经阅读并尝试了这里的每一个解决方案,
public class getServers extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server_list);
//ServerList ArrayList
ArrayList<ServerList> Items = new ArrayList<ServerList>();
//Create the listview
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new ServerItemAdapter(this, R.layout.server_list_item, Items));
listView.setTextFilterEnabled(true);
//Updates the server status to 0 (Forget)
public void updateServerStatus(Integer pos) {
String ID = gstrServerMacs[pos];
//Open the connection to the database adapter
DBAdapter db = new DBAdapter(this);
db.open();
//Do the update
Cursor c = db.updateStatus(ID);
c.moveToFirst();
db.close();
c.close();
//Fill the arrays with the updated data from th db
getServerList();
**I NEED TO CALL NotifyDataSetChanged() HERE!!!**
}
//LISTVIEW CLASS START/////////////////////////////////////////////////////////////////////
public class ServerItemAdapter extends ArrayAdapter<ServerList> {
private ArrayList<ServerList> Items;
//private String[] values;
public ServerItemAdapter(Context context, int textViewResourceId, ArrayList<ServerList> Items) {
super(context, textViewResourceId, Items);
this.Items = Items;
}
@SuppressLint("UseValueOf")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.server_list_item, parent, false);
}
//Array adapter
public class ServerList {
public String ServerName;
public String ServerMAC;
public String ServerIP;
public ServerList(String ServerName, String ServerMAC, String ServerIP) {
this.ServerName = ServerName;
this.ServerMAC = ServerMAC;
this.ServerIP = ServerIP;
}
}
公共类getServers扩展活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.server_列表);
//服务器列表数组列表
ArrayList Items=新建ArrayList();
//创建listview
ListView ListView=(ListView)findViewById(R.id.ListView);
setAdapter(新的ServerItemAdapter(this,R.layout.server_list_Items,Items));
setTextFilterEnabled(true);
//将服务器状态更新为0(忘记)
公共无效更新服务器状态(整数位置){
字符串ID=gstrServerMacs[pos];
//打开与数据库适配器的连接
DBAdapter db=新的DBAdapter(此);
db.open();
//进行更新
游标c=db.updateStatus(ID);
c、 moveToFirst();
db.close();
c、 close();
//用来自th db的更新数据填充阵列
getServerList();
**我需要在这里调用NotifyDataSetChanged()**
}
//LISTVIEW类开始/////////////////////////////////////////////////////////////////////
公共类ServerItemAdapter扩展了ArrayAdapter{
私有ArrayList项;
//私有字符串[]值;
公共ServerItemAdapter(上下文上下文、int-textViewResourceId、ArrayList项){
super(上下文、textViewResourceId、项);
这个。项目=项目;
}
@SuppressLint(“UseValueOf”)
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.layout.server\u list\u项,父项,false);
}
//阵列适配器
公共类服务器列表{
公共字符串ServerName;
公共字符串服务器MAC;
公共字符串服务器IP;
公共服务器列表(字符串ServerName、字符串ServerMAC、字符串ServerIP){
this.ServerName=ServerName;
this.ServerMAC=ServerMAC;
this.ServerIP=ServerIP;
}
}
如果您有权访问创建适配器的活动,则可以向该活动添加一个调用notify adapter的方法,并将活动传递给添加到数据库的类。其他方法包括在维护自定义适配器的活动中使用广播和设置本地广播侦听器。单例是also一个选项,但它们是由垃圾收集器收集的,所以要小心,因为你可能会浪费内存,而在android开发中,这是一个宝贵的资源
而不是使用:
listView.setAdapter(new ServerItemAdapter(this, R.layout.server_list_item, Items));
将serverItemAdapter的副本作为活动中的feld,并在onCreate中进行如下设置:
this.serverItemAdapter = new ServerItemAdapter(this, R.layout.server_list_item, Items));
listView.setAdapter(serverItemAdapter);
更改代码如下
//Create the listview
ListView listView = (ListView) findViewById(R.id.listView);
ServerItemAdapter adapter=new ServerItemAdapter(this, R.layout.server_list_item, Items)
listView.setAdapter(adapter);
getServerList();
adapter.notifyDataSetChanged()
然后像下面那样打电话
//Create the listview
ListView listView = (ListView) findViewById(R.id.listView);
ServerItemAdapter adapter=new ServerItemAdapter(this, R.layout.server_list_item, Items)
listView.setAdapter(adapter);
getServerList();
adapter.notifyDataSetChanged()
改变
listView.setAdapter(new ServerItemAdapter(this, R.layout.server_list_item, Items));
到
然后在你的onCreate中
ad = new ServerItemAdapter(this, R.layout.server_list_item, Items))
listView.setAdapter(ad);
你可以打电话
ad.notifyDataSetChanged();
或
在片段中使用它,然后使用适配器的对象调用notifydatasetchanged,该对象用于创建适配器即使Michael Shrestha的解决方案工作得很好,我只想补充一点,您不需要保留对ServerItemAdapter对象的引用 如果您是从范围外访问它,则可以从ListView对象获取并强制转换它,以便调用notifyDataSetChanged()。如下所示:
ServerItemAdapter listAdapter = (ServerItemAdapter) listView.getAdapter();
listAdapter.notifyDataSetChanged();
使用此方法,可以节省一些内存
同样,将其留在这里供将来参考。您是否尝试过从自定义适配器实例调用它?我需要从更新位于自定义适配器类之外的数据库的方法调用它。您可以发布代码吗?是的,我将尝试发布重要部分。在Activit中全局声明ServerItemAdaptery和
public void updateServerStatus(Integer pos,ServerItemAdapter adapter){String ID=gstrServerMacs[pos];//打开到数据库适配器DBAdapter db=new DBAdapter(this);db.Open();//执行更新游标c=db.updateStatus(ID);c.moveToFirst();db.close();c.close();//使用来自th db getServerList()的更新数据填充数组;adapter.notifyDataSetChanged();}
你能看看我刚刚发布的代码的一小部分吗?也许可以留下一个我将如何做到这一点的例子吗?谢谢。谢谢你花时间发布一个例子。我尝试了一下,我得到了这个错误:08-08 01:11:53.445:E/AndroidRuntime(15649):致命异常:main 08-08 01:11:53.445:E/AndroidRuntime(15649):java.lang.ArrayIndexOutOfBoundsException:length=0;index=0 08-08 01:11:53.445:E/AndroidRuntime(15649):检查是否从数据库返回数据…尝试调试getServerList()方法..哦,对了…我刚从数据库中删除了数据,然后尝试不使用任何内容更新列表。可能就是这样。我仍然需要调试此错误,但我认为
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mAdapter.notifyDataSetChanged();}
ServerItemAdapter listAdapter = (ServerItemAdapter) listView.getAdapter();
listAdapter.notifyDataSetChanged();