2017-09-03 77 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; 
}