How to add a new varible (from previous activity) to a list to display on listview in android studio?

35 views Asked by At
public class Main2Activity extends AppCompatActivity {
ListView simpleList;
String[] thoughtList = {" "};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Intent intent = getIntent();
    String newThought = intent.getStringExtra(MainActivity.EXTRA_TEXT);
    thoughtList.add(newThought); //error comes herer hich wont allow add
    simpleList = (ListView)findViewById(R.id.simpleListView);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    simpleList.setAdapter(arrayAdapter);


}

}

that is my code on second page it brings over varible (saved as newThought) from the previous activity which i then want to add to thoughtList to display in ListView but won't let me add the new varible to the list. does my code need tweaking or should i start again to achieve varible brougth over from previous activity to add into list view on activity 2

1

There are 1 answers

0
Justin Yang On

Have you tried using an ArrayList instead of an string array? String array has a fixed size when array list does not. That may be your problem.