将多个项目添加到哈希图



我正忙于一个需要用数据库中的下拉列表填充edit texts的程序,我正在使用哈希图,但我需要在哈希图中放置大约 10 列,因为有 10 个字段需要填充。

这是我的代码:

try {
ConnectionHelper conStr = new ConnectionHelper();
connect = conStr.connectionclass();
if (connect == null) {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
} else {
String query = "select * from Customers";
stmt = connect.prepareStatement(query);
rs = stmt.executeQuery();
ArrayList<String> dataF = new ArrayList<>();
hashmap = new HashMap<>();

while (rs.next()) {
String id = rs.getString("CUSTOMER_ID");
String CName = rs.getString("CUSTOMER_FIRST_NAME");
String Surname = rs.getString("CUSTOMER_SURNAME");
String Number = rs.getString ("CUSTOMER_TEL_NUMBER");
String Cell= rs.getString ("CUSTOMER_CELL_NUMBER");
String Buiilding= rs.getString ("CUSTOMER_BUILDING_NUMBER");
String Street= rs.getString ("CUSTOMER_STREET");
String suburb= rs.getString ("CUSTOMER_SUBURB");
String City= rs.getString ("CUSTOMER_CITY");
String postal= rs.getString ("CUSTOMER_ZIP_CODE");
// value of database
dataF.add(id);
hashmap.put(id,CName,Surname,Number,Cell,Buiilding,Street,suburb,City,postal);
}
ArrayAdapter NoCoreAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataF);
customerselect.setAdapter(NoCoreAdapter);
}
} catch (SQLException e) {
e.printStackTrace();
}
customerselect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String name = customerselect.getSelectedItem().toString();
String CName = hashmap.get(name);
String Surname = peopleByForename.get(name);
String Number = hashMap1.get(name);
String Cell = hashMap2.get(name);
String Building = hashMap2.get(name);
String Street = hashMap3.get(name);
String suburb = hashMap3.get(name);
String City = hashMap4.get(name);
String postal = hashMap4.get(name);
rFname.setText(CName);
rLname.setText(Surname);
rTelnum.setText(Number);
rCellnum.setText(Cell);
rCusbuild.setText(Building);
rCusstr.setText(Street);
rCussub.setText(suburb);
rCuscity.setText(City);
rCuszip.setText(postal);

}

我收到以下错误:

put() in HashMap cannot be applied to:
Expected Actual
Parameters Arguments
Key: String id
value: String CName 
Surname(java.lang.string)
Number(java.lang.string)
Cell(java.lang.string)
Building(java.lang.string)
Street(java.lang.string)
suburb(java.lang.string)
City(java.lang.string)
postal(java.lang.string)

我使用哈希图的原因是因为我遵循的示例使用了哈希地图,我不知道如何使用列表显示在下拉列表中

HashMap只接受两个参数:keyvalue

https://docs.oracle.com/javase/8/docs/api/index.html?java/util/HashMap.html


key应该是对象唯一的标识符。在您的情况下,您正在使用id.如果这是数据库表上的主键(或任何其他唯一编号(,则完全正确。

其他字段需要包装成一个对象(或者单独存储在HashMap中,每个字段都有自己唯一的id;根据您的问题,看起来您正在做的事情并非如此(。

你应该做这样的事情:

final Customer customerObject = new Customer(CName, Surname, Number, Cell, Buiilding, Street, suburb, City, postal);
hashmap.put(id, customerObject);

创建自定义Customer类及其所有字段留给读者作为练习。

如果你查看Map接口的put方法的源代码,你会发现你只能找到:

put(K key, V value)

这正是Map应该如何运作的。将键映射到值,以便可以快速访问它,而无需迭代整个映射。因此,在您的情况下,我建议您将所有信息存储在像Customer这样的 serperate 对象中,并将其映射到客户的相关 ID。

如果您需要有效地查询客户数据(同时不使用 id(,请考虑使用可以为您完成工作的数据库。

哈希映射只能包含两列。<键><值>可以创建 Customer 类并将这些值添加到 Customer 对象中。然后将该类放入哈希映射中。

Hashmap<String,Customer> map = new HashMap<>();
map.put(id, new Customer(id,surname,number...));

Put 方法是Map<K, V>.put(K key, V value)签名的。因此,您不能只添加值。首先弄清楚密钥应该是什么,然后使用 put 方法。可以通过调用Map.get(Object key)方法来使用键检索值。

我会创建包含所有这些字段的客户对象,而不仅仅是将该对象添加到映射中,就像hashmap.put(id, yourCustomerObject);

这只是一个例子,我希望你明白你是如何在你的场景中使用这个例子的。

public class Person{
String name,surName,number,....;
public String getName(){
return name;
}
public void  setame(String name){
this.name=name;
}
//Create more getters setters like this
}

// create our map
Map<String, List<Person>> peopleByForename = new HashMap<>();    
// populate it
List<Person> people = new ArrayList<>();
people.add(new Person("Bob Smith"));
people.add(new Person("Bob Jones"));
peopleByForename.put("Bob", people);
// read from it
List<Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs[0];
Person bob2 = bobs[1];

最新更新