You should use a fragment to hold the list view. And have your activity hold that fragment. This way you aren't trying to pass all that data from one activity to the next, you can just add it within the same activity where it's being shown.
So you'd end up with a MainActivity like:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new WidgetFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Fragment Class:
public class WidgetFragment extends Fragment {
private ArrayAdapter<String> widgetAdapter;
public WidgetFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add this line in order for this fragment to handle menu events.
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.widgetfragment, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_refresh) {
FetchWidgetData widgetTask = new FetchWidgetData();
widgetTask.execute("50131");
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create some dummy data for the ListView. Here's a sample weekly forecast
String[] data = {
"Dummy Data",
"Just an Example",
"Delete at anytime",
"Project 4",
"Project 5",
"Filler Data",
};
List<String> listData = new ArrayList<String>(Arrays.asList(data));
// Now that we have some dummy forecast data, create an ArrayAdapter.
// The ArrayAdapter will take data from a source (like our dummy forecast) and
// use it to populate the ListView it's attached to.
widgetAdapter =
new ArrayAdapter<String>(
getActivity(), // The current context (this activity)
R.layout.list_item_widget, // The name of the layout ID.
R.id.list_item_widget_textview, // The ID of the textview to populate.
listData);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// Get a reference to the ListView, and attach this adapter to it.
ListView listView = (ListView) rootView.findViewById(R.id.listview_widget);
listView.setAdapter(widgetAdapter);
//!!!You can add your button click listener here. And put the following line inside it to add data to your list adapter!!!
widgetAdapter.add("whatever whatever whatever"); //appends to end of list
widgetAdapter.clear(); //to reset/clear the whole thing.
widgetAdapter.remove(widgetAdapter.getItem(index); //removes the desired object. Note - remove takes object not an index.
return rootView;
}
}
Edit: Key List View Steps.
1) Create a reference to the root view where your list view lives
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
2) Create an Adapter to hold the data.
widgetAdapter =
new ArrayAdapter<String>(
getActivity(), // The current context (this activity)
R.layout.list_item_widget, // The name of the layout ID.
R.id.list_item_widget_textview, // The ID of the textview to populate.
new ArrayList<String>);
3) Create a reference to the listview itself
ListView listView = (ListView) rootView.findViewById(R.id.listview_widget);
4) Set the listview adapter
ListView.setAdapter(widgetAdapter);
5) Add data to your adapter
widgetAdapter.add("Whatever");
Edit: example static method to add to your Main Activity class assuming you have the list adapter declared as a static class variable,
public static void addWordToList(String word){
listAdapter.add(word); //LIST ADAPTER MUST BE STATIC OR THERE MAY BE NO INSTANCE OF IT
}
}
then you can call this from inside your test activity by doing
MainActivity.addWordToList("Whatever word you want to add");