2017-09-03 76 views
-3

我在这里有一些代码,我有一个getItem()方法,它不会返回任何东西,但零。我想知道为什么我试图前进,但不能没有这种方式。谁能帮忙?每次我将getItem()与0或1进行比较时,无论代码如何,我都可以发布更多代码(如其他文件,如果需要的话),但始终显示为零,但现在应该足够了。请帮忙 ????为什么我的方法不断返回0

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

配方是一个HashMap其中所花费的密钥和一个整数类型为值一类的类型。每次我比较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); 
} 
    } 
+0

哪里放值在你的食谱地图? – JensS

+0

它看起来像recipe.put(enchiladas.class,0);和recipe.put(tacos.class,1);等主要活动类 –

+0

我应该发布更多的代码? –

回答

0

您以奇怪的方式使用Map;你的getItem()只会迭代keySet并返回最后一个条目(注意Sets不是有序的,所以它基本上是随机的)。

我理解你的代码的样子,你想获得与类关联的指标,它应该是这样的:

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

    return index; 
}