Frage

I am inflating the passenger_details_layout in main_passenger_details_layout.At run time i am setting the values in TextView . I am setting up pax_id that is invisible for every row that i am inflating in it.So on click of passenger_details_layout i want to call a new activity and pass the pax_id to the new activity. Pax_id will be like 1,2,3 for every time for loop will run.So on click of 1 row i want to get the pax_id of first row that i have set.

Please help how could i do this Will be thank full to you.

Code to Infalate

ItinResponse data = obj.parseXMLData(fXmlFile);
        List<SectorRecords> sectorList = data.getSectorRecordsDetails();
        int items = 0;
        List<PassengerDetails> listdata = data.getPaxDetails();
        int i = 0;
        for (PassengerDetails elements : listdata) {
            inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            RelativeLayout layout = (RelativeLayout) inflater.inflate(
                    R.layout.passenger_details_layout, main, false);
            firstName = (TextView) layout.findViewById(R.id.first_pax_name);
            pax_id = (TextView) layout.findViewById(R.id.pax_id);
            firstName.setText(elements.getFirstName() + " "
                    + elements.getLastName());
            pax_id.setText(elements.getPaxId());
            main.addView(layout, i);
            i++;


 layout.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)"+pax_id.getText(), Toast.LENGTH_LONG).show();

                    }
                });

On every row is showing the last pax id

War es hilfreich?

Lösung

You have to do nothing what you are trying

Replace this

pax_id.setText(elements.getPaxId());

to

layout.setId(Integer.parseInt(elements.getPaxId()));

And in your Layout Click

layout.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)"+layout.getId(), Toast.LENGTH_LONG).show();
                    }
                });

Every time you will get the Unique ID that u want to set

All the best...!!!!

Andere Tipps

Use

TextView paxView = (TextView)v.findViewById(R.id.pax_id);
String pax_value = paxView.getText().toString();

On button click of every row do like this

Intent intent=new Intent(this,YOUR_NEXT_ACTIVIY.class);
                intent.putExtra("paxid",  pax_id.getText());
                startActivity(intent);
ItinResponse data = obj.parseXMLData(fXmlFile);
        List<SectorRecords> sectorList = data.getSectorRecordsDetails();
        int items = 0;
        List<PassengerDetails> listdata = data.getPaxDetails();
        int i = 0;
        for (PassengerDetails elements : listdata) {
            inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            RelativeLayout layout = (RelativeLayout) inflater.inflate(
                    R.layout.passenger_details_layout, main, false);
            firstName = (TextView) layout.findViewById(R.id.first_pax_name);
            pax_id = (TextView) layout.findViewById(R.id.pax_id);
            firstName.setText(elements.getFirstName() + " "
                    + elements.getLastName());
            pax_id.setText(elements.getPaxId());
            layout.setTag(elements.getPaxId());
            main.addView(layout, i);
            i++;
layout.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        String paxId = v.getTag().toString();
                        // do some staff
                    }
                });
        }
// try this
        ItinResponse data = obj.parseXMLData(fXmlFile);
        List<SectorRecords> sectorList = data.getSectorRecordsDetails();
        int items = 0;
        List<PassengerDetails> listdata = data.getPaxDetails();
        int i = 0;
        for (PassengerDetails elements : listdata) {
            inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            RelativeLayout layout = (RelativeLayout) inflater.inflate(
                    R.layout.passenger_details_layout, main, false);
            firstName = (TextView) layout.findViewById(R.id.first_pax_name);
            pax_id = (TextView) layout.findViewById(R.id.pax_id);
            firstName.setText(elements.getFirstName() + " "
                    + elements.getLastName());
            pax_id.setText(elements.getPaxId());
            layout.setTag(elements);
            main.addView(layout, i);
            i++;


            layout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    PassengerDetails elements = (PassengerDetails) v.getTag();
                    Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)"+elements.getPaxId(), Toast.LENGTH_LONG).show();

                }
            });
        }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top