Item in the ArrayList gets replaced instead of appending - java

I am trying to bring all the items from the ArrayList getting saved in EditActivity to the MainActivity using SharedPreferences. But only the last entered data gets displayed in the ListView on the first MainActivity. How can I make all the text entered into the EditText get displayed as a different item in the ListView and not replace the earlier item.
Here is my Code:
MainActivity
public class MainActivity extends AppCompatActivity {
private ListView list;
private Button nextButton;
ArrayList<String> arrayList = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
nextButton = (Button) findViewById(R.id.nextButton);
loadData();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
list.setAdapter(adapter);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nextActivity();
}
});
}
public void nextActivity(){
Intent intent = new Intent(this, EditActivity.class);
startActivity(intent);
}
public void loadData(){
SharedPreferences sp = getSharedPreferences("Data", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sp.getString("list", null);
Type type = new TypeToken<ArrayList<String>>() {}.getType();
arrayList = gson.fromJson(json, type);
if(arrayList == null){
arrayList = new ArrayList<String>();
}
}}
EditActivity
public class EditActivity extends AppCompatActivity {
private EditText input;
private Button addButton;
public ArrayList<String> list = new ArrayList<String>();
public ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
input = (EditText) findViewById(R.id.input);
addButton = (Button) findViewById(R.id.addButton);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getInput = input.getText().toString();
adapter.addAll(getInput);
saveData();
input.setText(""); //clear the value in the edit text
}
});
}
public void saveData(){
SharedPreferences sp = getSharedPreferences("Data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
Gson gson = new Gson();
String json = gson.toJson(list);
editor.putString("list", json);
editor.apply();
}}

I think you forget to add item to list before save and thus the result is you are saving only the last item.
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getInput = input.getText().toString();
list.add(getInput); //add this line
adapter.addAll(getInput);
saveData();
input.setText(""); //clear the value in the edit text
}
});

Related

Array adapter and ListView Showing wrong data

I am trying multiple activities that obtain data from ArrayLists. I have written the layout files and the java files but now one of the activities is showing the ArrayList from another activity.
There is no error so I am not quite sure what I am doing wrong.
The first array and adapter:
public class AlbumsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_albums);
ArrayList<String> albums = new ArrayList<>();
albums.add("Umqele");
albums.add("We are friends");
albums.add("Isiphithiphiti");
albums.add("Scorpion kings");
albums.add("Red");
albums.add("Different world");
albums.add("Irue");
albums.add("Dirty Computer");
albums.add("Anti");
albums.add("Love Girls");
ArrayAdapter<String> albumsAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, albums);
ListView albumView = findViewById(R.id.albums);
albumView.setAdapter(albumsAdapter);
}
The second:
public class PlaylistActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playlists);
ArrayList<String> lists = new ArrayList<>();
lists.add("House");
lists.add("MorningJams");
lists.add("SundayTunes");
ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, lists);
ListView listView = findViewById(R.id.playlist);
listView.setAdapter(listAdapter);
}
Here is the output of your code. It runs as expected. Meaning different data in each Activity.
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String a[] = new String[4];
playlist = findViewById(R.id.open_playlist_bt);
album = findViewById(R.id.open_albumlist_bt);
playlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, PlaylistActivity.class));
}
});
album.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, AlbumsActivity.class));
}
});
}
AlbumsActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String a[] = new String[4];
ArrayList<String> albums = new ArrayList<>();
albums.add("Umqele");
albums.add("We are friends");
albums.add("Isiphithiphiti");
albums.add("Scorpion kings");
albums.add("Red");
albums.add("Different world");
albums.add("Irue");
albums.add("Dirty Computer");
albums.add("Anti");
albums.add("Love Girls");
ArrayAdapter<String> albumsAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, albums);
ListView albumView = findViewById(R.id.album);
albumView.setAdapter(albumsAdapter);
}
public void openPlasylistActivity(View view) {
startActivity(new Intent(this, PlaylistActivity.class));
}
Playlist Activity
public class PlaylistActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ArrayList<String> lists = new ArrayList<>();
lists.add("House");
lists.add("MorningJams");
lists.add("SundayTunes");
ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, lists);
ListView listView = findViewById(R.id.play_list);
listView.setAdapter(listAdapter);
}
}

