Android studio将ListView传递给另一个活动

时间:2017-03-08 20:11:42

标签: java android android-studio

我正在尝试将数组列表传递给另一个活动,但这似乎还不够。我整天都在搜索数组列表" intent"但没有成功。我为学习目的编写了一个代码。如何传递数据并在第二个活动中显示Arraylist?

操作按钮是btn_save。如果您想了解更多详情,请与我们联系。

代码在

MainActivity(第一项活动):

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第二个活动:(几乎为空)

public class ListOfNames extends Activity {


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

    }
}

2 个答案:

答案 0 :(得分:0)

如果你想传递一个字符串的arraylist,那么这将很容易。只需通过这样的意图传递arraylist:

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

并在ActivityTwo中接收它:

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

答案 1 :(得分:0)

我找到了一个临时解决方案(该应用程序没有破产)。唯一的问题是arraylist没有增加。它包含并仅显示最后一个值。

主要活动:

...

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(第二个活动):

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);
        }
    }
}