【问题标题】:Why does my method keep returning 0为什么我的方法一直返回 0
【发布时间】:2018-02-11 19:32:57
【问题描述】:

我在这里有一些代码,其中我有一个 getItem() 方法,它不会返回除零之外的任何东西。我想知道为什么我要努力前进,但不能没有这个。任何人都可以帮忙吗?每次我将 getItem() 与零或 1 进行比较时,无论代码是什么,它总是显示为零,如果需要,我可以发布更多我的代码,例如其他文件,但这应该足够了。请帮忙 ????

    package com.mycompany.myapp;
    import java.util.*;
    import java.text.*;
    import java.lang.reflect.*;

    public class ActivityProducer
    {
        public HashMap<Class<?>, Integer> recipe = new HashMap<Class<?>, Integer>();

        public ActivityProducer(){
        }

         public int getItem(){
             int index = 0;
             for(Class<?> i : recipe.keySet()){
                 index = recipe.get(i);
             }
             return index;
         }

         public Class<?> getName(){
             Class<?> name = null;
             for(Class<?> names : recipe.keySet()){
                 name = names;
             }
             return name;
         }

         public Class<?>[] getNames(){
             Class<?>[] names = new Class<?>[recipe.size()];
              List<Class<?>> n = new ArrayList<Class<?>>();

             n.addAll(recipe.keySet());

              for(int i = 0; i < names.length; i++){
                 names[i] = n.get(i);
             }
             return names;
         }
     }

配方是一个散列图,它的 key 采用 Class 类型, value 采用 Integer 类型。每次我将 getItem() 与数字零进行比较时,总是会获胜,为什么??????所有其他方法都在起作用,只是不是那种方法??????发生这种情况一定是有原因的

这里是主要活动代码

    package com.mycompany.myapp;

    import android.app.*;
    import android.os.*;
    import android.widget.*;
    import android.widget.AdapterView.*;
    import android.view.*;
    import android.content.*;
    import java.util.*;

    public class MainActivity extends Activity
    {
        private Intent intent;
        private ActivityProducer producer;
        private Extendible ex;

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            producer = new ActivityProducer();

    producer.recipe.put(ChileConLecheActivity.class, 1);
    producer.recipe.put(ArrozActivity.class, 2);
    producer.recipe.put(EnchiladasActivity.class, 3);
    producer.recipe.put(SopaActivity.class, 4);

    ex = new Extendible();

    GridView gridView = (GridView) findViewById(R.id.mainGridView);
    gridView.setAdapter(new ImageAdapter(this));

    gridView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent , View view , int position , long id ){
            for(int i = 0; i < producer.recipe.keySet().size(); i++){
                if(position == i){
                    //make content view for each recipe

                            startActivity(makeIntent(position, producer.recipe));


                }
            }
        }
    });




}

private Intent makeIntent(int recipe , HashMap<Class<?>, Integer> map){
    for(int i = 0; i < map.size(); i++){
        for(Class<?> m : map.keySet()){
            intent = new Intent(MainActivity.this , m);
        }
    }
    return intent;
}
    }

这是我尝试切换布局或调用所在的代码

        package com.mycompany.myapp;
        import android.app.*;
        import android.os.*;
        import android.widget.*;
        import android.text.*;
        import android.content.*;

        public class Extendible extends Activity implements IActivities
    {
private LinearLayout layout;
private ActivityProducer pro;

public Extendible(){
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO: Implement this method
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recipes_layout);

    pro = new ActivityProducer();

    if(pro.getItem() == 1){
        chileConLecheLayout();
    } else if(pro.getItem() == 2){
        arrozLayout();
    }
}

public void chileConLecheLayout(){
    layout = (LinearLayout) findViewById(R.id.recipes_layoutLinearLayout);
    TextView recipeName = new TextView(Extendible.this);
    recipeName.setText("Chile Con Leche");
    recipeName.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT));
    recipeName.setTextSize(60);
    layout.addView(recipeName);
}

public void arrozLayout(){
    layout = (LinearLayout) findViewById(R.id.recipes_layoutLinearLayout);
    TextView recipeName = new TextView(Extendible.this);
    recipeName.setText("Arroz");
    recipeName.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT));
    recipeName.setTextSize(60);
    layout.addView(recipeName);
}

public void enchiladasLayout(){
    layout = (LinearLayout) findViewById(R.id.recipes_layoutLinearLayout);
    TextView recipeName = new TextView(Extendible.this);
    recipeName.setText("Enchiladas");
    recipeName.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT));
    recipeName.setTextSize(60);
    layout.addView(recipeName);
}
    }

【问题讨论】:

  • 在你的配方图中将值放在哪里?
  • 看起来像 recipe.put(enchiladas.class, 0);和 recipe.put(tacos.class, 1);以此类推在主活动类中
  • 我应该发布更多代码吗???
  • 是的,也请发布电话。
  • 好一会儿

标签: java android return return-value


【解决方案1】:

你以一种奇怪的方式使用地图;您的 getItem() 只会遍历 keySet 并返回最后一个条目(请注意,Set 没有排序,所以这基本上是随机的)。

按照我理解您的代码的方式,您希望获得与类关联的索引,如下所示:

public int getItem(Class clazz) {
    int index = 0;
    if(recipe.get(clazz) != null) {
        index = recipe.get(clazz);
    }

    return index;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 2021-04-19
    • 2013-01-26
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    相关资源
    最近更新 更多