Search code examples
androidandroid-intentarraylistcharsequence

Problems with putExtra and putStringArrayList


I'm trying to send some data from one activity to another and it's sorta working but not like I want to work. Problem 1-Things are getting mixed up. On the Next Activity part of the listitem is going to an incorrect textView and part to the correct textview. Problem 2- I am only able to list 1 item on the new activity but I want to be able to send multiple listitems. I think the problem lies in combining different types of putExtra request to the same place like I do here.

.putExtra("inputPrice",(CharSequence)pick) .putStringArrayListExtra("list", listItems)

Ant help would be appreciated.

Sending Data to next Activity

final TextView username =(TextView)findViewById(R.id.resultTextView);
String uname = username.getText().toString();

final TextView uplane =(TextView)findViewById(R.id.inputPrice);                     
String pick = uplane.getText().toString();

final TextView daplane =(TextView)findViewById(R.id.date);                  
String watch = daplane.getText().toString();

 startActivity(new Intent(MenuView1Activity.this,RecordCheckActivity.class)
.putExtra("date",(CharSequence)watch)
.putExtra("Card Number",(CharSequence)uname)
.putExtra("inputPrice",(CharSequence)pick)
.putStringArrayListExtra("list", listItems)
);
finish();

This is the Next Activity

        Intent is = getIntent();
    if (is.getCharSequenceExtra("Card Number") != null) {
        final TextView setmsg = (TextView)findViewById(R.id.saleRccn);
        setmsg.setText(is.getCharSequenceExtra("Card Number"));             
    }
        Intent it = getIntent();
    if (it.getCharSequenceExtra("date") != null) {
        final TextView setmsg = (TextView)findViewById(R.id.saleTime);
        setmsg.setText(it.getCharSequenceExtra("date"));                
    }
    Intent id1 = getIntent();
    if (id1.getCharSequenceExtra("inputPrice") != null) {
        final TextView setmsg = (TextView)findViewById(R.id.saleName);
        setmsg.setText(id1.getCharSequenceExtra("inputPrice"));
    }
    ArrayList<String> al= new ArrayList<String>();
    al = getIntent().getExtras().getStringArrayList("list");
    saleNotes= (TextView) findViewById(R.id.saleNotes); 
    saleNotes.setText(al.get(0));

Solution

  • Alright, a few things:

    First of all you do not need to cast your strings as CharSequence.

    Second thing,

    Define intent, add your extras and only then call startActivity as below:

    Intent intent = new Intent(MenuView1Activity.this,RecordCheckActivity.class);
    intent.putExtra("date", watch);
    startActivity(intent);
    

    Third, when retrieving the intent create a bundle first as below:

    Bundle extras = getIntent().getExtras();
    String date = extras.getString("date");
    

    EDIT:

    Here is how you convert your entire array list to one single string and add it to your textview.

    String listString = "";
    
    for (String s : al)
    {
        listString += s + "\t"; // use " " for space, "\n" for new line instead of "\t"
    }
    
    System.out.println(listString);
    saleNotes.setText(listString);
    

    Hope this helps!