我在 lst1 中有一些os项目,我想将它们放入 lst2 但不在每个 ListBox 上重复它们。
界面我使用:
这是有效的,但您可能需要它来理解我的疑问。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adpt = new SimpleAdapter(new ArrayList(), this);
ListView lView = (ListView) findViewById(R.id.listview);
lView.setAdapter(adpt);
// Exec async load task
(new AsyncListViewLoader()).execute("http://10.0.2.2:8080/JSONServer/rest/ContactWS/get");
}
的疑问:
现在,用户按下按钮将 lst1 的所选项目添加到 lst2 。简单!但是,如果他试图添加相同的项目,该怎么办? VB.Net可以阻止他这样做吗?
Dim dtTa_Enc As DataTable = New DataTable("Ta_Enc")
Dim dsTa As DataSet = New DataSet("Ta_E")
Dim adapter As New MySqlDataAdapter
Dim ds As DataSet = New DataSet
adapter.SelectCommand = New MySqlCommand
adapter.SelectCommand.Connection = connection
adapter.SelectCommand.CommandText = query
connection.Open()
adapter.Fill(ds, "tables")
connection.Close()
lst1.DataSource = ds.Tables("tables")
lst1.DisplayMember = "name"
lst1.ValueMember = "codta"
dtTa_Enc.Columns.Add("codta")
dtTa_Enc.Columns.Add("name")
dsTa.Tables.Add(dtTa_Enc)
lst2.DataSource = dsTa.Tables("Tables")
lst2.DisplayMember = "name"
lst2.ValueMember = "codta"
dtTa_Enc.Rows.Add(lst1.ValueMember, lst1.GetItemText(lst1.SelectedItem))
答案 0 :(得分:0)
您可以通过 lst2 进行枚举,以确定是否有任何值符合您选择的项目lst1.SelectedItem
。
所以循环,
Dim found As Boolean = False
For Each itm As String in lst2.Items
If itm = lst1.SelectedItem Then
found = True
End If
Next
If Not found Then
'Add it.
End If
或者您只需使用Contains
方法:
If Not lst2.Items.Contains(lst1.SelectedItem) Then
'Add it.
End if.
答案 1 :(得分:0)
在Add事件处理程序下,您可以在开头放置一些逻辑,以防止添加重复的项目。
Dim lst1Selected As String = CType(lst1.SelectedItem, DataRowView)("name").ToString
Dim flag As Integer = 0
For Each item As Object In lst2.Items
Dim istr As String = CType(item, DataRowView)("name").ToString
If istr = lst1Selected Then
flag = 1
Exit For
End If
Next
If flag = 1 Then
Exit Sub
End If