When i click on the images it doesnt change the display textview, where did i go wrong?
**List<ImageView> fields = new ArrayList<ImageView>();
for(int i = 0; i < 64; i++) {
try{
id = R.id.class.getField("square" + (i+1)).getInt(0);
views.add((ImageView)findViewById(id));
((View) fields).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View fields) {
for(int i=0; i < 64; i++)
{
display = (TextView) findViewById(R.id.textView1);
display.setText("square" + i);
}
}
});
}
catch(Exception e){}
}**
}
}
It looks like you're adding the onClick listener to the ArrayList of views, not the image you added. Change
((View) fields).setOnClickListener
to
findViewById(id).setOnClickListener
Related
I've created pragmatically 5 radio groups with 4 radio buttons each. How can i get the Id of the radio buttons? Very important, i dont't want to use setOnCheckedChangeListener. Here is my code.
radioGroup = new RadioGroup[5];
answer = new RadioButton[4];
int i = 0;
for (Question qn : questions) {
radioGroup[i] = new RadioGroup(this);
radioGroup[i].setId(i);
int j = 0;
for (Answer an : answers) {
if (qn.getID() == an.getQuestion_id_answer()) {
answer[j] = new RadioButton(this);
answer[j].setText(an.getAnswer());
answer[j].setId(j);
radioGroup[i].addView(answer[j]);
j++;
}
}
linearLayout.addView(radioGroup[i]);
answer[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int checkedRadioButtonId = v.getId();
RadioButton checkedRadioButton = (RadioButton) findViewById(checkedRadioButtonId);
if (checkedRadioButton.isChecked()) {
Toast.makeText(getApplicationContext(), "Checkbox" + checkedRadioButtonId + " checked", Toast.LENGTH_SHORT).show();
}
}
});
i++;
}
Thanks!
as in log :
ArrayIndexOutOfBoundsException: length=4; index=4
Because answer Array is size of j instead of i, so move RadioButton click listener inside for loop:
for (Answer an : answers) {
if (qn.getID() == an.getQuestion_id_answer()) {
answer[j] = new RadioButton(this);
...your code here...
answer[j].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your code here...
}
});
j++;
}
}
I have a simple app that updates a TextView from a values generated in a loop when i click on a button.
But when i run the app, it only shows the last number in the TextView. I added a Thread.sleep() to slow down the loop but it did not work, Still the same result. What am i doing wrong here? Thanks in advace
public class MainThread extends ActionBarActivity {
Button btnShow;
TextView txtSee;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_thread);
btnShow = (Button) findViewById(R.id.btnShow);
txtSee = (TextView) findViewById(R.id.txtSee);
btnShow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
for (int i = 0; i <= 5; i++) {
String text1 = "Iteration No " + i + " occured";
txtSee.setText(text1);
Thread.sleep(2000);
}
} catch (InterruptedException e) {
}
}
});
}
....
}
use
txtSee.append(text1);
instead of
txtSee.setText(text1);
your loop is executing too fast to let you observe the changes
Call another function inside loop and use
txtSee.setText(text1); inside that function.
or (int i = 0; i <= 5; i++) {
String text1 = "Iteration No " + i + " occured";
call_function(text1,txtSee);
}
call_function:-
call_function(String text1,TextView txtSee;){
txtSee.setText(text1);
Thread.sleep(2000);
}
I am having some problems when trying to trigger events on table layout in Android. These are the codes I used to populate the table layout dynamically:
private void BuildTable() {
try {
DatabaseAdapter mDbHelper = new DatabaseAdapter(Category.this);
mDbHelper.open();
CategoryController cc = new CategoryController(mDbHelper.open());
Cursor mCur = mDb.rawQuery(cc.getAllCat(), null);
if (mCur.getCount() != 0) {
if (mCur.moveToFirst()) {
// Setting table header
TableRow row = new TableRow(this);
row.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
do {
int cols = mCur.getColumnCount();
row = new TableRow(this);
for (int j = 1; j < cols; j++) {
// Dynamically load data fetch from database
// into table layout
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv.setPadding(5, 5, 10, 5);
tv.setGravity(Gravity.LEFT);
tv.setText(mCur.getString(j));
row.addView(tv);
row.setId(j);
}
row.setClickable(true);
row.setOnClickListener(tablerowOnClickListener);
table_layout.addView(row);
} while (mCur.moveToNext());
}
}
} catch (SQLException mSQLException) {
throw mSQLException;
}
}
And here is my table row onClickListener:
private OnClickListener tablerowOnClickListener = new OnClickListener() {
public void onClick(View v) {
//Highlight selected row
v.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
final int k = v.getId();
Toast.makeText(Category.this, Integer.toString(k),
Toast.LENGTH_SHORT).show();
}
};
My problem is when I highlighted certain row and I select another row again, the previous highlighted row remains the same highlighted background color. I wonder how to solve it?
Thanks in advance.
Provided table_layout is in the same scope as tablerowOnClickListener, change the Listener as follows:
private OnClickListener tablerowOnClickListener = new OnClickListener()
{
public void onClick(View v)
{
//Highlight selected row
for (int i = 0; i < table_layout.getChildCount(); i++)
{
View row = table_layout.getChildAt(i);
if (row == v)
{
row.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
}
else
{
//Change this to your normal background color.
row.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
}
//...
}
};
I created a ImageView object(img) and pass some resources through the same object(img) to a Linear Layout with a for-loop. On each Iteration of the loop I invoke a setOnClickListener() on img(img.setOnClickListener()) to show a Toast that reflects the value of the loop controller variable (i). The code segment i tried is below:
for (i = 1; i <= 6; i++)
{
img = new ImageView(this);
img.setImageResource(R.drawable.thambu);
body.addView(img);
this.img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(Details.this, Integer.toString(i) , Toast.LENGTH_LONG).show();
}
});
}
The thing is whenever i click on the Images that is generated, i have a Toast displaying 7.
I know why its displaying 7. but i want to display the index of the image that is being clicked.
(body is the id of a linear layout on which i pass an ImageView)
How can i do that on android. Thanks in advance.
for (i = 1; i <= 6; i++)
{
img = new ImageView(this);
img.setImageResource(R.drawable.thambu);
body.addView(img);
img.setTag(i);
this.img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int tagInt = (int) v.getTag();
Toast.makeText(Details.this, Integer.toString(tagInt) , Toast.LENGTH_LONG).show();
}
});
}
try this, i have used tags..
Actually, it's rather a general Java question.
You should do something like the following:
for (int i = 1; i <= 6; i++) {
img = new ImageView(this);
img.setImageResource(R.drawable.thambu);
body.addView(img);
final int j = i;
this.img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(Details.this, Integer.toString(j) , Toast.LENGTH_LONG).show();
}
});
}
I have created a dynamic array of buttons that come from sqllite database. For the life of me I cannot get a click listener set up to address each button individually. I have been searching for 3 hours with no success at all.
Any help would be appreciated. Don't worry about the database crap, it's just the listener.
imports...
public class CreatePlayList extends Activity {
ScrollView scrollleft, scrollright;
LinearLayout songsright, songsleft;
TextView testtext;
String[][] allsongs;
int numofsongs, i;
Button b1[];
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.createplaylist);
scrollleft = (ScrollView) findViewById(R.id.scrollleft);
scrollright = (ScrollView) findViewById(R.id.scrollright);
songsright = (LinearLayout) findViewById(R.id.layRight);
songsleft = (LinearLayout) findViewById(R.id.layLeft);
LyricDb connect = new LyricDb(CreatePlayList.this);
connect.open();
numofsongs = connect.getNumSongs();
b1 = new Button[(numofsongs)];
allsongs = new String[numofsongs][2];
allsongs = connect.getSongArray();
connect.close();
testtext = new TextView(this);
testtext.setText("Test");
songsleft.addView(testtext);
for (i = 0; i < allsongs.length; i++) {
b1[i] = new Button(this);
b1[i].setText(allsongs[i][1]);
songsright.addView(b1[i]);
}
b1[19].setText("test 123");
createClic();
}
public void createClic(){
for (i = 0; i < (allsongs.length - 1); i++) {
b1[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
testtext.setText(b1[i].getText());
}
});
}
}
}
Dump the 'i' declared at class level. Replace method with this (the "Button theButton" needs to stay 'final').
public void createClic()
{
for (int i = 0; i < (allsongs.length - 1); i++)
{
final Button theButton = b1[i];
theButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// TODO Auto-generated method stub
testtext.setText(theButton.getText());
}
});
}
}
First implement OnClickListener in your Activity and call setOnClickListener() method to all of your buttons when you are creating it.
b[i].setOnClickListener(this);
and also set id with each button
b[i].setId(i);
//and override this method
#Override
public void onClick(View v)
{
//here check which button called
if(v.getId==1)
{
//its Button 1 do what ever u want
}
if(v.getId==2)
{
//its Button 2 do what ever u want its Button 2
}
......
}
Try it once...
b1[i] = new Button[allsongs.length];
for (i = 0; i < allsongs.length; i++) {
b1[i].setText(allsongs[i][1]);
songsright.addView(b1[i]);
}