将项目添加到另一个活动中的列表视图(仅添加 1 个条目)

我正在尝试制作一个应用程序,我想在其中将值从一个传递editText(MainActivity.java)到listView(bestFitActivity.java)另一个活动中。我的代码的问题是它只将一个值传递给listView. 当我输入一个新值时,它只会替换以前的值。


MainActivity.java


public class MainActivity extends AppCompatActivity {

    int i = 0;

    String getChunkSize;

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

    EditText chunkSize;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        Button chunkSave = (Button)findViewById(R.id.chunkSave);

        Button processSave = (Button)findViewById(R.id.processSave);

        Button bestFitDirector = (Button)findViewById(R.id.bestFitdirector);

        Button worstFitDirector = (Button)findViewById(R.id.worstFitdirector);

        Button firstFitDirector = (Button)findViewById(R.id.firstFitDirector);


        EditText processSize = (EditText)findViewById(R.id.processSize);

        chunkSize = (EditText)findViewById(R.id.chunkSize);


        chunkSave.setOnClickListener(new View.OnClickListener() {


            @Override

            public void onClick(View v) {


                i++;

                Log.d("Manager" , "%d" +i);



                getChunkSize = chunkSize.getText().toString();

                //Log.d("Manager" , "" +getChunkSize);



                if(getChunkSize == null ) {

                    Toast.makeText(getBaseContext(), "Empty Input", Toast.LENGTH_SHORT).show();

                }

            }

        });


        bestFitDirector.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Intent myIntent =  new Intent(MainActivity.this , bestFitActivity.class);

                myIntent.putExtra("chunkSizeArray" , getChunkSize );

                startActivity(myIntent);

            }

        });


}

PS:这段代码不完整


波斯汪
浏览 92回答 2
2回答

蓝山帝景

每次单击按钮时,都会创建一个带有新空 chunkSize ArrayList 的新活动,仅使用单个值创建。您可以在每次单击按钮时添加到 MainActivity 中的chunkSizeArray,然后在各个 intentExtras 中发送一个计数器和数组的值。然后从 bestFitActivity 中的所有 Intent Extra 中检索并构建 ArrayList。这就是你可以做到的public class MainActivity extends AppCompatActivity {&nbsp; &nbsp; public static final String INTENTKEY_ARRAYELEMENTS = "arrayelemets";&nbsp; &nbsp; public static final String INTENYKEY_ELEMENTX = "element_";&nbsp; &nbsp; private static ArrayList<String> chunkSizeArray = new ArrayList<>();&nbsp; &nbsp; Button bestFitDirector;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; bestFitDirector = (Button)findViewById(R.id.bestFitdirector);&nbsp; &nbsp; &nbsp; &nbsp; bestFitDirector.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunkSizeArray.add("Test" + String.valueOf(chunkSizeArray.size() + 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent myitent = new Intent(MainActivity.this,bestFitActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myitent.putExtra(INTENTKEY_ARRAYELEMENTS, chunkSizeArray.size());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i < chunkSizeArray.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myitent.putExtra(INTENYKEY_ELEMENTX + String.valueOf(i),chunkSizeArray.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(myitent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}和public class bestFitActivity extends AppCompatActivity {&nbsp; &nbsp; ArrayList<String> chunkSizeArray = new ArrayList<>();&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_best_fit);&nbsp; &nbsp; &nbsp; &nbsp; Intent mypassedIntent = getIntent();&nbsp; &nbsp; &nbsp; &nbsp; int elements = mypassedIntent.getIntExtra(MainActivity.INTENTKEY_ARRAYELEMENTS,0);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < elements; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chunkSizeArray.add(mypassedIntent.getStringExtra(MainActivity.INTENYKEY_ELEMENTX + String.valueOf(i)));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this,"The number of strings in the chukSizeArray is " + String.valueOf(chunkSizeArray.size()),Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; for (String s: chunkSizeArray) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("chunkSizeArray Value",s);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}点击 5 次后(点击返回返回 MainActivity)日志为2019-03-10 17:19:51.687 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test12019-03-10 17:19:56.362 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test12019-03-10 17:19:56.362 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test22019-03-10 17:19:59.877 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test12019-03-10 17:19:59.877 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test22019-03-10 17:19:59.877 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test32019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test12019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test22019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test32019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test42019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test12019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test22019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test32019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test42019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test5添加间隙以将点击分开一种更简单的方法是使用putStringArrayListExtra和getStringArrayListExtra方法,它们基本上代表您执行循环。所以主要活动有&nbsp;public static final String INTENTKEY_ARRAY = "bestfitarray";在 onClick 侦听器中,您可以:-myitent.putStringArrayListExtra(INTENTKEY_ARRAY,chunkSizeArray);随着myitent.putStringArrayListExtra(INTENTKEY_ARRAY,chunkSizeArray);在 bestFitActivity 中。

MYYA

你可以做一个简单的黑客攻击改变ArrayList<String> chunkSizeArray = new ArrayList<>();静态喜欢private static ArrayList<String> chunkSizeArray = new ArrayList<>();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java