Java 如何在ListView中显示HashMap中的所有数据?
我正在尝试创建一个简单的recipes应用程序,它将在列表视图中显示配方及其成分,问题是它只显示我的HashMap中的一个项目,以下是我到目前为止的代码: 适配器:Java 如何在ListView中显示HashMap中的所有数据?,java,android,Java,Android,我正在尝试创建一个简单的recipes应用程序,它将在列表视图中显示配方及其成分,问题是它只显示我的HashMap中的一个项目,以下是我到目前为止的代码: 适配器: public class RecipesListAdapter extends BaseAdapter { private final ArrayList mData; public RecipesListAdapter(Map<String, BigDecimal> map) { mData = new
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;
}
}
公共类RecipesListAdapter扩展了BaseAdapter{
私有最终ArrayList mData;
公共配方列表适配器(映射){
mData=newarraylist();
mData.addAll(map.entrySet());
}
@凌驾
public int getCount(){
返回mData.size();
}
@凌驾
公共映射。条目getItem(int位置){
返回(Map.Entry)mData.get(position);
}
@凌驾
公共长getItemId(int位置){
//TODO使用ID实现您自己的逻辑
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
最终查看结果;
if(convertView==null){
结果=LayoutInflater.from(parent.getContext()).flate(R.layout.adapter\u view\u layout,parent,false);
}否则{
结果=转换视图;
}
Map.Entry item=getItem(位置);
((TextView)result.findviewbyd(android.R.id.text1)).setText(item.getKey());
((TextView)result.findviewbyd(android.R.id.text2)).setText(item.getValue().toString());
返回结果;
}
}
您需要将适配器分配给ListView:
result.setAdapter(nameOfAdapter)好吧,我放弃了这种尝试,做了一些不同的事情,我仍然在使用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;
}
}
公共类帮助器{
私有静态字符串名;
私有静态HashMap组件;
公共配方助手(字符串名称、HashMap成分){
this.name=名称;
这个。成分=成分;
}
公共静态字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共静态HashMap getComponents(){
返回成分;
}
公共无效设置成分(HashMap成分){
这个。成分=成分;
}
}
因此,我可以像这样保存在Recipes活动中(稍后用户会插入自己的食谱和配料,但现在我这样做是为了测试):
ListView mListView=(ListView)findViewById(R.id.ListView);
HashMap ingredients1=新HashMap();
IngCredits1.put(“Guanciale(g)”,BigDecimal.valueOf(25));
IngCredits1.put(“去皮西红柿(g)”,大十进制值(100));
ingredients1.put(“葡萄酒(毫升)”,大十进制数值(12.5));
IngCredits1.put(“意大利面/布卡蒂尼(g)”,BigDecimal.valueOf(100));
IngCredits1.put(“Pecorino Romano(g)”,BigDecimal.valueOf(15));
RecipesHelper recipe1=新配方助手(“意大利通心粉”,Ingredits1);
ArrayList recipeList=新的ArrayList();
recipeList.add(recipe1);
RecipesListAdapter=新RecipesListAdapter(this,R.layout.adapter\u view\u layout,recipeList);
mListView.setAdapter(适配器);
最后,我的适配器如下所示:
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;
}
公共类RecipesListAdapter扩展了ArrayAdapter{
私有上下文;
私人国际资源;
private int lastPosition=-1;
私有静态类视图持有者{
文本视图名称;
文本视图成分;
}
/**
*PersonListAdapter的默认构造函数
*@param上下文
*@param资源
*@param对象
*/
公共RecipesListAdapter(上下文上下文、int资源、ArrayList对象){
超级(上下文、资源、对象);
mContext=上下文;
mResource=资源;
}
@非空
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//获取配方信息
字符串名称=getItem(position).getName();
HashMap Components=getItem(position).getComponents();
//使用名称和成分创建配方对象
配方助手配方=新配方助手(名称、成分);
//创建用于显示动画的视图结果,实际上并不需要这样做
最终查看结果;
//视图持有者对象
视窗座;
if(convertView==null){
LayoutFlater充气机=LayoutFlater.from(mContext);
convertView=充气机。充气(mResource,父项,false);
holder=新的ViewHolder();
holder.name=(TextView)convertView.findViewById(android.R.id.text1);
holder.components=(TextView)convertView.findViewById(android.R.id.text2);
结果=转换视图;
convertView.setTag(支架);
}
否则{
holder=(ViewHolder)convertView.getTag();
结果=转换视图;
}
//再次动画,不需要
动画=AnimationUtils.loadAnimation(mContext,
(位置>最后位置)?R.anim.load\u down\u anim:R.anim.load\u up\u anim);
结果:开始动画(动画);
最后位置=位置;
holder.name.setText(recipe.getName());
holder.contracents.setText(recipe.getcontracents().toString());
返回视图;
}
所以基本上我在这里做的就是找到一种使用经典ArrayAdapter的方法,而不是创建我自己的。你是说在主要活动中?我做到了。这可能是“位置”吗是否始终以0的形式传入getView方法,因为getItemId函数尚未使用正确的代码实现,因此在listview中只显示第一项?getItemId从未从getView调用,仅从getItem调用。但是,是的,0是唯一传入的对象,因为当我在另一个上手动设置位置时编号,它将显示该位置编号中的某些内容
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;
}