简体   繁体   中英

Android studio pass ListView to another activity

I am trying to pass an array list to another activity but it seems that is not enough. I searched all day to pass the array list with "intent" but with no success. I wrote a code for learning purposes. How to pass the data and show the Arraylist in a second activity?

The action button is btn_save. If you want further details let me know.

The code is in

MainActivity (first activity):

public class MainActivity extends AppCompatActivity {

    ArrayList<String> addArray = new ArrayList<>();


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

        final EditText editTextOne = (EditText) findViewById(R.id.editTextOne);
        final EditText editTextTwo = (EditText) findViewById(R.id.editTextTwo);
        Button btn_showText = (Button) findViewById(R.id.buttonShow);
        final TextView textView = (TextView) findViewById(R.id.textResults);
        Button btn_refresh = (Button) findViewById(R.id.btn_refresh);
        Button btn_close = (Button) findViewById(R.id.btn_close);
        Button btn_save = (Button) findViewById(R.id.btn_save);
        final ListView showMe = (ListView) findViewById(R.id.list_items);

        btn_showText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = editTextOne.getText().toString();
                String textTwo = editTextTwo.getText().toString();

                if (text.length() == 0 || textTwo.length() == 0){
                    textView.setText("You must enter Name & Surname");
                }else {
                    textView.setText(Html.fromHtml(

                            "<font color=\"red\">"+ text +  "</font> " +
                                    "<font color=\"blue\"><b>" + textTwo + " </b></font>"));
                }
            }
        });
        btn_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                startActivity(getIntent());
            }
        });
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                System.exit(0);
            }
        });

        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//              Context context = getApplicationContext();
//              CharSequence mytext = "Hahahaha";
//              int duration = Toast.LENGTH_SHORT;
//              Toast toast = Toast.makeText(context,mytext,duration);
//              toast.show();
                String text = editTextOne.getText().toString();
                String textTwo = editTextTwo.getText().toString();
                String getInput = text + textTwo;
                if (addArray.contains(getInput)){
                    Toast.makeText(getBaseContext(), "Item already Added!", Toast.LENGTH_LONG).show();
                }
                else if (getInput == null || getInput.trim().equals("")){
                    Toast.makeText(getBaseContext(), "Input field is empty", Toast.LENGTH_LONG).show();
                }
                else{
                    addArray.add(getInput);
                    ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);
                    showMe.setAdapter(adapter);
                    Intent intent = new Intent(MainActivity.this, ListOfNames.class);

                    ((EditText)findViewById(R.id.editTextOne)).setText(" ");
                    ((EditText)findViewById(R.id.editTextTwo)).setText(" ");
                }
            }
        });


    }

    public void onButtonClick(View v){

        if (v.getId() == R.id.btn_list){
            Intent i = new Intent(MainActivity.this, ListOfNames.class);
            startActivity(i);
        }

    }

}

ListOfNames second activity: (almost empty)

public class ListOfNames extends Activity {


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

    }
}

If you are trying to pass a arraylist of string then it will be easy. Just pass arraylist with intent like this:

ArrayList<String> list = new ArrayList<String>();  
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putStringArrayListExtra("key", list);
startActivity(intent);

And receive it in ActivityTwo like this:

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

I found a temporary solution for this (The app is not broke). The only problem is the arraylist didn't increased. It contains and show only the last value.

Main Activity:

...

else{
//  addArray.add(getInput);
//  ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);
//                    showMe.setAdapter(adapter);
    Intent intent = new Intent(MainActivity.this, ListOfNames.class);
    intent.putExtra("data", getInput);
    startActivity(intent);

//  ((EditText)findViewById(R.id.editTextOne)).setText(" ");
//  ((EditText)findViewById(R.id.editTextTwo)).setText(" ");
                }
...

ListOfNames (Second Activity):

public class ListOfNames extends Activity {
    ArrayList<String> addArrayT = new ArrayList<>();

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

        Bundle bundle = getIntent().getExtras();
        String data = bundle.getString("data");
        Button btn_more = (Button) findViewById(R.id.btn_more);

        ListView showMe = (ListView) findViewById(R.id.list_items);
        addArrayT.add(data);
        ArrayAdapter<String> adapterT = new ArrayAdapter<>(ListOfNames.this, android.R.layout.simple_list_item_1, addArrayT);
        showMe.setAdapter(adapterT);

    }
    public void onClickMore(View v){

        if (v.getId() == R.id.btn_more){
            Intent i = new Intent(ListOfNames.this, MainActivity.class);
            startActivity(i);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM