【发布时间】:2014-08-11 13:10:30
【问题描述】:
我正在尝试使用已设置为主类子类的多个枚举作为单独片段上列表视图的数据源。
每个片段将有两个列表视图,但每个列表视图只需要使用数据的某些部分。每个枚举都有两个字符串“name”和“abbr”以及一个双精度“value”。
我想将两个字符串都设置为 ListView 标题,并在计算中使用该值。
Listview1 将在中间行包含标题、缩写和一个 EditText。 Listview2 将包含标题、缩写和另外一个 TextView,它们将根据 EditText 输入和来自枚举的值进行更新。我意识到我需要两个自定义适配器,一个用于异构Listview1,一个用于Listview2。
我对实现有点迷茫,只尝试为Listview2 做自定义适配器。
我尝试查看多个 SO 问题,以及使用数据库模型的 listview 教程,然后尝试使用它,但使用我的静态枚举列表,但我有点迷失了。任何来自高级方法、细节或不错的教程的帮助将不胜感激。我可能还没有走上正确的道路,因为我是 Android 新手,而且在 OOP 方面相对较新,感谢您忍受糟糕的代码!
到目前为止我所做的(我建立了一个测试项目,这就是为什么我在 main 中而不是在片段中膨胀了这个列表视图 - 如果除了切换上下文之外还有任何问题,请告诉我):
包含所有枚举的枚举类
public class Enums {
public enum Pressures{
ITEM1 ("name", "abbr", 1.0),
etc...;
private final String name;
private final String abbr;
private final double value;
Pressures(String name, String abbr, double value) {
this.name = name;
this.abbr = abbr;
this.intermediary = intermediary;
}
public String getNames() {
return name;
}
public String getAbbr() {
return abbr;
}
public double getIntermediary() {
return intermediary;
}
}
public enum Enum2 {
...
}
}
Listview2 的自定义适配器:
public class CustomAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Enums.Pressures> pressureEnum;
public CustomAdapter(Activity activity, List<Enums.Pressures> units) {
this.activity = activity;
this.pressureEnum = units;
}
@Override
public int getCount() {
return pressureEnum.size();
}
@Override
public Object getItem(int location) {
return pressureEnum.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView abbr = (TextView) convertView.findViewById(R.id.abbr);
Enums.Pressures p = pressureEnum.get(position);
name.setText(p.getNames());
abbr.setText(p.getAbbr());
return convertView;
}
}
主要:
public class Main extends Activity {
//This List was a poor attempt at setting the list from the enum
//I don't believe ArrayList is the proper choice as I have an enum object
//but I'm not quite sure what to use
private List<Enums.Pressures> pressureUnits = new ArrayList<Enums.Pressures>();
private ListView listView;
private CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomAdapter(this, pressureUnits);
listView.setAdapter(adapter);
}
}
【问题讨论】:
-
您是否尝试过使用
Enums.Pressures.values();来返回Enums.Pressures[]数组中的所有压力?不过,您仍然需要一个自定义适配器。我很惊讶您可以像这样访问Enums.Pressures,而无需将其指定为public static enum。 -
@Zhu 枚举在 Java 中默认是静态的,不需要声明为静态的。从我对它们的研究中我确实知道一件事!另外,感谢您的回复。我知道我忽略了一些简单的事情。
-
哦,我明白了,这就解释了为什么你可以访问它。如果您需要显示枚举中的所有数据,我仍然宁愿使用
Enums.Pressures.values(),而不是列表(顺便说一下,列表是空的,因为它似乎没有被任何东西初始化)。 -
@Zhu 谢谢,我知道它现在是空的。如果我只想显示某些数据字段而不是每个枚举拥有的所有数据,我会遇到任何问题吗?
-
从技术上讲
values()为您提供了数组中特定枚举的每个枚举值,因此如果您需要限制显示的数量,您可能应该坚持使用列表并使用任何一个初始化它需要。
标签: java android listview android-listview enums