Reputation: 17
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://schemas.android.com/apk/res/android"
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();
}
}
Upvotes: 0
Views: 63
Reputation: 44158
You can loop through specific View's children like this:
List<Integer> lotoValues = new ArrayList<Integer>();
ViewGroup layout = (ViewGroup) findViewById(R.id.myLayout);
for(int i=0; i<layout.getChildCount(); ++i) {
ViewLoto nextChild = (ViewLoto) layout.getChildAt(i);
lotoValues.add((Integer)nextChild.getText());
}
EDIT:
The above code will loop through a layout's children and their getText
results to an array lotoValues. Note that your custom views should have a method getText to return the text of the view.
<LinearLayout
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.example.loto.ViewLoto
android:layout_width="102dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<com.example.loto.ViewLoto
android:layout_width="102dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<com.example.loto.ViewLoto
android:layout_width="102dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
Upvotes: 1
Reputation: 341
solution 1:
if your goal is to avoid from create listener for each view, just implement OnClickListener at your class, and on the listener cheack which view is pressed (in that method you need to declare id for each
solution 2:
create your views dynamically and add them to your layout:
View views = new View[7];
for(int i=0;i<views.length;i++){
view[i] = new ViewLoto(); // your custom view
}
and add each view to your layout by call yourLayout.addView():
LinearLayout yourLayout = (LinearLayout)findViewById(R.id.layout_id);
for(int i=0;i<views.length;i++){
// here you can set attribute yo your view. for example onClickListener
// view[i].setOnClickListener(this);
yourLayout.addView(views[i]);
}
Upvotes: 0