Note that there are some explanatory texts on larger screens.

plurals
  1. POAdvice with for loop
    primarykey
    data
    text
    <p>so I've been making mathematical programs for Android for a while, and I've come across a problem where i need to print a range of numbers using a for loop, I'm not sure how to enable my program to print the variables the way I'd like, I've tried TextView, but it only shows the first variable, do I need to use ListView? and how do set the text into it? here's my xml and code files that I've tried so far:</p> <p>class:</p> <pre><code>public class Screen extends ListActivity { public EditText txtbound1; private double a = 0; private double b1 = 0; private double c = 0; //LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS ArrayList&lt;String&gt; listItems=new ArrayList&lt;String&gt;(); //DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW ArrayAdapter&lt;String&gt; adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.screen1); Bundle b = getIntent().getExtras(); double bound = b.getDouble("name"); adapter=new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, listItems); setListAdapter(adapter); //METHOD WHICH WILL HANDLE DYNAMIC INSERTION //public void addItems(View v) { // listItems.add("Clicked : "+clickCounter++); // adapter.notifyDataSetChanged(); for(double i = bound;i&gt;0;i--) { a = bound; b1 = a * 2; c = a*3; bound--; } } public void addItems(double a, double b1, double c) { // or you can parse it before and have string parameters listItems.add(Double.toString(a) + " " + Double.toString(b1) + " " + Double.toString(c)); adapter.notifyDataSetChanged(); } } </code></pre> <p>and layout is:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;Button android:id="@+id/addBtn" android:text="Add New Item" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="addItems"/&gt; &lt;ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>so lets say the user inputs 5 as bound, I'd like to to be displayed as such:</p> <ul> <li>A B C</li> <li>5 10 15</li> <li>4 8 12</li> <li>3 6 9</li> <li>2 4 6</li> <li>1 2 3</li> </ul> <p>however it only displays the 5 10 and 15 line, how do I edit the for loop so it works? Do i need to set a, b1 and c as an array ? or will ListView work? I've never used ListView before so if ListView is what I need please show me how to initialise it in the xml layout and how to print the variables to it as I've done previous, Thanks </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload