Answer the question
In order to leave comments, you need to log in
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);
}
}
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question