0

I am unsure of what the following line of code does?

int convertedNumber = getIntent().getIntExtra("convertedNumber", 0);

I think it is getting the int converted Number from another activity? But then how is it possible to use this number in a list view etc in the current activity?

Current Class:

public class DisplayTimesTable extends Activity {
    // set up vars
    TextView trace;
    ListView listView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // setting equal to text layout View
        setContentView(R.layout.display);

        //initialise vars
        initialiseVars();

        // This declares and int variable, assigns it an int value from the calling Intent if its not there it is defaulted to 0
        int convertedNumber = getIntent().getIntExtra("convertedNumber", 0);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

        int result = 0;
        // loop to give list view
        for (int i = 1; i <= convertedNumber; ++i)

            adapter.add(convertedNumber + "x" + i + "=" + result);

        listView.setAdapter(adapter);

    }

    /**
     * method to initialise all of the buttons, textviews etc used to clean up
     * the onCreate.
     */
    private void initialiseVars() {
        // Setting up (initialising) all the buttons text views etc from the xml
        trace = (TextView) findViewById(R.id.tvTrace);
        listView = (ListView) findViewById(R.id.lvTables);

    }

}

4 Answers4

0
int convertedNumber // this is declaring an int variable

= // this is an assignment operator

getIntent() // this gets the calling Activity which is the Activity that started this Intent

getIntExtra("convertedNumber",0) // this is getting an integer from the calling activity, if it has no value it is set to 0 the default

So

// This declares and int variable, assigns it an int value from the calling Intent if its not there it is defaulted to 0

int convertedNumber = getIntent().getIntExtra("convertedNumber",0);
kandroidj
  • 13,784
  • 5
  • 64
  • 76
0

When you open an Acitivty you can pass some data to it (called extras) (could be an int, float or an Object), with this line you read the extra named "convertedNumber" if present or 0 if nothing is found with this name. (Remember when you launch the activity you should use the name name!).. the result will be inserted in the variable.

After all you will have an integer, could means everything and you can use it everywhere... in a ListView, edittext, TextView etc.

Intent: http://developer.android.com/reference/android/content/Intent.html


Example as you said

int value = getIntent().getIntExtra("convertedNumber", 0);

ListView listView = (ListView) findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

for (int i = 1; i <= value; ++i)
    adapter.add(value + "x" + i);

listView.setAdapter(adapter);

It will output a list with:

convertedNumberx1
convertedNumberx2
convertedNumberx3
convertedNumberx4
convertedNumberxconvertedNumber
Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
  • Thanks, However I am unsure of how I would use this in a list View? –  Apr 03 '14 at 12:47
  • It depends, I mean you could create a simple ArrayAdapter and add this value... But are you sure you want to use a ListView? Its used to show a list not a single value... maybe a TextView? – Marco Acierno Apr 03 '14 at 12:50
  • yeah well I want to create a list of times tables stemming from that value. i.e. if it is 6 then 6x1 6x2 etc.. will be displayed in the next activity, what is the best way to do this? –  Apr 03 '14 at 14:21
  • @RNI2013 See my example. – Marco Acierno Apr 03 '14 at 14:40
  • Thank you that is great, can you please check my edit, I am attempting to get the answer to be outputted as well however I am getting an error with "i". I think it is just something simple –  Apr 03 '14 at 15:05
  • Maybe because you already used "i" as name. The code works fine but it's just an example you should change it. Anyway everything is ok now? You understand what the line does and how it could work with a listview? – Marco Acierno Apr 03 '14 at 16:01
0

You are correct it does get an int with key "convertedNumber" from another activity. If the key "convertedNumber" is not found 0 is assigned to the variable convertedNumber.

As for the other part of your question,have a look at using listView in the documentation.

epocolis
  • 701
  • 1
  • 8
  • 12
0

It will contain value passed in getIntextra.

Intent intent = new Intent(myActivity.this, newClass.class); 
intent.putExtra("selectedItem", info.id); 
this.startActivity(intent); 

and target activity will contain

long iSelectedItem = intent.getIntExtra("selectedItem", -1);

and value of iSelectedItem set to what is passed in info.id;