Android 对其传递的数据进行分段,但不';t更新列表适配器
我在一个Android 对其传递的数据进行分段,但不';t更新列表适配器,android,list,android-fragments,adapter,Android,List,Android Fragments,Adapter,我在一个活动中有两个片段。当我单击片段a上的按钮时,一个对象通过托管它们的活动中的接口实现传递到片段B。 它确实到达了片段B,但是列表没有更新,它仍然是空的 我已经尝试过以各种可能的方式放置notifyDataSetChanged() 片段B: public class HistoryFragment extends ListFragment { private static PasswordAdapter adapter ; private static List<Password&
活动中有两个片段。当我单击片段a上的按钮时,一个对象通过托管它们的活动中的接口实现传递到片段B。
它确实到达了片段B,但是列表没有更新,它仍然是空的
我已经尝试过以各种可能的方式放置notifyDataSetChanged()
片段B:
public class HistoryFragment extends ListFragment
{
private static PasswordAdapter adapter ;
private static List<Password> historyList = new ArrayList<Password>();
//This is the object I get from Fragment A via the activity interface implementation
public static void addToData( Password addToList )
{
historyList.add( addToList );
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
historyList = Password.getTriedPasswords();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
adapter = new PasswordAdapter( historyList );
setListAdapter(adapter);
return view;
}
private class PasswordAdapter extends BaseAdapter
{
private ArrayList<Password> data;
public PasswordAdapter( List<Password> historyList )
{
data = new ArrayList<Password>();
notifyDataSetChanged();
data.addAll( historyList );
}
@Override
public int getCount()
{
return data.size();
}
@Override
public Password getItem( int position )
{
return data.get( position );
}
@Override
public long getItemId(int position) {
// TODO implement you own logic with ID
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View result;
if (convertView == null)
{
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_list_item, parent, false);
}
else
{
result = convertView;
}
Password item = getItem( position );
( (TextView) result.findViewById(R.id.historyPasswordTextView) ).setText(item.getPasswordString());
( (TextView) result.findViewById(R.id.historyDateTextView) ).setText(item.getPasswordString());
return result;
}
}
公共类HistoryFragment扩展了ListFragment
{
专用静态密码适配器;
private static List historyList=new ArrayList();
//这是我通过活动接口实现从片段A获得的对象
公共静态无效addToData(密码addToList)
{
添加(addToList);
}
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
historyList=密码。getTriedPasswords();
}
@凌驾
创建视图上的公共视图(布局充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.fragment\u历史,容器,false);
适配器=新密码适配器(historyList);
setListAdapter(适配器);
返回视图;
}
私有类密码适配器扩展BaseAdapter
{
私有数组列表数据;
公共密码适配器(列表历史列表)
{
数据=新的ArrayList();
notifyDataSetChanged();
data.addAll(历史列表);
}
@凌驾
public int getCount()
{
返回data.size();
}
@凌驾
公共密码getItem(int位置)
{
返回数据。获取(位置);
}
@凌驾
公共长getItemId(int位置){
//TODO使用ID实现您自己的逻辑
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
最终查看结果;
if(convertView==null)
{
结果=LayoutInflater.from(parent.getContext()).flate(R.layout.history\u list\u项,parent,false);
}
其他的
{
结果=转换视图;
}
密码项=getItem(位置);
((TextView)result.findViewById(R.id.historyPasswordTextView)).setText(item.getPasswordString());
((TextView)result.findViewById(R.id.historyDateTextView)).setText(item.getPasswordString());
返回结果;
}
}
向列表中添加项目时,不会通知适配器。
试着在你的片段中这样做
public void addToData( Password addToList )
{
historyList.add(addToList);
adapter.updateList(historyList);
}
在适配器中创建一个新方法
public void updateList(ArrayList<Password> newData){
data.clear();
for(int i = 0; i < newData.size(); i++){
data.add(newData.get(i));
}
notifyDataSetChanged();
}
public void updateList(ArrayList newData){
data.clear();
对于(int i=0;i
为什么要使用静态列表?某些限制。例如,公共静态void addToData(…)
如果列表不是静态的,则会导致错误。我认为您的menber函数和变量不应该是静态的…好吧,该函数也不应该是静态的…在封闭的活动中,您应该保留对该片段实例的引用,或者使用FragmentManager再次找到它。谢谢。好的,我已经更改了这一点。不遗憾尽管它仍然不能解决问题..我更新了我的答案,你能检查一下这对你是否有效吗?非常感谢!!我已经连续两天这样做了!效果很好:)