Reputation: 23
I'm trying to create a app where you can add strings to a list from another activity, but every time i add a new string to the list it replaces the old one.
How can I fix this?
Code where the list is.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_list);
Button button = (Button) findViewById(R.id.addBtn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ShowList.this, AddToList.class));
}
});
Intent i = getIntent();
Bundle b = i.getExtras();
if(b!=null) {
String textToList = b.getString("listText");
List<String> stuff = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, stuff);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
adapter.add(textToList);
adapter.notifyDataSetChanged();
}
}
the xml files code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ShowList">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/addBtn"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="List:"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_centerHorizontal="true"
android:layout_above="@+id/addBtn"
android:layout_below="@+id/textView" />
Code for the file where you input text:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_to_list);
final Bundle b = new Bundle();
final Intent i = new Intent(AddToList.this, ShowList.class);
final EditText text = (EditText)this.findViewById(R.id.listText);
Button btn = (Button) findViewById(R.id.btnAdd);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = text.getText().toString();
b.putString("listText", s);
i.putExtras(b);
startActivity(i);
}
});
}
And finally the xml code for the previous peace of code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.mathias.listapp.AddToList">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listText"
android:layout_alignParentTop="true"
android:layout_marginTop="100dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:inputType="text"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD"
android:id="@+id/btnAdd"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
Upvotes: 2
Views: 463
Reputation: 16214
This happens because you are creating a new List everytime you enter in your activity. So the field in which you save your list has to be a "permanent" one. I recommend you to declare a static list and check if it's not null:
Code where your list is:
public class Nameofmyclass{
static List<String> stuff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_list);
if(stuff==null){
stuff = new ArrayList<String>();
}
ListView list = (ListView) findViewById(R.id.listView);
Button button = (Button) findViewById(R.id.addBtn);
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, stuff);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ShowList.this, AddToList.class));
}
});
Intent i = getIntent();
Bundle b = i.getExtras();
if(b!=null) {
String textToList = b.getString("listText");
stuff.add(textToList);
}
list.setAdapter(adapter);
}
}
By the way there are several "problems" in your code:
Upvotes: 2
Reputation: 3688
try this ,
String textToList = b.getString("listText");
List<String> stuff = new ArrayList<String>();
stuff.add(textToList)
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, stuff);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
Upvotes: -1