我想使用HashMap
獲取Adapter
的項目列表,以便ListView
。我打算使用ArrayAdapter<>
,但我不能,因爲它只與List<>
一起工作。我應該使用什麼適配器?我應該在ListView中使用什麼適配器來使用HashMap
回答
你可以得到你的地圖項的數組:
Entry<MyKey, MyValue>[] entries =
map.entrySet().toArray(new Entry<MyKey, MyValue>[0]);
話雖這麼說,如果你在更新地圖或使用地圖功能的適配器,然後進行自定義適配器規劃並不困難,並會更有意義。
您可以通過子類BaseAdapter開始,它提供了所有需要運行的幾個Adapter方法的實現。
沒有預定義的適配器將呈現HashMap。我建議通過擴展BaseAdapter來創建自己的適配器。
編輯:這是更多鈔票與使用HashMap和擴大BaseAdapter,這裏是一個(未經測試)例如:
public class HashMapAdapter extends BaseAdapter {
private HashMap<String, String> mData = new HashMap<String, String>();
private String[] mKeys;
public HashMapAdapter(HashMap<String, String> data){
mData = data;
mKeys = mData.keySet().toArray(new String[data.size()]);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(mKeys[position]);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
String key = mKeys[pos];
String Value = getItem(pos).toString();
//do your view stuff here
return convertView;
}
}
這具有以下警告,該項目的順序是不能保證是相同的爲了添加它們。寫這個例子讓我意識到了;不要在一個適配器使用HashMap的:)
您無法使用HashMap是您的擴展BaseAdapter類,因爲BaseAdapter實現了ListAdapter。 – Eugene 2011-03-08 19:40:33
上面添加的例子 – longhairedsi 2011-03-08 23:04:52
如果有人仍在尋找解決方案,我建議檢查我對類似問題的答案:http:// stackoverflow。com/a/19467717/1891118 – 2014-02-21 15:15:49
感謝您的回答longhairedsi
這具有以下警告,該項目的順序是不能保證你加入他們的順序相同。寫這個例子讓我意識到了;不要在適配器中使用HashMap :)
要解決此問題,請改爲使用LinkedHashMap。
public class HashMapAdapter extends BaseAdapter {
private LinkedHashMap<String, String> mData = new LinkedHashMap<String, String>();
private String[] mKeys;
public HashMapAdapter(LinkedHashMap<String, String> data){
mData = data;
mKeys = mData.keySet().toArray(new String[data.size()]);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(mKeys[position]);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
String key = mKeys[pos];
String Value = getItem(pos).toString();
//do your view stuff here
return convertView;
}
}
在適配器文件
MyAdapter.java
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Map;
public class MyAdapter extends BaseAdapter {
private final ArrayList mData;
public MyAdapter(Map<String, String> map) {
mData = new ArrayList();
mData.addAll(map.entrySet());
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Map.Entry<String, String> getItem(int position) {
return (Map.Entry) mData.get(position);
}
@Override
public long getItemId(int position) {
// TODO implement you own logic with ID
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View result;
if (convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_adapter_item, parent, false);
} else {
result = convertView;
}
Map.Entry<String, String> item = getItem(position);
// TODO replace findViewById by ViewHolder
((TextView) result.findViewById(R.id.text1)).setText(item.getKey());
((TextView) result.findViewById(R.id.text2)).setText(item.getValue());
return result;
}
}
調用此適配器
public void showCinemas(HashMap<String, String> cinemas) {
MyAdapter adapter = new MyAdapter(cinemas);
list.setAdapter(adapter);
}
詞典中維持秩序,如果密鑰類型適用,你總是可以將它們添加到它們自己的ArrayList中,進行排序它使用Collections.sort,並將其作爲參考點。
private ArrayList<String> mKeys = new ArrayList<>();
for (String aKey :data.keySet()){
mKeys.add(aKey);
}
Collections.sort(mKeys);
你能做什麼,恕我直言,是生成您嘗試添加HashMap中的他們鍵值一個新的列表,然後將該列表傳遞到自定義適配器。然後將HashMap集合導入到適配器中,並使用參考鍵列表中的HashMap.get(key);
。
- 1. 我應該使用什麼鍵爲HashMap?
- 2. 我應該使用表適配器嗎?
- 3. 我應該使用同步適配器
- 4. 使用適配器的listview
- 5. 我應該使用哪個適用於Ruby的CouchDB適配器?
- 6. android-在使用Base適配器的Fragment中實現ListVIew,我做錯了什麼?
- 7. 我應該使用什麼控制器?
- 8. 帶有HashMap的ListView適配器
- 9. 在我的項目中使用ListView的適配器
- 10. 我們應該在生產中使用什麼AWS配置?
- 11. 我應該在Xcode中使用什麼工具來查看FPS?
- 12. 在Twisted中使用MongoDB。爲什麼我不應該使用pymongo?
- 13. 爲什麼我應該使用額外的佈局文件來呈現ListView?
- 14. 爲什麼我應該在python中使用@property裝飾器?
- 15. 何時應該在ActiveMQ中使用JDBC持久性適配器?
- 16. 我應該重新使用表適配器實例嗎?
- 17. 爲什麼來自適配器getView(..)方法的條件不適用於ListView
- 18. 爲什麼我應該在Silverlight應用程序中使用MVVM?
- 19. 我應該在Mac和Windows上使用什麼來編碼C++?
- 20. 我應該使用什麼樣的應用程序服務器?
- 21. 在Java中我使用「導入」,在C#中我使用「使用」,在VB6中我應該使用什麼?
- 22. 我應該在Hudson中使用什麼樣的配置文件來構建我的iOS應用程序?
- 23. 我應該用什麼來比較DBNull?使用DBNull.Value或ToString()。IsNullOrEmpty()
- 24. 我應該使用什麼工具來構建我的網站?
- 25. 我應該使用什麼方法來製作我的Java GUI?
- 26. 我應該使用什麼URL來創建我的simpleDB域?
- 27. 使用適配器來顯示ListView單聲道爲Android
- 28. 我應該使用什麼? AJAX或jQuery?
- 29. 我應該使用什麼jetty jar?
- 30. 我應該使用什麼積壓值?
你如何處理'不能創建一個通用數組 Map.Entry'異常? –
2012-02-02 09:23:19