【问题标题】:Remove data that was previously added in App移除之前在 App 中添加的数据
【发布时间】:2020-04-27 02:50:11
【问题描述】:

您好,我在 android studio 上有一个地址簿应用程序的 Java 代码,其中我有添加按钮,所以无论我输入什么名称,它都会被添加到地址簿应用程序中。我做了一个删除按钮,所以无论我输入什么名称,如果它已经添加,它将从地址簿数据中删除它。如何让删除按钮工作

public void btnAddData(View v)
{
    String name= firstName.getText().toString();
    String surName= lastName.getText().toString();
    String phone1= phone.getText().toString();

    Person person= new Person();
    person.name=name;
    person.surname=surName;
    person.phone=phone1;
    persons.add(person);
    setTextToTextView();
}

public void btnRemoveData(View v)
{
    //code to delete name 
}

private void setTextToTextView()
{
    String text = "";
    for (int i=0;i<persons.size(); i++)
    {
        text=text + persons.get(i).name+","+persons.get(i).surname+","+persons.get(i).phone+"\n";
    }
    results.setText(text);
}

【问题讨论】:

    标签: java android xml android-studio android-layout


    【解决方案1】:

    你会这样做:-

    persons.remove(<index or object name>);
    

    【讨论】:

    • 那么什么会替换索引或对象,它会是 people.remove(firstName,lastName);
    • 对象的名称,例如。这里是人。或者您可以获得所需元素的索引。我建议改用数据库。
    【解决方案2】:

    您可以使用循环找到它,然后将其删除。

    public void btnRemoveData(View v)
    {
        String name= firstName.getText().toString();
        for (int i=0;i<persons.size(); i++)
        {
            if(persons.get(i).name.equals(name)) persons.remove(i);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多