0

配列ListViewを使用して を作成しようとしています。HashMapしかし、HashMap配列を使用する例を見つけることができませんでした。使用の概念は理解していますが、配列ListViewsを使用して組み込むことはできないようですHashMap。彼らが私に指摘できる例を知っている人はいますか?SOにはHashMap、タイプ配列ではなくaだけを使用する例がいくつかあります。

あなたの助けに感謝します。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.deals);

    ds = new DealsSQL(DealsUI.this);
    dm = new DealsManage(DealsUI.this);

    //new LoadDeals().execute();

    ArrayList<HashMap<String, String>> inboxList new ArrayList<HashMap<String, String>>();

        ListView listView = (ListView) findViewById(android.R.id.list);
        listView.setAdapter(new HashMapAdapter(inboxList));
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
}

public class HashMapAdapter extends BaseAdapter {

    private ArrayList<HashMap<String, String>> mData = new ArrayList<HashMap<String, String>>();
    private String[] mKeys;
    public HashMapAdapter(ArrayList<HashMap<String, String>> inboxList){
        mData  = inboxList;
        mKeys = mData.keySet().toArray(new String[inboxList.size()]);

    }


    public int getCount() {
        return mData.size();
    }


    public Object getItem(int position) {
        return mData.get(mKeys[position]);
    }


    public long getItemId(int arg0) {
        return arg0;
    }


    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;
    }
}
4

1 に答える 1

0

あなたが今やっていることは、すべてのリスト項目で HashMap を取得することです。私が(あなたのコードから)あなたがしたいことは、あなたのすべての行で、あなたListViewの内容をフェッチして表示することですHashMap<String,String>。とはいえ、ここに例があります。

ofListViewを使用して を作成することは、他の と同じです。-method で、メソッドを使用して現在のアイテムをフェッチします。次に、通常どおりビューを作成します。ArrayListHashMap<?,?>ArrayListgetView(...)int posgetItem(int pos)

ViewHolder パターン (および convertView) の詳細については、http://www.vogella.com/articles/AndroidListView/article.html#adapterperformance_hoder を確認してください

例:

...

/**
 * Return the HashMap that is stored in your ArrayList at position, instead
 * of just the value.
 */
public HashMap<String, String> getItem(int position) {
    return mData.get(position);
}

...

public View getView(int pos, View convertView, ViewGroup parent) {

    HashMap<String, String> item = getItem(pos);
    ViewHolder holder;        

    if(convertView == null) {
        // ConvertView is re-used by ListView. That means we don't have to re-inflate
        // it on every row. Instead, if convertView is already set we just reuse it.
        // To prevent having to call .findViewById( id ) every time, we use the pattern
        // called the `ViewHolder`-pattern.
        convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item);

        holder = new ViewHolder();
        holder.textview1 = convertView.findViewById(R.id.textview1);

        convertView.setTag(holder);

    } else {
        holder = convertView.getTag();
    }

    // now, we have both a populated convertView and a reference to it's individual subviews
    // in the form of holder.

    // manipulate the views in your listitem here
    // I assume you want to display the contents of the HashMap<String,String> here
    // so convert the HashMap to something iterable, like EntrySet
    for(Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            // notice how we set the TextView attributes by accessing holder.textview1
            holder.textview1.setText( key + " => " + value + "\n");
    }


    return convertView;
}

private class ViewHolder {
    public TextView textview1;
}

.

ノート:

HashMap エントリごとに listitem を表示したいだけの場合は、それを を使用して配列に変換し、hashmap.entrySet().toArray()その配列をコンストラクターで HashMapAdapter にフィードするのが最善だと思いMap.EntryますmDatasetgetView(...)-method では、データ項目をループする代わりに、 and を使用しitem.getKey()ますitem.getValue()。また、あなたのgetItem(position)-method はMap.Entryの代わりにtype を返す必要がありHashMap<String, String>ます。

于 2012-09-02T11:42:07.760 に答える