J
J
justxz2020-01-04 20:00:46
Java
justxz, 2020-01-04 20:00:46

Why is the received data not displayed?

I want to receive data from the second activity and display it in the listView in the first one, but for some reason nothing appears on the screen.
The code itself:
The first activity, in it I create a second activity and display a listView on the screen

public class MainActivity extends AppCompatActivity {

    public List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = new ArrayList<>();

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.array_adapter, list);

        ListView lv = (ListView)findViewById(R.id.listView);
        lv.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Intent intent = getIntent();
        String item = intent.getStringExtra("main");
        list.add(item);
    }

    public void floatinButton1OnClick(View view) {
        Intent intent = new Intent(this, AddElement.class);
        startActivityForResult(intent, 1);
    }
}

The second activity, in it I read the data from the editText and pass it to the first activity
public class AddElement extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_element);
    }

    public void floatingButton1OnClick_AddElement(View view) {

        TextView textView = (TextView)findViewById(R.id.editText1_add_element);
        String main_item = textView.getText().toString();

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("main", main_item);

        setResult(RESULT_OK, intent);
        finish();
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
raxmatart, 2020-02-01
@raxmatart

So you just add an element to the list, and the adapter itself does not inform about it, therefore it is not displayed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question