简体   繁体   中英

handle multiple buttons in android

In a table layout i have few text views and buttons for each row. Now when i click a particular button only that text view value related to that particular button must be passed to next intent.How can i achieve this ? Please ask if u need any further info.

TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);
        for(i=0;i<count;i++)
        {



                // Create a TableRow and give it an ID
                TableRow tr = new TableRow(this);


                // Create a TextView to house the name of the province
                TextView labelTV = new TextView(this);

                labelTV.setText(smsListDate.get(i));
                labelTV.setTextColor(Color.WHITE);


                // Create a TextView to house the value of the after-tax income
                TextView valueTV = new TextView(this);

                valueTV.setText(smsListMessage.get(i));
                tr.addView(valueTV);
                TextView valueTV2 = new TextView(this);
               valueTV.setTextColor(Color.WHITE);


               Button submit = new Button(this);
               submit.setText("respond");

               tr.addView(submit);

                // Add the TableRow to the TableLayout
                tl.addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

        }

Thanks

I guess there was some gap in understanding. I do have written the submit action as

submit.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                 Intent myIntent = new Intent(getApplicationContext(),
                 SmsActivity.class);

                 startActivity(myIntent);

            }});

but my intention now is to carry the values of text views in particular row to the next intent on clicking that particular button.

You will need a button listener for your submit button and in its onClick method you will need to put the value in the bundle which can get passed to the next intent.

submit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //create an intent
                Intent intent = new Intent(ClassYouAreIn.this, 
                                           ClassForNextIntent.class);

                //put the text view value into the bundle for the intent
                //the bundle is a Map and uses key/value pairs 
                //(it is like a dictionary)
                //in this case the key is myTextViewValue 
                //and the value is valueTV.getText()
                intent.putExtra("myTextViewValue", valueTV.getText());

                //now start your new activity with the intent you just made
                startActivity(intent);

           }
});

Now in your other class (the class that the new intent will go to) you will need to access the bundle and get the value you wanted to pass to this intent. For this example i'm going to get my bundle data in my onCreate method but you could really get the data from the bundle in any method you wanted.

@Override
public void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    setContentView(R.layout.your_view);

    //get the extra(s) you put in the bundle
    bundle = getIntent().getExtras(); 

    String valueFromTV = bundle.getString("myTextViewValue");

    //and then do whatever you want with it
}

Hope this helps you

You need to create a click listener for the button and in that, create the intent and put a String extra which you get from the valueTV text.

submit.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
        Intent i = new Intent();
    i.putExtra("value", valueTV.getText());
    startActivity(i);
}
});

Hmm... your question isn't totally clear to me, but I think what you need to do, for starters, is add an OnClickListener (I use an anonymous one) to "submit" and then create/start your intent within it's onClick(View) method. You can pass the value using Intent.putExtra(String, String). Something like this:

submit.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(ThisActivity.this, NextActivity.class);
        intent.putExtra("value", valueTV.getText())
        startActivity(intent);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM