我在这里有一些代码,我有一个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;
}
}
配方是一个散列映射,它为键获取Class类型,为值获取Integer类型。每次我将getItem()与数字0进行比较总是赢得为什么?????所有其他方法都工作不只是那个?????必须有这样的原因才能发生这种情况
以下是主要活动代码
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 :(得分:0)
您以奇怪的方式使用地图;你的getItem()将遍历keySet并返回最后一个条目(注意Sets没有被排序,所以这基本上是随机的。)
我理解你的代码的方式,你想得到一个类的索引,如下所示:
public int getItem(Class clazz) {
int index = 0;
if(recipe.get(clazz) != null) {
index = recipe.get(clazz);
}
return index;
}