I have made a listView that is populated with an array. I can add data to this listview by entering in data in a textbox called txtinputs and then click a button called btnAdds this all works fine when I have it all in a single class. However what I want to do is have this on 2 separate screens using 2 separate classes, the txtinputs and the btnAdds in a separate class than the listView. When the data is entered into the txtinputs and the btnAdds is selected it will add that data into the listView on a seperate class. I want the listView in a class called ListDeadlines and my textBox and Button in a class called AddDeadline.
I currently add the data into the listView like this.
public ArrayList<String> arrayList;
public ArrayAdapter<String> adapter;
public EditText txtInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_deadlines);
final ListView listView = (ListView)findViewById(R.id.listv);
String[] items= {"HCI","ITM","Presentation"};
arrayList=new ArrayList<>(Arrays.asList(items));
adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.txtitem,arrayList);
listView.setAdapter(adapter);
txtInput=(EditText)findViewById(R.id.txtinputs);
Button btAdd=(Button)findViewById(R.id.btnAdds);
btAdd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String newItem=txtInput.getText().toString();
arrayList.add(newItem);
adapter.notifyDataSetChanged();;
}
});
}
I have tried to do it like this in my AddDeadline class like this however I get a runtime on the button click
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_deadline);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final ListDeadlines lst= new ListDeadlines();
lst.txtInput=(EditText)findViewById(R.id.txtinput);
Button btAdd=(Button)findViewById(R.id.btnAdd);
btAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newItem = lst.txtInput.getText().toString();
lst.arrayList.add(newItem);
lst.adapter.notifyDataSetChanged();
}
});
}
