-2

How can i get the text set on the button inside the on-click() class? i need to get the button text for sq l select statement

 tableLayout.addView(tableRow);
    int a = 0;
    for (Integer j = 0; j < count; j++)
    {
        Button b = new Button(getApplicationContext());
        b.setText(c.getString(c.getColumnIndex("jour")));
         b.setId(a++);

        tableRow.addView(b);
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Integer fff = v.getId();
                Toast.makeText(getApplicationContext(), fff.toString(), Toast.LENGTH_SHORT).show();


                Log.d("TAG", "The index is");
            }
        });
        c.moveToNext() ;


enter code here
keshav
  • 13
  • 1

5 Answers5

2

You can type caste the view to button and use it to getText().

tableLayout.addView(tableRow);
int a = 0;
for (Integer j = 0; j < count; j++)
{
    Button b = new Button(getApplicationContext());
    b.setText(c.getString(c.getColumnIndex("jour")));
     b.setId(a++);

    tableRow.addView(b);
    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Integer fff = v.getId();
            Toast.makeText(getApplicationContext(), fff.toString(),                
           Toast.LENGTH_SHORT).show();

          Button b = (Button)v;
           String buttonText = b.getText().toString();
            Log.d("TAG", "The text is " + buttonText);
        }
    });
    c.moveToNext() ;
Soham Dutta
  • 129
  • 2
  • 9
1

You are adding the Button to table row tableRow.addView(b);. I at first look at the code din't see it. Missed it.

So Make it final

 final Button b = new Button(getApplicationContext());
 // Use ActivtiyContext
 final Button b = new Button(ActivityName.this);
 // posted a link at the end read it.

An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.

http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html#accessing

Inside onClick

 public void onClick(View v) {

        String value = b.getText().toString()
        }

Also check

When to call activity context OR application context?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

As per my way create String Array and :

String Title = new String[count];

And now implement like this:

 for (int j = 0; j < count; j++)
{
    Button b = new Button(getApplicationContext());
    b.setText(c.getString(c.getColumnIndex("jour")));
    b.setId(j);

    Title[a] = c.getString(c.getColumnIndex("jour");

    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v1) {

            Toast.makeText(getApplicationContext(), "Button click on(): "+Title[v1.getId()].toString(), Toast.LENGTH_SHORT).show();

            Log.d("TAG", "The index is: "+v1.getId());
        }
    });
    tableRow.addView(b);
    c.moveToNext() ;
  }
M D
  • 47,665
  • 9
  • 93
  • 114
0
  • First you give onclick event for Button like (buttonClick).

    final Button testButton = new Button(getApplicationContext());
    
    void buttonClick(View v){
     Log.v("text", testButton.getText().toString()); // get the text
     testButton.setText("sometext");  //to change the text
    }
    
0

Try this code:

    public void onClick(View v) {

    String value = (Button)v.getText().toString()
    }
Hideme Sri
  • 193
  • 9