Moving from one activity to another based on the input in three spinners

I have three spinners in my activity. So I have to open specific activity based on the input of three spinners; if I have entered "1" in my first spinner "cse" in my second spinner and "b" in my third spinner I have to open the respected activity and so on for different combinations.
I have tried using if statements and the app is crashing every time since.
Please beware of the comments in the code those are the codes which I have tried and didn't get any result.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button btn;
/*TextView selection1;
TextView selection2;
TextView selection3;*/
Spinner spin1;
Spinner spin2;
Spinner spin3;
String[] years = {"1","2","3","4"};
String[] branches = {"CSE","ECE","EEE","CIVIL","IT","MECH"};
String[] sections = {"A","B","C"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin1 = (Spinner) findViewById(R.id.spinner1);
spin1.setOnItemSelectedListener(this);
/*List<String> branches = new ArrayList<String>();
branches.add("CSE");
branches.add("ECE");
branches.add("IT");
branches.add("MECH");
branches.add("CIVIL");
branches.add("EEE");*/
ArrayAdapter<String> dataAdapter =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,years);
//Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//attaching dataAdapter to spinner
spin1.setAdapter(dataAdapter);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
spin2.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,branches);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(dataAdapter2);
Spinner spin3 = (Spinner) findViewById(R.id.spinner3);
spin3.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sections);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin3.setAdapter(dataAdapter3);
Button btn= (Button) findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displaycheck();
}
});
}
public void displaycheck(){
/*Spinner s1 = (Spinner)findViewById(R.id.spinner1);
Spinner s2 = (Spinner)findViewById(R.id.spinner2);
Spinner s3 = (Spinner)findViewById(R.id.spinner3);
String sp1 = s1.getPrompt().toString();
String sp2 = s2.getPrompt().toString();
String sp3 = s3.getPrompt().toString();
if (sp1.equals("3")&&sp2.equals("CSE")&&sp3.equals("B")){*/
Toast.makeText(this, "Welcome", Toast.LENGTH_LONG).show();
Intent it = new Intent(this,M2.class);
startActivity(it);
//}
/*else{
Toast.makeText(this, "Invalid input", Toast.LENGTH_LONG).show();
}*/
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//on selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
/*if (spin1.getSelectedItem().toString().equals("3")) {
if (spin2.getSelectedItem().toString().equals("CSE")) {
if (spin3.getSelectedItem().toString().equals("B")) {*/
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
/*Intent it = new Intent(this, M2.class);
startActivity(it);*/
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
/*Button btn =(Button)findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
displaycheck();
}
}
);
}
public void displaycheck(){
Spinner s1 =(Spinner) findViewById(R.id.spinner1);
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
Spinner s3 = (Spinner) findViewById(R.id.spinner3);
String ye = y.getText().toString();
String br = b.getText().toString();
String se = s.getText().toString();
if (ye.equals("3")&&br.equals("cse")&&se.equals("b")) {
Toast.makeText(this, "ra ra ", Toast.LENGTH_LONG).show();
Intent it = new Intent(this, M2.class);
startActivity(it);
}
else{
Toast.makeText(this, "po po", Toast.LENGTH_LONG).show();
}
}*/
}
You are not giving value to variable which are global - Activity Scope - spin1, spin2, spin3.
Instead your are giving value to onCreate or Method specific scope; when you're fetching value from it it's never initialized.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button btn;
Spinner spin1;
Spinner spin2;
Spinner spin3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get spinner in variable
spin1 = (Spinner) findViewById(R.id.spinner1);
...
// Your adapter, ItemListener, DropDownViewResource
spin2 = (Spinner) findViewById(R.id.spinner2);
... // Your adapter, ItemListener, DropDownViewResource
spin3 = (Spinner) findViewById(R.id.spinner3);
... // Your adapter, ItemListener, DropDownViewResource
Button btn= (Button) findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displaycheck();
}
});
}
public void displaycheck(){
String sp1 = spin1.getSelectedItem().toString();
String sp2 = spin2.getSelectedItem().toString();
String sp3 = spin3.getSelectedItem().toString();
// Your combinations check
if (sp1.equals("3")&&sp2.equals("CSE")&&sp3.equals("B")){
// based on combination - redirect user
}
}
}
To get selected item from spinner you use this
String value1 = s1.getSelectedItem().toString();
String value2 = s2.getSelectedItem().toString();
String value3 = s3.getSelectedItem().toString();
Now you can check the values with any logic for example:
if(value1.equals("a") && value2.equals("cse") && value3.equals("b"))
openActivityCodeHere();

