如何在ListView中显示HashMap的所有数据?

huangapple go评论77阅读模式
英文:

How to display all data from HashMap in ListView?

问题

Adapter:

public class RecipesListAdapter extends BaseAdapter {
    private final ArrayList<Map.Entry<String, BigDecimal>> mData;

    public RecipesListAdapter(Map<String, BigDecimal> map) {
        mData = new ArrayList<>();
        mData.addAll(map.entrySet());
    }

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

    @Override
    public Map.Entry<String, BigDecimal> getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO implement your 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.adapter_view_layout, parent, false);
        } else {
            result = convertView;
        }

        Map.Entry<String, BigDecimal> item = getItem(position);

        ((TextView) result.findViewById(android.R.id.text1)).setText(item.getKey());
        ((TextView) result.findViewById(android.R.id.text2)).setText(item.getValue().toString());

        return result;
    }
}

Note: The code you provided had some HTML-encoded characters (e.g., &lt; and &gt;) that I've corrected in the translated code. Make sure to use the corrected code in your implementation.

英文:

I'm trying to create a simple recipes app that will display recipes and their ingredients in list view, the problem I have is it only displays one item from my HashMap, here is my code so far:

Adapter:

public class RecipesListAdapter extends BaseAdapter {
private final ArrayList mData;
public RecipesListAdapter(Map&lt;String, BigDecimal&gt; map) {
mData = new ArrayList();
mData.addAll(map.entrySet());
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Map.Entry&lt;String, BigDecimal&gt; 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.adapter_view_layout, parent, false);
} else {
result = convertView;
}
Map.Entry&lt;String, BigDecimal&gt; item = getItem(position);
((TextView) result.findViewById(android.R.id.text1)).setText(item.getKey());
((TextView) result.findViewById(android.R.id.text2)).setText(item.getValue().toString());
return result;
}
}

答案1

得分: 0

你需要将适配器分配给 ListView:

result.setAdapter(nameOfAdapter)

英文:

You need to assign the adapter to the ListView:

result.setAdapter(nameOfAdapter)

答案2

得分: 0

好的,这是翻译后的内容:

好的,我放弃了以这种方式尝试,尝试了不同的方法,我仍然在使用 HashMap 来保存数据,因为在将来扩展这个项目时我会需要它,但这是我现在的做法。

我创建了一个 HelperClass 用于保存数据。

public class RecipesHelper {
    private static String name;
    private static HashMap<String, BigDecimal> ingredients;

    public RecipesHelper(String name, HashMap<String, BigDecimal> ingredients) {
        this.name = name;
        this.ingredients = ingredients;
    }

    public static String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static HashMap<String, BigDecimal> getIngredients() {
        return ingredients;
    }

    public void setIngredients(HashMap<String, BigDecimal> ingredients) {
        this.ingredients = ingredients;
    }
}

这样我可以像这样在 Recipes 活动中保存数据(在以后的日期里,用户会插入自己的食谱和配料,但现在为了测试,我是这样做的):

ListView mListView = (ListView) findViewById(R.id.listView);

HashMap<String, BigDecimal> ingredients1 = new HashMap<>();
ingredients1.put("Guanciale(g)", BigDecimal.valueOf(25));
ingredients1.put("Peeled Tomatoes(g)", BigDecimal.valueOf(100));
ingredients1.put("Wine(ml)", BigDecimal.valueOf(12.5));
ingredients1.put("Spaghetti/Bucatini(g)", BigDecimal.valueOf(100));
ingredients1.put("Pecorino Romano(g)", BigDecimal.valueOf(15));

RecipesHelper recipe1 = new RecipesHelper("Pasta all'Amatriciana", ingredients1);
ArrayList<RecipesHelper> recipeList = new ArrayList<>();
recipeList.add(recipe1);

RecipesListAdapter adapter = new RecipesListAdapter(this, R.layout.adapter_view_layout, recipeList);
mListView.setAdapter(adapter);

最后,我的适配器看起来像这样:

public class RecipesListAdapter extends ArrayAdapter<RecipesHelper> {

    private Context mContext;
    private int mResource;
    private int lastPosition = -1;

    private static class ViewHolder {
        TextView name;
        TextView ingredients;
    }

