1

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();

        }
    });
}

I want to achieve something like thisenter image description here

2
  • After adding item, do you want to close your second activity and go back to first one? Use startActivityForResult and pass your data in an intent. Commented Feb 13, 2016 at 20:56
  • Yes that's exactly what I want to do @GokhanArik I'm new with android development, if you don't mind is there anyways you could show me how I could go about doing this. Thanks in advance. Commented Feb 13, 2016 at 21:07

1 Answer 1

1

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();
        }
    }
}

Getting a Result from an Activity

Sign up to request clarification or add additional context in comments.

4 Comments

I will give that a shot and get back to you thanks :)
Where would I enter that second section of code? Would I just enter it under my @Override public void onClick(View v) { String newItem=txtInput.getText().toString(); arrayList.add(newItem); adapter.notifyDataSetChanged(); } });
You legend it worked like a dream. Thanks so much. If you up-vote my question I can up-vote your answer as I don't have enough reputation at the moment. Thanks again.
You need to learn which answer to accept first. The guy above me just copied the answer from my code and you accepted his answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.