I would like to add elements to the listView in one fragment when the button in another fragment is pushed. I tried to figure it out by myself, but I couldn't find any solution.
These are my trials:
Code:
HistoryFragment.java - fragment in which I want a list view to be displayed
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class HistoryFragment extends Fragment {
ListView listView;
HistoryAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View history = inflater.inflate(R.layout.fragment_history, container, false);
ArrayList<ToSave> arrayOfData = new ArrayList<ToSave>();
adapter = new HistoryAdapter(getActivity().getBaseContext(), arrayOfData);
listView = (ListView) history.findViewById (R.id.listView1);
listView.setAdapter(adapter);
//ToSave toSave = new ToSave();
//adapter.add(toSave);
return history;
}
}
HistoryAdapter.java - my listView adapter
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class HistoryAdapter extends ArrayAdapter<ToSave>{
public HistoryAdapter(Context context, ArrayList<ToSave> records) {
super(context, R.layout.history_item, records);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ToSave record = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.history_item, parent, false);
}
TextView tvDate = (TextView) convertView.findViewById(R.id.textViewDate);
TextView tvStats = (TextView) convertView.findViewById(R.id.textViewStats);
// Populate the data into the template view using the data object
tvDate.setText(Integer.valueOf(record.numberDrinks).toString());
tvStats.setText(Integer.valueOf(record.numberDrinks).toString());
// Return the completed view to render on screen
return convertView;
}
}
Interface which I created in the fragment from which I want to controll the list view:
OnFinishedParty mCallback;
public interface OnFinishedParty {
public void onFinishedParty();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnFinishedParty) activity;
}
catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnFinishedParty");
}
}
Button listener in this Fragment
Button button5 = (Button) started.findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCallback.onFinishedParty();
}
});
in MainActivity:
public void onFinishedParty() {
///What to put here??????
///Tried this but did not work:
ToSave toSave = new ToSave();
HistoryFragment.adapter.add(toSave);
HistoryFragment.adapter.notifyDataSetChanged();
}
0 comments:
Post a Comment