    public RecipesListAdapter(Context context, int resource, ArrayList<RecipesHelper> objects) {
        super(context, resource, objects);
        mContext = context;
        mResource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String name = getItem(position).getName();
        HashMap<String, BigDecimal> ingredients = getItem(position).getIngredients();
        RecipesHelper recipe = new RecipesHelper(name, ingredients);

        final View result;
        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(android.R.id.text1);
            holder.ingredients = (TextView) convertView.findViewById(android.R.id.text2);
            result = convertView;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            result = convertView;
        }

        Animation animation = AnimationUtils.loadAnimation(mContext,
                (position > lastPosition) ? R.anim.load_down_anim : R.anim.load_up_anim);
        result.startAnimation(animation);
        lastPosition = position;

        holder.name.setText(recipe.getName());
        holder.ingredients.setText(recipe.getIngredients().toString());

        return convertView;
    }
}

所以基本上我在这里所做的是找到了一种使用经典的 ArrayAdapter 的方法,而不是创建自己的方法。

英文:

Okay I gave up on trying it this way and did something different, I'm still using HashMap for saving data because down the road I will need it when I expand this project but this is how I'm doing it now.

I created a HelperClass for saving data

public class RecipesHelper {
private static String name;
private static HashMap&lt;String, BigDecimal&gt; ingredients;
public RecipesHelper(String name,HashMap&lt;String, BigDecimal&gt; ingredients) {
this.name = name;
this.ingredients=ingredients;
}
public static String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static HashMap&lt;String, BigDecimal&gt; getIngredients() {
return ingredients;
}
public void setIngredients(HashMap&lt;String, BigDecimal&gt; ingredients) {
this.ingredients = ingredients;
}
}

so that I can save in Recipes activity like this (at a later date a user would insert his own recipes and ingredients, but for now I'm doing it like this for testing):

    ListView mListView = (ListView) findViewById(R.id.listView);
HashMap&lt;String, BigDecimal&gt; ingredients1 = new HashMap&lt;&gt;();
ingredients1.put(&quot;Guanciale(g)&quot;, BigDecimal.valueOf(25));
ingredients1.put(&quot;Peeled Tomatoes(g)&quot;, BigDecimal.valueOf(100));
ingredients1.put(&quot;Wine(ml)&quot;, BigDecimal.valueOf(12.5));
ingredients1.put(&quot;Spaghetti/Bucatini(g)&quot;, BigDecimal.valueOf(100));
ingredients1.put(&quot;Pecorino Romano(g)&quot;, BigDecimal.valueOf(15));
RecipesHelper recipe1 = new RecipesHelper(&quot;Pasta all&#39;Amatriciana&quot;, ingredients1);
ArrayList&lt;RecipesHelper&gt; recipeList = new ArrayList&lt;&gt;();
recipeList.add(recipe1);
RecipesListAdapter adapter = new RecipesListAdapter(this, R.layout.adapter_view_layout,recipeList);
mListView.setAdapter(adapter);

and then finally my adapter looks like this:

public class RecipesListAdapter extends ArrayAdapter&lt;RecipesHelper&gt; {
private Context mContext;
private int mResource;
private int lastPosition = -1;
private static class ViewHolder {
TextView name;
TextView ingredients;
}
/**
* Default constructor for the PersonListAdapter
* @param context
* @param resource
* @param objects
*/
public RecipesListAdapter(Context context, int resource, ArrayList&lt;RecipesHelper&gt; objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//get the recipe information
String name = getItem(position).getName();
HashMap&lt;String, BigDecimal&gt; ingredients =  getItem(position).getIngredients();
//Create the recipe object with the name and ingredients
RecipesHelper recipe = new RecipesHelper(name,ingredients);
//create the view result for showing the animation, doesn&#39;t really need this
final View result;
//ViewHolder object
ViewHolder holder;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
holder= new ViewHolder();
holder.name = (TextView) convertView.findViewById(android.R.id.text1);
holder.ingredients = (TextView) convertView.findViewById(android.R.id.text2);
result = convertView;
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
result = convertView;
}
//Again animation, not needed
Animation animation = AnimationUtils.loadAnimation(mContext,
(position &gt; lastPosition) ? R.anim.load_down_anim : R.anim.load_up_anim);
result.startAnimation(animation);
lastPosition = position;
holder.name.setText(recipe.getName());
holder.ingredients.setText( recipe.getIngredients().toString());
return convertView;
}

So basically what I did here was just find a way to use the classic ArrayAdapter instead of creating my own.

huangapple
  • 本文由 发表于 2020年9月9日 23:25:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63814811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定