Pergunta

I have 10 buttons and i want to set OnClickListener for all that buttons. Also with clicking on any button app will go another activity. I only post the buttons' definitions on the activity class. My code;

Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
Button button8 = (Button) findViewById(R.id.button8);
Button button9 = (Button) findViewById(R.id.button9);
Button button10 = (Button) findViewById(R.id.button10);
Button buttons[] = {button1, button2, button3, button4, button5, button6, button7, button8, button9, button10};
final String urlOfButtons[] = {"","","","","","","","","","",""};
final String titles[] = {"","","","","","","","","","",""};

JsonNode itemNode = jsonNode.path("Docs");
for(int i=0 ; i<itemNode.size() ; i++){
    titles[i] = itemNode.get(i).path("Text").asText();
            title = titles[i];
    buttons[i].setText(title);
    urlOfButtons[i] = itemNode.get(i).path("Link").asText();
            url = urlOfButtons[i];
    buttons[i].setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(HouseDetail.this, HouseDetailPdf.class);
            intent.putExtra("title", title);
            intent.putExtra("url", url);
            startActivity(intent);
        }
    });
}

This only takes the last values of title and url normally. String url, title is defined on top of class. Every button i click the same value goes to other activity. I want 10 different values to add extra. So i want to add instead of title and url; titles[i] and url[i] as extra. I hope i'm clear.

Foi útil?

Solução

You can set title and url as tag to each button

buttons[i].setTag(1, title);
buttons[i].setTag(2, url);

and rerieve the tag inside onClick

@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(HouseDetail.this, HouseDetailPdf.class);
            intent.putExtra("title", v.getTag(1));
            intent.putExtra("url", v.getTag(2));
            startActivity(intent);
        }

Or else you can do something like this.Store the index as the tag so that you can get the index when the button is clicked.

buttons[i].setTag(i);

Get the index which is stored as tag.

@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             int position = Integer.parseInt(v.getTag().toString());
            Intent intent = new Intent(HouseDetail.this, HouseDetailPdf.class);
            intent.putExtra("title", titles[position]);
            intent.putExtra("url", urlOfButtons[position]);
            startActivity(intent);
        }

Outras dicas

Try this..

You have passed the title and url values to the next Activity. But, at the end of for loop the title and url will only have the last value of the for loop. So, irrespective of the button click only the last stored values in title and url will only go to the next activity.

You can make use of the setTag(int key, Object tag) and set the url and title values for the button as tags inside for loop and you can retrieve them inside button click using getTag(int key) method..

JsonNode itemNode = jsonNode.path("Docs");
for(int i=0 ; i<itemNode.size() ; i++){
    titles[i] = itemNode.get(i).path("Text").asText();
            title = titles[i];
    buttons[i].setText(title);

    urlOfButtons[i] = itemNode.get(i).path("Link").asText();
            url = urlOfButtons[i];

    buttons[i].setTag(1,title);
    buttons[i].setTag(2,url);

    buttons[i].setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(HouseDetail.this, HouseDetailPdf.class);
            intent.putExtra("title", v.getTag(1));
            intent.putExtra("url", v.getTag(2));
            startActivity(intent);
        }
    });
}

setTag(int key, Object tag);

getTag(int key);

You can grab the relevant title and url in each iteration of the loop and store them in something final, then you can use them in the onClickListener.

for(int i=0 ; i<itemNode.size() ; i++){
    final String title = itemNode.get(i).path("Text").asText();
    final String url = itemNode.get(i).path("Link").asText();
    buttons[i].setOnClickListener(/* same as you had before */);
}

(warning: untested code).

Obviously if you're using the titles and urls arrays somewhere else, you should assign the values there too.

Your class should implement onclickListner like this:

public class HomeScreen extends Activity implements OnClickListener

Initialize your buttons:

Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
Button button4 = (Button) findViewById(R.id.button4);
butto4.setOnClickListener(this);

Handle the click item for all buttons:

public void onClick(View v) {
        switch (v.getId()) {        
        case R.id.button1:
            Intent intent = new Intent(HouseDetail.this, HouseDetailPdf.class);
            intent.putExtra("title", title);
            intent.putExtra("url", url);
startActivity(intent);
            break;
        case R.id.button2:
                Intent intent = new Intent(HouseDetail.this, HouseDetailPdf.class);
            intent.putExtra("title", title);
            intent.putExtra("url", url);
startActivity(intent);
            break;
        case R.id.button3:
                Intent intent = new Intent(HouseDetail.this, HouseDetailPdf.class);
            intent.putExtra("title", title);
            intent.putExtra("url", url);
startActivity(intent);
            break;
        case R.id.button4:
                Intent intent = new Intent(HouseDetail.this, HouseDetailPdf.class);
            intent.putExtra("title", title);
            intent.putExtra("url", url);
startActivity(intent);
            break;
        }

A good way to make it is avoiding this loop. Just define the url and the title in xml file.

you could put this in your button definition:

android:tag="title|url"

and then, in the onClickListener you should put:

        @Override
        public void onClick(View v) {
            if(v.getTag() != null){
                        String[] details = v.getTag().split("|");
                        Intent intent = new Intent(HouseDetail.this, HouseDetailPdf.class);
                        intent.putExtra("title", details[0]);
                        intent.putExtra("url", details[1]);
                        startActivity(intent);
            }
        }
    });

And then, the loop just could be used to setOnClickButtonListener(listener); But sure, you must define this listener as the code above.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top