0

Lets say I have the ability to add player names in my adapter as inputs and then show them in the listview. Now i want to show these names that the user just added in my second activity, but not as list view. But single items. How can I don that?

Here i create the adapter and add player names:

public class ZaidejaiActivity extends ActionBarActivity implements View.OnClickListener{
    public Button mBtnIstrinti;
    public Button mBtnPrideti;
    public Button mBtnPradeti;
    public EditText mYrasytiVarda;
    public ListView mZaidejai;

    ArrayList<String> list = new ArrayList<String>();
    ArrayAdapter<String> adapter;

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

        mBtnPradeti = (Button)findViewById(R.id.žaistiBtn);
        mBtnPradeti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent pradetiZaidima = new Intent(v.getContext(), ZaidimasActivity.class);
                startActivity(pradetiZaidima);
            }
        });



        mBtnPrideti = (Button)findViewById(R.id.pridėtiBtn);
        mBtnPrideti.setOnClickListener(this);
        mYrasytiVarda = (EditText)findViewById(R.id.VardoYrasymoBtn);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);

        // set the mZaidejai variable to your list in the xml

        mZaidejai=(ListView)findViewById(R.id.sarašas);
        mZaidejai.setAdapter(adapter);






    @Override
    public void onClick(View v) {
        String input = mYrasytiVarda.getText().toString();
        if(input.length() > 0)
        {
            // add string to the adapter, not the listview
            adapter.add(input);

            // no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
        }else{
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Klaida:");
            alertDialog.setMessage("Blogai yrašytas vardas");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
// here you can add functions
                }
            });

            alertDialog.show();
        }
    }

Here i want to show the added inputs, as single values.

public class ZaidimasActivity extends ZaidejaiActivity {

    ArrayList<String> listas = getIntent().getStringArrayListExtra("list");

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



    }

How to get the context from the adapter in my last activity and how to show them?

user3364333
  • 5
  • 1
  • 5
  • I think this will help you: http://stackoverflow.com/questions/4848260/passing-data-from-list-view-to-another-activity – Christopher Jan 21 '15 at 14:46

1 Answers1

0

what you want to do is pass the list as a bundle to the new activity. Make a getter method for your adapter class to get the list.

 mBtnPradeti.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent pradetiZaidima = new Intent(v.getContext(), 
 ZaidimasActivity.class);
 pradetiZaidima.putExtra("playerList", adapter.getList());
            startActivity(pradetiZaidima);
        }
    });

Then in your launched activity

public class ZaidimasActivity extends ZaidejaiActivity {

ArrayList<String> listas;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zaidimas);
    listas.getIntent().getExtras().getStringArrayList("playerList");
}
Marcin D
  • 918
  • 7
  • 14