how can i get value from view without use ids?



I have 7 outcome view in my XML file and I want to control them (get value form them) without declare on each view separately and I want to do a loop (for loop) that over on each view and store the value in array, so anyone can help me with a solution for that problem that I have? Thanks in advance for your help. here is my XML code :



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<com.example.loto.ViewLoto
android:id="@+id/viewLoto1"
android:layout_width="102dp"
android:layout_weight="1"
android:layout_height="wrap_content" >
</com.example.loto.ViewLoto>

<com.example.loto.ViewLoto
android:id="@+id/viewLoto2"
android:layout_width="102dp"
android:layout_weight="1"
android:layout_height="wrap_content" >
</com.example.loto.ViewLoto>

<com.example.loto.ViewLoto
android:id="@+id/viewLoto3"
android:layout_width="102dp"
android:layout_weight="1"
android:layout_height="wrap_content" >
</com.example.loto.ViewLoto>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<com.example.loto.ViewLoto
android:id="@+id/viewLoto4"
android:layout_width="102dp"
android:layout_weight="1"
android:layout_height="wrap_content" >
</com.example.loto.ViewLoto>

<com.example.loto.ViewLoto
android:id="@+id/viewLoto5"
android:layout_width="102dp"
android:layout_weight="1"
android:layout_height="wrap_content" >
</com.example.loto.ViewLoto>

<com.example.loto.ViewLoto
android:id="@+id/viewLoto6"
android:layout_width="102dp"
android:layout_weight="1"
android:layout_height="wrap_content" >
</com.example.loto.ViewLoto>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<com.example.loto.ViewLoto
android:id="@+id/viewLoto7"
android:layout_width="102dp"
android:layout_height="wrap_content" >

</com.example.loto.ViewLoto>

<TextView
android:id="@+id/txtResult"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="24sp"
android:text="The result will be here!" />

</LinearLayout>

<Button
android:id="@+id/btnDraw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Draw numbers" />

</LinearLayout>


and java code:



package com.example.loto;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;


public class ActivityLoto extends Activity
{
class Layout
{
public Layout()
{
txtResult = (TextView)findViewById(R.id.txtResult);
btnDraw = (Button)findViewById(R.id.btnDraw);
}

TextView txtResult;
Button btnDraw;
}

int[] views = new int[7];

class Events
{
public Events()
{
l.btnDraw.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
int[] numbers = new int[7];
for(int i=0;i<numbers.length;i++)//I stuck here
{
int curent =i+1;
ViewLoto viewLoto = (ViewLoto)findViewById(R.id.viewLoto+curent);
numbers[i] = viewLoto.getValue();
}
}
});
}
}

Layout l;
Events e;

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

l = new Layout();
e = new Events();
}

}

No comments:

Post a Comment