简体   繁体   中英

How to display all data from HashMap in ListView?

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<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 (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<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;

}
}

You need to assign the adapter to the ListView:

result.setAdapter(nameOfAdapter)

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

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<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);

and then finally my adapter looks like this:

public class RecipesListAdapter extends ArrayAdapter<RecipesHelper> {



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<RecipesHelper> 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<String, BigDecimal> 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'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 > 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM