添加新项目时,Android listView无法获得更新

时间:2022-09-25 18:39:31

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:

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" />

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:

最后是前一段代码的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" />

2 个解决方案

#1


1  

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:

顺便说一下,你的代码中有几个“问题”:

  1. You added the item to the adapter, not to the list
  2. 您将项目添加到适配器,而不是列表

  3. You initialized your listview only if your bundle isn't null, why? Your listview will remain everytime, you don't need to initialize it with this check
  4. 只有在捆绑包不为空时才初始化列表视图,为什么?您的列表视图将保留在每次,您不需要使用此检查初始化它

  5. You created a final intent in your other class outside the click. This isn't programmatically wrong, but there isn't any motivation in this case for which you need to do it. It's better do it locally inside the click as you did in the first class
  6. 您在点击之外的其他课程中创建了最终意图。这在程序上不是错误的,但在这种情况下,您没有任何动机需要这样做。最好在点击内部进行本地操作,就像在第一堂课中一样

#2


-1  

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();

#1


1  

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:

顺便说一下,你的代码中有几个“问题”:

  1. You added the item to the adapter, not to the list
  2. 您将项目添加到适配器,而不是列表

  3. You initialized your listview only if your bundle isn't null, why? Your listview will remain everytime, you don't need to initialize it with this check
  4. 只有在捆绑包不为空时才初始化列表视图,为什么?您的列表视图将保留在每次,您不需要使用此检查初始化它

  5. You created a final intent in your other class outside the click. This isn't programmatically wrong, but there isn't any motivation in this case for which you need to do it. It's better do it locally inside the click as you did in the first class
  6. 您在点击之外的其他课程中创建了最终意图。这在程序上不是错误的,但在这种情况下,您没有任何动机需要这样做。最好在点击内部进行本地操作,就像在第一堂课中一样

#2


-1  

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();