Update list view after navigating back to previous activity

Excuse my noobness. I just don't understand how to implement it to work with my code. What I'm doing is editing a name that's in a list view. When editing the name in "EditDeleteList" and get back to the previous activity (ListView) to see the name updated within the list view, nothing happens. I have to go out of the activity completely to see the change reflected. How do I get this to update? I implement an onActivityReult() method but then get this error message "java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference" so I removed it completely. How I could get the list view to update without getting that error message?
ListView.Java
public class ListView extends AppCompatActivity {
private static final String TAG = "ListView";
DatabaseHelper mDatabaseHelper;
Button btnAdd;
private EditText editText;
private android.widget.ListView listView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
mDatabaseHelper = new DatabaseHelper(this);
btnAdd = (Button) findViewById(R.id.btnAdd);
editText = (EditText) findViewById(R.id.editText);
listView = (android.widget.ListView) findViewById(R.id.lv);
ArrayList<String> list = getIntent().getStringArrayListExtra("myList");
android.widget.ListView lv = (android.widget.ListView) findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
//Takes user back to the main activity
ImageView ivBack = (ImageView) findViewById(R.id.ivBackArrow);
ivBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: pressed back arrow");
Intent intent = new Intent(ListView.this, MainActivity.class);
startActivity(intent);
}
});
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newEntry = editText.getText().toString();
if (editText.length() != 0) {
addData(newEntry);
editText.setText("");
} else {
toastMessage("you must put something in the text field");
}
}
});
populateListView();
}
public void addData(String newEntry) {
boolean insertData = mDatabaseHelper.addData(newEntry);
if (insertData) {
toastMessage("Successfully inserted");
recreate();
} else {
toastMessage("Whoops, something went wrong");
}
}
private void toastMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
private void populateListView() {
Log.d(TAG, "populateListView: displaying data in the listview");
//get data and append to list
Cursor data = mDatabaseHelper.getData();
ArrayList<String> listData = new ArrayList<>();
while(data.moveToNext()) {
//get the value from the database in column 1
//set it to the arraylist
listData.add(data.getString(1));
}
//create arraylist and set it to the adapter
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
listView.setAdapter(adapter);
//set onclick listen to edit activity
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String name = adapterView.getItemAtPosition(position).toString();
Log.d(TAG, "onItemClick: you clicked on " + name);
Cursor data = mDatabaseHelper.getItemID(name); //get the id associated with that name
int itemID = -1;
while (data.moveToNext()) {
itemID = data.getInt(0);
}
if (itemID > -1) {
Log.d(TAG, "onItemID: the ID is: " + itemID);
Intent editScreenIntent = new Intent(ListView.this, EditDeleteList.class);
editScreenIntent.putExtra("id",itemID);
editScreenIntent.putExtra("name",name);
startActivity(editScreenIntent);
} else {
toastMessage("No ID found");
}
}
});
}
}
EditDeleteList.java
public class EditDeleteList extends AppCompatActivity {
private static final String TAG = "EditDeleteList";
DatabaseHelper mDatabaseHelper;
private ImageView ivDelete;
private ImageView ivApprove;
private EditText editHashtag;
private String selectedName;
private int selectedID;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_delete);
mDatabaseHelper = new DatabaseHelper(this);
editHashtag = (EditText) findViewById(R.id.editHashtag);
ivDelete = (ImageView) findViewById(R.id.ivDelete);
ivApprove = (ImageView) findViewById(R.id.ivApprove);
//get the intent extra from the ListView activity
final Intent receivedIntent = getIntent();
//get item ID passed as an extra
selectedID = receivedIntent.getIntExtra("id", -1);
//get name passed as an extra
selectedName = receivedIntent.getStringExtra("name");
//set text field to selected item text
editHashtag.setText(selectedName);
ivApprove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String item = editHashtag.getText().toString();
if (!item.equals(null)) {
mDatabaseHelper.updateName(item, selectedID, selectedName);
} else {
toastMessage("you must enter a #hashtag");
}
finish();
}
});
}
private void toastMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
In the EditDeleteList.java I have an onClickListener that saves the changes and goes back to the previous activity by using finish();
ivApprove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String item = editHashtag.getText().toString();
if (!item.equals(null)) {
mDatabaseHelper.updateName(item, selectedID, selectedName);
} else {
toastMessage("you must enter a #hashtag");
}
finish();
}
});
Notify the adapter in some part of the activity lifecycle.
OnCreate() should not run when you go back (this is the reason you have to completely recreate the activity to see the list updated) so you should use OnRestart/OnStart/OnResume to notify the adapter to check for new items.
Check this image

