2

I am trying to make an app in which I want to pass a value from an editText(MainActivity.java) to a listView(bestFitActivity.java) which is placed in another activity. The issue with my code is that it passes only one value to the listView. When I enter a new value it just replaces the previous value.

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

        /*worstFitDirector.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent2 = new Intent (MainActivity.this , worstFitActivity.class);
                startActivity(myIntent2);
            }
        });

        firstFitDirector.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent3 = new Intent (MainActivity.this , firstFitActivity.class);
                startActivity(myIntent3);
            }

        });*/
    }
}

bestFitActivity.java

public class bestFitActivity extends AppCompatActivity {
    String chunkSIZE;
    ArrayList<String> chunkSizeArray = new ArrayList<>();


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

        ListView blockSize = (ListView)findViewById(R.id.blockSize);
        ListView finalProcessList  = (ListView)findViewById(R.id.finalprocesslist);

        chunkSIZE = getIntent().getExtras().getString("chunkSizeArray");
        Log.d("Manager" , " "+chunkSIZE);

        chunkSizeArray.add(chunkSIZE);
        Log.d("Manager" , ""+chunkSIZE);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(bestFitActivity.this , android.R.layout.simple_expandable_list_item_1 , chunkSizeArray);
        blockSize.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        //((EditText)findViewById(R.id.chunkSize)).setText(" ");
        //EditText tv = (EditText) findviewbyID(R.id.)
    }
}

PS: This code is not complete

2
  • Do code formatting in proper manner Mar 10, 2019 at 5:51
  • You're sending only one input via your getChunkSize variable. You've to store all entered values into chunkSizeArray, and send that variable via intent. Look at this link of SO for sending ArrayLists to another activity: stackoverflow.com/questions/4030115/… Mar 10, 2019 at 5:58

2 Answers 2

3

Each time you click the button a new activity, with a new empty chunkSize ArrayList is, created with just the single value.

You could, each time the button is clicked, add to the chunkSizeArray in the MainActivity and then send a counter and the values of the array in individual intentExtras.

And then retrieve and build the ArrayList from all the intent extras in the bestFitActivity.

This is how you could do it

public class MainActivity extends AppCompatActivity {

    public static final String INTENTKEY_ARRAYELEMENTS = "arrayelemets";
    public static final String INTENYKEY_ELEMENTX = "element_";
    private static ArrayList<String> chunkSizeArray = new ArrayList<>();
    Button bestFitDirector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bestFitDirector = (Button)findViewById(R.id.bestFitdirector);

        bestFitDirector.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chunkSizeArray.add("Test" + String.valueOf(chunkSizeArray.size() + 1));
                Intent myitent = new Intent(MainActivity.this,bestFitActivity.class);
                myitent.putExtra(INTENTKEY_ARRAYELEMENTS, chunkSizeArray.size());
                for (int i=0; i < chunkSizeArray.size(); i++) {
                    myitent.putExtra(INTENYKEY_ELEMENTX + String.valueOf(i),chunkSizeArray.get(i));
                }
                startActivity(myitent);
            }
        });
    }
}

and

public class bestFitActivity extends AppCompatActivity {

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

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

        Intent mypassedIntent = getIntent();

        int elements = mypassedIntent.getIntExtra(MainActivity.INTENTKEY_ARRAYELEMENTS,0);
        for (int i = 0; i < elements; i++) {
            chunkSizeArray.add(mypassedIntent.getStringExtra(MainActivity.INTENYKEY_ELEMENTX + String.valueOf(i)));
        }
        Toast.makeText(this,"The number of strings in the chukSizeArray is " + String.valueOf(chunkSizeArray.size()),Toast.LENGTH_SHORT).show();
        for (String s: chunkSizeArray) {
            Log.d("chunkSizeArray Value",s);
        }
    }
}

After clicking 5 times (click back to return to the MainActivity) the log is

2019-03-10 17:19:51.687 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1

2019-03-10 17:19:56.362 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:19:56.362 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test2

2019-03-10 17:19:59.877 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:19:59.877 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test2
2019-03-10 17:19:59.877 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test3

2019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test2
2019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test3
2019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test4

2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test2
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test3
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test4
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test5

gaps added to split the clicks up

A simpler way is to use the putStringArrayListExtra along with the getStringArrayListExtra methods, which basically does the loops on your behalf.

So with main activity having

 public static final String INTENTKEY_ARRAY = "bestfitarray";

In the onClick listener you could have :-

myitent.putStringArrayListExtra(INTENTKEY_ARRAY,chunkSizeArray);

Along with

myitent.putStringArrayListExtra(INTENTKEY_ARRAY,chunkSizeArray);

in the bestFitActivity.

1

There is a simple hack you can do

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

to static like private static ArrayList<String> chunkSizeArray = new ArrayList<>();

1
  • nice to hear it happy coding :D Mar 11, 2019 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.