片段它传递数据但不更新列表适配器

Fragment it passed data but doesn't update the list adapter

我在一个 Activity 上有两个片段。当我单击 fragment A 上的按钮时,一个对象通过托管它们的 Activity 中的接口实现传递给 Fragment B。 它确实到达了 Fragment 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;
  }
 }

将项目添加到您的列表时,您的适配器不会收到通知。 尝试在你的片段中这样做

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();
}