Reputation: 2480
I have a display of some data on one fragment, and each time I click on a button it should add to the listView in the second fragment. For some reason when I click the button I the listView doesn't change.
fragment1 :
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
HistoryFragment.addToHistory( myObject );
}});
HistoryFragment (listView) :
public class HistoryFragment extends ListFragment
{
private static List<Password> historyList = new ArrayList<MyObject>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
historyList = myManager.getMyObjects ();
}
public static void addToHistory( MyObject myObject )
{
historyList.add( myObject );
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
MyObjectAdapter adapter = new MyObjectAdapter( historyList );
setListAdapter(adapter);
return view;
}
....
....
....
private class MyObjectAdapter extends BaseAdapter
{
private final ArrayList<MyObject> data;
public MyObjectAdapter( List<MyObject> historyList )
{
data = new ArrayList<MyObject>();
data.addAll( historyList );
}
@Override
public int getCount()
{
return data.size();
}
@Override
public MyObject 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;
notifyDataSetChanged();
if (convertView == null)
{
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_list_item, parent, false);
}
else
{
result = convertView;
}
MyObject item = getItem( position );
( (TextView) result.findViewById(R.id.historyObjectTextView) ).setText(item.toString());
return result;
}
}
fragment main(1) :
public class MainFragment extends Fragment
{
Map<String,MyObject> objects = new HashMap<String,MyObject>();
List< MyObject > objectsList = new ArrayList< MyObject >();
TextView nextDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
objects = myManager.getInstance().getObjects();
objectsList = MyObject.getObjectsList();
}
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
View view = inflater.inflate(R.layout.fragment_main, container, false);
nextDisplay = ( TextView ) view.findViewById( R.id.displayTextView );
nextDisplay.setText( objectsList.get( 0 ).getObjectString() );
Button nextButton = ( Button ) view.findViewById( R.id.nextObjectButton );
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
if( objectsList.size() > 0 )
{
objects.get(objectsList.get( 0 ).getObjectString()).setTried( true );
long currentTime = System.currentTimeMillis();
objects.get(objectsList.get( 0 ).getObjectString()).setTime( currentTime );
HistoryFragment.addToHistory( objectsList.get( 0 ) );
objectsList.remove( 0 );
if( objectsList.size() > 0 && !objectsList.get( 0 ).getTried() )
{
nextCodeDisplay.setText( objectsList.get( 0 ).getObjectString() );
}
}
}});
return view;
}
Upvotes: 1
Views: 61
Reputation: 2456
Like This.
private List<Password> historyList = new ArrayList<MyObject>();
private MyObjectAdapter adapter
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
adapter = new MyObjectAdapter( historyList );
setListAdapter(adapter);
return view;
}
private static List<Password> historyList = new ArrayList<MyObject>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
historyList = myManager.getMyObjects ();
}
public void addToHistory( MyObject myObject )
{
historyList.add( myObject );
adapter.notifyDataSetChanged();
}
Upvotes: 1
Reputation: 363975
To populate or update the ListView you have to update the list used by the adapter. In your code you are updating the list inside the Fragment, not the data used by the adapter.
In your code you should do something like this:
adapter.add(MyObject myObject);
in your adapter:
private class MyObjectAdapter extends BaseAdapter{
public void add( List<MyObject> historyList )
{
if (data == null)
data = new ArrayList<MyObject>();
data.addAll( historyList );
notifyDataSetChanged();
}
}
Upvotes: 1
Reputation: 5986
Try calling notifyDataSetChanged()
on the OnClickListener:
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
HistoryFragment.addToHistory( myObject );
notifyDataSetChanged();
}});
Upvotes: 0