【问题标题】:Assign any unique identifier to elements in a inflated view为膨胀视图中的元素分配任何唯一标识符
【发布时间】:2017-09-15 06:09:53
【问题描述】:

我想知道我们是否可以为膨胀视图中的元素分配任何唯一编号。

我的视图看起来像这样,这张图片可能会让你知道我想要做什么:

我要做的是根据这样选择的微调器项目更改编辑文本框的值:

//spinnerPlanItinerary is the first spinner
    spinnerPlanItinerary.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<Town> listOfTowns = new ArrayList<Town>();
                ArrayAdapter<Town> townplanitineraryadapter2 = new ArrayAdapter<Town>(this,
                        android.R.layout.simple_dropdown_item_1line, listOfTowns);
                townplanitineraryadapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerTown.setAdapter(townplanitineraryadapter2);
                spinnerTown.setText(""); //second spinner in the view
                edBelt.setText(""); //first edit text in the view
                edStrata.setText(""); //second edit text in the view

                townplanitineraryadapter2.notifyDataSetChanged();
            }
        });

我不能为元素提供任何唯一的 ID,因为它们在我的布局文件中已经有 ID。我也一直在使用 setTag()getTag()getTag() 没有给我任何东西。

另请注意,点击这样的按钮会放大卡片视图:

final LinearLayout itineraryDetailLL = (LinearLayout) findViewById(R.id.itineraryDetailLinearlayout);

        final View childView = getLayoutInflater().inflate(R.layout.cardview, null);
        itineraryDetailLL.addView(childView);

【问题讨论】:

  • 对不起,我不明白你想做什么?布局文件中的 ID 已经是唯一 ID。为什么需要一秒钟,为了什么?您拥有所需的一切......您对点击做出反应,然后为您的文本字段设置值。
  • @Grisgram,两个膨胀视图上的编辑文本具有相同的 id,因为同一个视图膨胀两次,因为您可以看到图像。我的问题是,每当我从第一个膨胀视图中的第一个微调器中选择不同的项目时,第二个膨胀视图元素中就会发生更改(设置编辑文本中的值)。我希望这已经说明了我的问题。
  • @Grisgram,我也稍微编辑了我的问题。
  • 你可以通过调用setTag(Object object)来设置一个唯一的id
  • 啊我明白了 - 你动态添加它们 - 好的,现在我明白了。

标签: android layout-inflater


【解决方案1】:

感谢您在 cmets 中进行澄清。

根据我现在的理解,您可以动态扩展并添加额外的文本字段。那么他们都有相同的ID,正确的。

那么,你如何区分它们呢? 简单的解决方案:

final View childView = getLayoutInflater().inflate(R.layout.cardview, null);
itineraryDetailLL.addView(childView);

您不应只创建一个childView 成员,而应创建一个列表(或字典,或最适合您下一步操作的任何内容)。 在每个中,您可以为该视图中包含的编辑文本的 ID 执行 .findViewById

所以,它可能看起来像这样:

Map<Integer, View> myChilds = new ....
final View childView = getLayoutInflater().inflate(R.layout.cardview, null);
itineraryDetailLL.addView(childView);
// Here is your "make it unique" part:
myChilds.put(your_next_unique_id, childView);
...
// When you want to access them
EditText txt = myChilds.get(my_unique_id).findViewById(xml_id_of_edittext);
txt.setText("...");

我希望您知道这可以将您带到哪里。 干杯,格里斯

【讨论】:

  • 谢谢。我试试看
  • 这是解决方案被点燃!非常感谢! :)
【解决方案2】:

如果您想更改 EditText 的值,则不需要为此设置唯一标识符。你可以这样做:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

       public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

        if (spinner.getSelectedItem().toString().equals("someSpinnerData")) {
                  editText.setText("yourDesiredText");    
        } else if (spinner.getSelectedItem().toString().equals("someSpinnerData2")) {
                  editText.setText("someSpinnerData2");    
        }
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多