Reputation: 2480
I have two fragments on one Activity
. When I click a button on fragment A , an object passes to Fragment B through an interface implementation in the Activity
hosting them both.
It does get to the Fragment B , but then the list doesn't get updated, it remains empty..
I've tried placing notifyDataSetChanged()
in every possible way already..
Fragment 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;
}
}
Upvotes: 1
Views: 89
Reputation: 1214
When adding an item to your list your adapter is not notified. Try doing this in your fragment
public void addToData( Password addToList )
{
historyList.add(addToList);
adapter.updateList(historyList);
}
In your adapter you create a new method
public void updateList(ArrayList<Password> newData){
data.clear();
for(int i = 0; i < newData.size(); i++){
data.add(newData.get(i));
}
notifyDataSetChanged();
}
Upvotes: 1