0

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:

class:

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<String> listItems=new ArrayList<String>();

//DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> 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<String>(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>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();

         }
     }

and layout is:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/addBtn"
android:text="Add New Item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItems"/>

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>

so lets say the user inputs 5 as bound, I'd like to to be displayed as such:

  • A B C
  • 5 10 15
  • 4 8 12
  • 3 6 9
  • 2 4 6
  • 1 2 3

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

David
  • 39
  • 1
  • 5

2 Answers2

1
  1. Your loop has a flaw in its logic. Setting a = bound means that every time you enter the loop a will be 5, since bound is never modified. It looks like you want to set a = i (or just use i for the calculations, perhaps).

  2. TextView.setText() replaces the current text of the view with the new text, overwriting anything that was previously displayed. You could instead use TextView.append() or build the whole string beforehand (e.g. with a StringBuilder or such) and then call TextView.setText() one time after the loop has finished.

eldarerathis
  • 35,455
  • 10
  • 90
  • 93
0

Yes, it makes more sense if you do use a Listview. Checkout this post

Where on the suggested "addItems" method, a simple concatenation will suffice:

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();
}
Community
  • 1
  • 1
EdGs
  • 356
  • 6
  • 18