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();
}
});
}
In your ListDeadLinesActivity
when a user hit the "+" button you want to start AddDeadlineActivity
for a result.
You can put this code inside the onClick
of that "+" button.
Intent intent = new Intent(this, AddDeadlineActivity.class);
startActivityForResult(intent, ADD_DEADLINE_REQUEST);
ADD_DEADLINE_REQUEST
is the request code, you can declare a static int.
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
btAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent returnIntent = getIntent();
returnIntent.putExtra("result", yourEditText.getText().toString());
setResult(RESULT_OK,returnIntent);
finish();
}
});
}
In your ListDeadLinesActivity
override onActivityResult
method,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ADD_DEADLINE_REQUEST){
if (resultCode == RESULT_OK) {
String item = data.getStringExtra("result");
arrayList.add(item);
adapter.notifyDataSetChanged();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.