Pass value from first activity to third activity into a listview overrides old value

I have an app, where a user can enter a number and save to listView. The app is split into 3 activities.
The first activity is the MainActivity, which does something else. The second activity is where I enter a number and save it. The code Is below
public class HomeTeamScored extends Activity {
protected static EditText display;
protected static ArrayAdapter<String> adapter;
Button addButton;
CheckBox checkBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rect_activity_home_goal_player);
display = (EditText) findViewById(R.id.editText);
addButton = (Button) findViewById(R.id.btn);
checkBox = (CheckBox) findViewById(R.id.checkBox);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String task = display.getText().toString();
adapter.add(task);
adapter.notifyDataSetChanged();
SavePreferences();
Intent i = new Intent(HomeTeamScored.this, MainActivity.class);
startActivity(i);
finish();
}
});
}
protected void SavePreferences() {
// TODO Auto-generated method stub
SharedPreferences data = getApplicationContext().getSharedPreferences("saveNumber", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = data.edit();
for (int i = 0; i < adapter.getCount(); ++i){
// This assumes you only have the list items in the SharedPreferences.
editor.putString(String.valueOf(i), adapter.getItem(i));
}
editor.commit();
}
}
In my third activity, I retrieve the saved number.
public class ScoreBoardOverview extends Activity {
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rect_activity_score_board_overview);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
lv = (ListView) findViewById(R.id.listView);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
loadPreferences();
}
protected void loadPreferences(){
SharedPreferences data = getApplicationContext().getSharedPreferences("saveNumber", Context.MODE_PRIVATE);
for (int i = 0;; ++i){
final String str = data.getString(String.valueOf(i), "");
if (!str.equals("")){
adapter.add(str);
} else {
break; // Empty String means the default value was returned.
}
}
}
}
All this is working fine, and it adds a number to listView, but the problem is that it overrides the old value, were instead it should add a second row.
Anyone who can help??
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String task = display.getText().toString();
adapter.add(task);
adapter.notifyDataSetChanged();
SavePreferences();
Intent i = new Intent(HomeTeamScored.this, MainActivity.class);
startActivity(i);
finish();
}
});
}
here your adapter item size will be 0 when you call second activity each time. if you want to add second or third item you should write your
protected void loadPreferences(){
SharedPreferences data = getApplicationContext().getSharedPreferences("saveNumber", Context.MODE_PRIVATE);
for (int i = 0;; ++i){
final String str = data.getString(String.valueOf(i), "");
if (!str.equals("")){
adapter.add(str);
} else {
break; // Empty String means the default value was returned.
}
}
}
method in second activity too. and call loadPreferences() after this line
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
your adapter data size will be changed.

Android studio pass ListView to another activity

