The constructor SimpleAdapter(new View.OnClickListener(){}, List>, int, String[], int[]) is undefined
Eclipse 提示建構子不匹配,甚至自動修復添加分號後仍報錯。
View.OnClickListener,但 SimpleAdapter 建構子要求第一參為 Context。List> 應為 List<Map<String, Object>>,鍵值對應 from 陣列。android.widget.SimpleAdapter 而非自定義同名類,或版本不一致。List<Map<String, Object>> data = new ArrayList<>();
Map<String, Object> item = new HashMap<>();
item.put("name", "張三");
item.put("age", 25);
data.add(item);
SimpleAdapter adapter = new SimpleAdapter(
this, // Context
data, // List<Map<String, Object>>
R.layout.item_layout, // resource
new String[]{"name", "age"}, // from
new int[]{R.id.tv_name, R.id.tv_age} // to
);
listView.setAdapter(adapter);
Context 來源:Activity 用 this,Fragment 用 getContext(),Adapter 內部用 parent.getContext()。from 字符串陣列與 Map 鍵完全一致(區分大小寫)。to 資源 ID 陣列對應的 View 存在於 item 佈局中。Project → Clean,排除編譯緩存干擾。若仍報錯,請貼出完整建構子調用代碼與導入語句,進一步定位是編譯期錯誤還是運行時 ClassCastException。