目标:应用程序应允许用户为显示的项目添加数量。考虑从所有可能产品的列表中创建产品顺序。产品的代码列表可能会很长(例如成千上万的商品,甚至更多)。另一方面,为该订单选择的商品数量很少(最多几个或几十个)。
已经实现:到目前为止,我已经实现了OrderOverviewActivity
使用LoaderManager
和内容提供者从数据库访问信息的方法。我确实使用MyCursorAdapter
它来扩展CursorAdapter
和覆盖其newView()
andbindView()
方法。
该newView()
还创建了一个ViewHolder
与以子视图,并与从光标位置提取的值的缓冲区引用实例-这样的:
private static class ViewHolder {
public TextView code;
public CharArrayBuffer codeBuffer = new CharArrayBuffer(20);
public TextView name;
public CharArrayBuffer nameBuffer = new CharArrayBuffer(100);
public TextView quantity;
public CharArrayBuffer quantityBuffer = new CharArrayBuffer(10);
}
在newView()
看起来像这样:
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mInflater.inflate(mLayout, parent, false);
ViewHolder holder = new ViewHolder();
holder.code = (TextView) v.findViewById(R.id.code);
holder.name = (TextView) v.findViewById(R.id.name);
holder.quantity = (TextView) v.findViewById(R.id.quantity);
v.setTag(holder);
return v;
}
在bindView()
使用支架的缓冲区,以避免许多的创作String
对象:
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
cursor.copyStringToBuffer(1, holder.codeBuffer); // ProductTable.COLUMN_CODE
cursor.copyStringToBuffer(2, holder.nameBuffer); // ProductTable.COLUMN_NAME
holder.code.setText(holder.codeBuffer.data, 0, holder.codeBuffer.sizeCopied);
holder.name.setText(holder.nameBuffer.data, 0, holder.nameBuffer.sizeCopied);
// The quantity (v for value) from the HashMap<String, String>.
String v = mQuantity.get(String.valueOf(holder.codeBuffer.data, 0,
holder.codeBuffer.sizeCopied));
if (v != null) {
holder.quantity.setText(v);
holder.quantity.setBackgroundColor(Color.DKGRAY);
}
else {
holder.quantity.setText(null);
holder.quantity.setBackgroundColor(Color.TRANSPARENT);
}
}
问题的核心:请注意,数量是通过从HashMap<String, String>
对象中提取的String.valueOf(data, 0, data.size)
。恐怕它会创建一个字符串对象,以后必须对其进行垃圾回收,从而破坏性能。有没有更好的办法从地图上设置数量TextView值?
对于,我不会同意yanchenko的意见CharArrayBuffer
,这在Cursor
基于基础的适配器中使用是一个不错的改进(尤其是当要使用Cursor
许多String
列时)。要摆脱这些多余String
的东西,第一个选择是将其设置为mQuantity
HashMap
无效的键(以及它是类,final
因此您根本无法使其工作)。
识别行的另一种方法是仍然_id
需要的列ListView
。
与此相关的您可以:
有两个HashMap
s都具有一个键Long
(_id)作为键,一个将保存COLUMN_CODE
,另一个将保存实际数量(或一个_id
在代码和代码之间的映射映射,另一个是您的当前映射)。这两个映射将被同步(如果您删除了一个长ID的值,那么也会将其从另一个值中删除,添加值时也是如此)。在该getView()
方法中,您将通过使用_id获得名称Cursor
,但是这将需要自动装箱操作(long
至Long
)。
class DataWrapper {
private HashMap<Long, String> mCodeMapping = new HashMap<Long, String>();
private HashMap<Long, String> mQuantityMapping = new HashMap<String, String>();
public String getCode(long id) {
return mCodeMapping.get(id);
}
public String getQuantity(long id) {
return mQuantityMapping.get(getCode());
}
public String getQuantity(String code) {
return mQuantityMapping.get(code);
}
public void put(long id, String code, String quantity) {
mCodeMapping.put(id, code);
mQuantityMapping .put(code, quantity);
}
}
尽管_id
是,long
并且您有相当数量的值,但您可能会威胁它int
(因为您将处于最大值的边界内int
)。这样做将使您可以简单地将aSparseArray
与自定义类一起使用(在long
和之间进行转换int
):
class WrapperData {
String code; //or use the CharArraybuffer?
String quantity;
}
SparseArray<WrapperData> data = new SparseArray<WrapperData>();
// in the getView() method
WrapperData wd = data.get((int) _id_value); // further use wd.code or wd.quantity
最后一个选择是实现自己的类SparseArray
,这将使您能够在简单的long
键(因此没有自动装箱)到代码和数量值之间建立映射。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句