【发布时间】:2018-02-20 16:55:22
【问题描述】:
这是我的场景:在我的应用程序中,我有一个主要活动、两个片段和一个在后台运行的服务。我使用这段代码将片段附加到我的主要活动中:
Fragment f1;
f1=new loginfragment();
FragmentTransaction ft=getFragmentManager().beginTransaction();
ft.add(R.id.frame,f1);
ft.commit();
哪个工作正常。其中一个片段包含一个连接到适配器的列表视图。(我已验证适配器已正确连接到列表视图,并且可以将条目添加到列表视图而没有任何问题。
为了能够扩展列表视图适配器(扩展 BaseAdapter)内的布局,我创建了一个构造函数来向它提供上下文。
后台服务定期检查页面并使用片段实现的接口将结果发送到其中一个片段(发送广播对我不起作用)。
接口定义如下:
interface resultInterface{
void receive(String s,Context con);
}
片段实现接口如下:
@Override
public void receive(String s,Context con){
String elements[]=s.split("<br>");
if(elements.length>1) {
if(elements[0].equals("REQUEST")) {
init_notification("REQUEST",detect(elements[1],elements[2]));// a function that shows a notification
item i = new item(elements[1], elements[2]);
orders.add(i);
adapter = new listview_adapter(orders,c);
list.setAdapter(adapter);}
}
}
orders 是一个 Arraylist,c 是传递给 listview 适配器的上下文。服务调用这个接口方法是这样的:
resultInterface resultinterface=new servefragment();
resulstinterface.receive(s,this);
在接收数据(导致调用接口方法)时,片段方法应该拆分数据并将条目添加到列表视图。(我已验证数据已正确传递给接口方法。
但是,显示为列表视图条目的视图不正确(看起来以某种方式损坏)。以下是它的外观(使用虚拟数据):Like this
但这是它们实际添加的方式(从服务器接收到的 1 个项目是正确的,但布局不正确):Like this
如果您需要更多信息,请发表评论,我会添加它们
【问题讨论】:
标签: java android listview android-fragments