我正在尝试创建一个应用程序,您可以在其中将字符串添加到另一个活动的列表中,但每次我在列表中添加新字符串时都会替换旧字符串。
我该如何解决这个问题?
列表所在的代码。
@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();
}
}
xml文件代码:
<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" />
您输入文字的文件的代码:
@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);
}
});
}
最后是前一段代码的xml代码:
<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" />
答案 0 :(得分:2)
这是因为您每次进入活动时都会创建一个新列表。因此,保存列表的字段必须是“永久”字段。我建议你声明一个静态列表并检查它是否为空:
您的列表所在的代码:
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);
}
}
顺便说一下,你的代码中有几个“问题”:
答案 1 :(得分:-1)
试试这个,
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();