简体   繁体   中英

Assign different ids to the same view

One Parent Linear layout is inflated and child views are added in the for-loop. But all child-view have the same id. I could find the id out by v.getId() and then hardcode the id into the case "id of child view" . But id might change on another phone.
In my case all ids are the same and to set them manually is probably also bad practice. I want to call the child-views by the onClick(View v) -Method but can't get id right.
I added a tree view for illustration: tree view layout

@Override
    public void onCreate(Bundle savedInstanceState) {
        // extending existing parent class' methods
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.mainbase, null);
        for (int i = 0; i < 4; i++) {
            View custom = inflater.inflate(R.layout.identifierplate, null);
            switch (i) {
                case 0: {
                    //set layout images and text for first <include>
                    ImageView SensorReadout = (ImageView) custom.findViewById(R.id.sensor_icon);
                    SensorReadout.setImageResource(R.drawable.ic_launcher);
                    TextView sensor_name = (TextView) custom.findViewById(R.id.sensor_name);
                    sensor_name.setText(R.string.sr_1);
                    TextView sensor_description = (TextView) custom.findViewById(R.id.sensor_description);
                    sensor_description.setText(R.string.sr_2);
                    custom.setOnClickListener(this);
                    parent.addView(custom);
                    break;
                }
                case 1: {
                    // do same as in case 0 but different text/image
                    custom.setOnClickListener(this);
                    parent.addView(custom);
                    break;
                }      
                default: { // Defaults are already set in the XML                    
                }
            }
        }
        setContentView(parent);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case 0: {
                Intent intent = new Intent(this, First.class);
                startActivity(intent);
                break;
            }
            case 1: {
                Intent intent = new Intent(this, Second.class);
                startActivity(intent);
                break;
            }
            default: {
                int id = v.getId();
                String ids = String.valueOf(id);
                Toast.makeText(this, ids, Toast.LENGTH_SHORT).show(); //error: all have same id
            }
        }
    }

How about using setTag and getTag method of childview ?

You can then check if getTag == first child and according code your logic.

In the for loop you can set tag using the setTag with i variable.

Mike M. pointed me into the right direction I added to .java:

custom.setId(R.id.id_v_2);

added to ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item
        type="id"
        name="id_v_0" />
    <item
        type="id"
        name="id_v_1" />
</resources>

changed the onClick()

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.id_v_0: { ...

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