I am trying to pass an array list to another activity but it seems that is not enough. I searched all day to pass the array list with "intent" but with no success. I wrote a code for learning purposes. How to pass the data and show the Arraylist in a second activity?
The action button is btn_save. If you want further details let me know.
The code is in
MainActivity (first activity):
public class MainActivity extends AppCompatActivity {
ArrayList<String> addArray = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editTextOne = (EditText) findViewById(R.id.editTextOne);
final EditText editTextTwo = (EditText) findViewById(R.id.editTextTwo);
Button btn_showText = (Button) findViewById(R.id.buttonShow);
final TextView textView = (TextView) findViewById(R.id.textResults);
Button btn_refresh = (Button) findViewById(R.id.btn_refresh);
Button btn_close = (Button) findViewById(R.id.btn_close);
Button btn_save = (Button) findViewById(R.id.btn_save);
final ListView showMe = (ListView) findViewById(R.id.list_items);
btn_showText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = editTextOne.getText().toString();
String textTwo = editTextTwo.getText().toString();
if (text.length() == 0 || textTwo.length() == 0){
textView.setText("You must enter Name & Surname");
}else {
textView.setText(Html.fromHtml(
"<font color=\"red\">"+ text + "</font> " +
"<font color=\"blue\"><b>" + textTwo + " </b></font>"));
}
}
});
btn_refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
startActivity(getIntent());
}
});
btn_close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
System.exit(0);
}
});
btn_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Context context = getApplicationContext();
// CharSequence mytext = "Hahahaha";
// int duration = Toast.LENGTH_SHORT;
// Toast toast = Toast.makeText(context,mytext,duration);
// toast.show();
String text = editTextOne.getText().toString();
String textTwo = editTextTwo.getText().toString();
String getInput = text + textTwo;
if (addArray.contains(getInput)){
Toast.makeText(getBaseContext(), "Item already Added!", Toast.LENGTH_LONG).show();
}
else if (getInput == null || getInput.trim().equals("")){
Toast.makeText(getBaseContext(), "Input field is empty", Toast.LENGTH_LONG).show();
}
else{
addArray.add(getInput);
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);
showMe.setAdapter(adapter);
Intent intent = new Intent(MainActivity.this, ListOfNames.class);
((EditText)findViewById(R.id.editTextOne)).setText(" ");
((EditText)findViewById(R.id.editTextTwo)).setText(" ");
}
}
});
}
public void onButtonClick(View v){
if (v.getId() == R.id.btn_list){
Intent i = new Intent(MainActivity.this, ListOfNames.class);
startActivity(i);
}
}
}
ListOfNames second activity: (almost empty)
public class ListOfNames extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_screen);
}
}
If you are trying to pass a arraylist of string then it will be easy. Just pass arraylist with intent like this:
ArrayList<String> list = new ArrayList<String>();
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putStringArrayListExtra("key", list);
startActivity(intent);
And receive it in ActivityTwo like this:
ArrayList<String> list = getIntent().getStringArrayListExtra("key");
I found a temporary solution for this (The app is not broke). The only problem is the arraylist didn't increased. It contains and show only the last value.
Main Activity:
...
else{
// addArray.add(getInput);
// ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);
// showMe.setAdapter(adapter);
Intent intent = new Intent(MainActivity.this, ListOfNames.class);
intent.putExtra("data", getInput);
startActivity(intent);
// ((EditText)findViewById(R.id.editTextOne)).setText(" ");
// ((EditText)findViewById(R.id.editTextTwo)).setText(" ");
}
...
ListOfNames (Second Activity):
public class ListOfNames extends Activity {
ArrayList<String> addArrayT = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_screen);
Bundle bundle = getIntent().getExtras();
String data = bundle.getString("data");
Button btn_more = (Button) findViewById(R.id.btn_more);
ListView showMe = (ListView) findViewById(R.id.list_items);
addArrayT.add(data);
ArrayAdapter<String> adapterT = new ArrayAdapter<>(ListOfNames.this, android.R.layout.simple_list_item_1, addArrayT);
showMe.setAdapter(adapterT);
}
public void onClickMore(View v){
if (v.getId() == R.id.btn_more){
Intent i = new Intent(ListOfNames.this, MainActivity.class);
startActivity(i);
}
}
}

Categories