[英]Display my HashSet to a listVIew
我試圖將我的HashSet數組顯示在ListView上,以便可以單擊單元格並顯示警報。
我的代碼現在在該HashSet數組中具有三個項目,並能夠通過EditText添加更多項目,該EditText捕獲用戶輸入並將其存儲在HashSet中。
我想知道是否有一種方法可以將HashSet項顯示在列表視圖中。 我相信列表視圖使我可以單擊一個單元格並通過警報顯示其他隨機信息。
這有可能嗎?
這是我的代碼:
public class MainActivity extends Activity {
Button aButton; // Global Scope
Button sButton;
TextView text2;
EditText eText;
HashSet<String> list = new HashSet<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);
aButton = (Button) this.findViewById(R.id.button1);
text2 = (TextView) this.findViewById(R.id.textView1);
aButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
list.add("Books");
list.add("Newspapers");
list.add("Magazines");
String listString = "";
for (String s : list) {
listString += s + " - ";
}
text2.setText(listString);
}
});
sButton = (Button) this.findViewById(R.id.button2);
eText = (EditText) this.findViewById(R.id.editText1);
sButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//Log.v("EditText", eText.getText().toString());
if( !list.add(eText.getText().toString()) )
{
System.out.println("Not Unique Item");
Toast.makeText(getApplicationContext(), "Already Saved!",Toast.LENGTH_LONG).show();
} else
{
System.out.println("Unique Entry Added");
Toast.makeText(getApplicationContext(), "Saved To Items.", Toast.LENGTH_LONG).show();
}
}
});
}
}
HashSet的問題是,根據定義,集沒有順序,因此允許與ListView直接耦合沒有太大意義。 但是很容易將HashSet轉換為ArrayList,如下所示:
List<String> list = new ArrayList<String>(hashset);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.