How to modify a Listview present in another Activity without using Intent?

Asked

Viewed 120 times

0

I created a listview in the initial Activity of a project, and would like to know how to modify it from other activitys, without having to use the "Intent.".

Activity01 (where the listview is):

package genesysgeneration.lvatt;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public ListView lvItens;
    public ArrayList<String> alsItens;
    public ArrayAdapter<String> aasItens;
    private Button btnNext;

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

        lvItens=(ListView)findViewById(R.id.lvItens);
        alsItens=new ArrayList<String>();
        aasItens=new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1);
        lvItens.setAdapter(aasItens);

        btnNext=(Button)findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent it = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(it);

            }
        });

    }

}

Activity02 (one of the activitys that modifies it):

package genesysgeneration.lvatt;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

public class Main2Activity extends AppCompatActivity {

    private Button btnArco, btnEspada, btnBack;

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

        btnArco=(Button)findViewById(R.id.btnArco);
        btnEspada=(Button)findViewById(R.id.btnEspada);
        btnBack=(Button)findViewById(R.id.btnBack);

        btnArco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                alsItens.add("Arco");
                aasItens.notifyDataSetChanged();

            }
        });

        btnEspada.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                alsItens.add("Espada");
                aasItens.notifyDataSetChanged();

            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent it = new Intent(Main2Activity.this, Main3Activity.class);
                startActivity(it);

            }
        });

    }
}

Notice that I put in the "sets" of each Button the modification that I would like to happen (It would work if everything was in the same).

I would like the modifications to happen at the moment when each of the buttons were clicked (setOnClickListener) and that the listview was updated and correct independent of the previous Activity.

The initial Activity, where the listview, will always be executed.

  • 1

    My suggestion is to use the results of Activity to receive a value resulting from MainActivity2 in his MainActivity. The only problem I see is you need to finalize the MainActivity2 at some point informing the items to be added to MainActivity.

  • then the change would only occur when the main2 was finished?

  • That, exact, you need to set the result and finalize the MainActivity2 for another Activity to be notified.

  • but I could do this in order to finish main2 and go, for example, to a main3 and still the main1 be modified?

  • But you don’t have to upgrade Main to Main3, right? Just need to update the Main when you go back to it, which you would do when giving back press I think, you would have to intercept the back press in Main2 and set the result.

  • how do I do that?

  • I’m going to need to run a little test and put in an answer.

  • Okay, thanks in advance!

Show 3 more comments

1 answer

2

As I said, the simplest way is to use the results of Activity to send a result from MainActivity2 to the MainActivity.

Using this technique would look like this:

public class Main2Activity extends AppCompatActivity {

    private Button btnArco, btnEspada, btnBack;
    private ArrayList<String> resultados = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Restante do seu código...

        btnArco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultados.add("Arco");
            }
        });

        btnEspada.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultados.add("Espada");
            }
        });
    }

    @Override
    public void onBackPressed() {
        final Intent resultado = new Intent();

        resultado.putStringArrayListExtra("resultado", resultados);
        setResult(Activity.RESULT_OK, resultado);

        super.onBackPressed();
    }
}

public class MainActivity extends AppCompatActivity {

    private static final int RC_ITEMS = 231;

    // Restante das suas variáveis...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Restante do seu código...
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(MainActivity.this, Main2Activity.class);
                startActivityForResult(it, RC_ITEMS);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RC_ITEMS && resultCode == Activity.RESULT_OK) {
            ArrayList<String> items = data.getStringArrayListExtra("resultado");

            alsItens.addAll(items);
            aasItens.addAll(items);    
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}
  • nn will need the notifyDataSetChanged?

  • I don’t think so, looking at the source code of ArrayAdapter, he already makes that call on addAll. But you can call if you need.

  • I think I will have to put it only if I am going to check for existence, in case the item has already been added, and not be repeating http://answall.com/questions/184756/howto erase a certain data

  • error on the line => private static final RC_ITENS = 231;

  • Repetition there is already another problem, my suggestion would be to use a Set instead of a list, the Bundle natively accepted a Set of String.

  • sim, this is subject of another issue of another problem, thing to be treated in another question if it is the case, but returning to this RC_ITENS... is accusing error

  • Missed to define the type, already edited the answer with the right type.

  • It turned out if I immediately go from main2 to main1. Now if before that I go to main3 or to any other Activity, nothing happens. Also if I go from main2 to main1, then to main2 and main1 (and the second time I don’t add anything) lisVview appears empty. I would like it to be saved permanently while the application is running.

  • As your case is much more complex, I see no alternative but to store everything in a local bank, solves all problems, although it requires a little more complexity in the solution as well.

  • Hmmmm any link ki can recommend me so I can resolve such a stalemate? I don’t want to take your time more than Jah took, it would be abuse

  • Nothing, I just can’t post an answer now because it will take a long time, but this documentation is very rich: https://developer.android.com/training/basics/data-storage/databases.html. Just be careful not to access the bank in the Main thread for no problems.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.