简体   繁体   English

如何在ObservableList中编辑特定对象?

[英]How can I edit a specific object in ObservableList?

For example, I have an ObservableList which holds two strings: 例如,我有一个ObservableList ,其中包含两个字符串:

string1 = "foo"
string2 = "bar"

I want to edit string1 so that it becomes "foobaz". 我想编辑string1 ,使其变为“ foobaz”。

How can I do that? 我怎样才能做到这一点?

I've tried using .set(int index, Object element) , but it adds another element, instead of editing the element at that index. 我尝试使用.set(int index, Object element) ,但是它添加了另一个元素,而不是在该索引处编辑该元素。

I've tried using .set(int index, Object element) , and now it works (?). 我尝试使用.set(int index, Object element) ,现在它可以工作了(?)。 So that means that I've basically solved my problem. 这意味着我已经基本解决了我的问题。

Thanks for the replies everyone, and sorry for wasting your time. 感谢大家的答复,也很抱歉浪费您的时间。

Assuming you want to replace the String foo and obsrverList is an ArrayList of String - 假设您要替换String fooobsrverListStringArrayList

int size = obsrverList.size();

for(int index=0; index<size; index++){

   if("foo".equals(observerList.get(index)){

      obsrverList.set(index, "foobaz");

   }

}  

You have to set String instead of Object if obsrverList is an ArrayList of String . 如果obsrverListStringArrayList ,则必须设置String而不是Object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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