我的 ListView 在执行第二个更改后更新第一个更改 [英] My ListView updates the first change after doing a second one

查看:29
本文介绍了我的 ListView 在执行第二个更改后更新第一个更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 MainActivity 中有一个 ListView 和一个添加"按钮.当按钮被按下时,您将进入第二个活动,其中包含一个表单,该表单接受一些输入(tokenName、tokenID 和 averagePrice)并创建一个对象 'Token' .第二个活动还有一个按钮,可将令牌保存到 TokenList 并再次将您带到 MainActivity.问题是您刚刚创建的令牌没有显示,但是如果您创建一个新令牌,第一个出现在 ListView 中

I have a ListView and an 'add' button in my MainActivity. When the button is pressed, you are taken to a second activity with a form that takes some input (tokenName, tokenID and averagePrice) and creates an object 'Token' . The second activity also has a button that saves the token to a TokenList and takes you to the MainActivity again. The thing is that the token you just created isn't showing but if you create a new one, the first one appears in the ListView

主活动代码:

public class MainActivity extends AppCompatActivity {

    public static ListView listView;
    ArrayList<String> arrayList = new ArrayList<>();
    private Button addButton;
    public static ArrayAdapter<String> arrayAdapter;
    private EditText name;
    public static TokenList tokenList = new TokenList();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addButton = (Button) findViewById(R.id.addbutton);
        listView = (ListView)findViewById(R.id.listview);
        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
        listView.setAdapter(arrayAdapter);


        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openAddToken();
                arrayList.clear();
                for(Token token : tokenList.tokenList){
                    addToList(token.getTokenName());
                }
            }
        });

    }

    //Calls addToList method in TokenList class
    public void addToList(String tokenName){
        arrayList.add(tokenName);
    }

    //Opens the second activity with the new token form
    public void openAddToken(){
        Intent intent = new Intent(this, AddToken.class);
        startActivity(intent);
    }
}

第二个活动代码:

public class AddToken extends AppCompatActivity {
    private Button addButton;
    EditText tokenName, tokenID, averagePrice;
    TokenList tokenList = MainActivity.tokenList;
    Token token = new Token();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        tokenName=(EditText) findViewById(R.id.tokenname2);
        tokenID=(EditText) findViewById(R.id.tokenid2);
        averagePrice=(EditText) findViewById(R.id.averageprice);

        addButton = (Button) findViewById(R.id.add);
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                token.setTokenName(tokenName.getText().toString());
                token.setTokenID(tokenID.getText().toString());
                token.setTokenName("err2");
                token.setAveragePrice(Double.parseDouble(averagePrice.getText().toString()));
                tokenList.addToTokenList(token);

                finish();

            }
        });
    }
}

如何在您按下第二个活动中的接受"按钮时更新 MainActivity 中的 ListView?我是 Java 新手,对 android 开发更感兴趣,所以请随时指出任何不好的做法.

How can I update the ListView in MainActivity the moment you press the 'accept' button in the second activity? I'm new to java and even more to android development so feel free to point out any bad practice.

谢谢!

推荐答案

更好的实施方式是使用本地数据库,例如
一个更简单的方法来解决你的问题,更好的维护性是在你的主文件中使用 startActivityForResult()onActivityResult() .
一个例子是,在 .onClick() 方法中的 AddToken.class 中添加:

A better implementatiojn would be using a local database with for example the ROOM library.
A simpler approach to your problem, and better for mantainability would be using the startActivityForResult() and onActivityResult() in your main.
An example would be, in your AddToken.class in the .onClick() method add:

Intent resultIntent = new Intent();
resultIntent.putExtra("some_key", "String data"); 
setResult(Activity.RESULT_OK, resultIntent);
finish();

并在 MainActivity.class 中添加以下内容:

And in MainActivity.class add the following:

//Inside the onClick() method in the onCreate()
//...
Intent intent = new Intent(MainActivity.this, AddToken.class);  
startActivityForResult(intent, ANY_NOT_NEGATIVE_NUMBER);
//...
//Add the following outside onCreate() as a new method
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  switch(requestCode) {
    case (ANY_NOT_NEGATIVE_NUMBER) : {
      if (resultCode == Activity.RESULT_OK) {
        // TODO Extract the data returned from the child Activity.
        String returnValue = data.getStringExtra("some_key");
      }
      break;
    } 
  }
}

在此代码中,ANY_NOT_NEGATIVE_NUMBER 是您可以在 MainActivity.class 中定义的数字.不要使用共享引用,而是使用意图的 putExtra() 方法(在您的 AddToken.class 中将数据传递回 MainActivity> 并将其添加到您的列表中,并使用 Listview 适配器上的 notifyDataSetChanged() 通知适配器更改.

In this code ANY_NOT_NEGATIVE_NUMBER is a number that you can define in your MainActivity.class. Instead of having a shared reference, then use the putExtra() method of the intent (in your AddToken.class to pass the data back to MainActivity and there add it to your list and notify the adapter of the change with notifyDataSetChanged() on your Listview adapter.

我建议使用 recyclerview 以获得更好的性能和功能.你可以看看

I would suggest to use a recyclerview for better performance and functions. You can check it out HERE: Create dynamic lists with RecyclerView

这篇关于我的 ListView 在执行第二个更改后更新第一个更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