这是在自定义 ArrayAdapter 中使用 SQLite select 查询在 android 中滚动列表视图时检查每一行的好习惯吗
Is this a good practice to use SQLite select query in custom ArrayAdapter to check each and every row while scrolling list view in android
我在下面有一个数组列表,其中数据来自云服务器。
protected ArrayList<ProductList> doInBackground(String... params) {
String sObject = params[0];
String result=null;
String mUrl = "http://xxxxxxxxxxxxxx";
ClassReadingWritngApi data_class = new ClassReadingWritngApi();
String Mydata = data_class.SendData(mUrl,sObject);
if (Mydata != null) {
try {
pList= new ArrayList<>();
JSONArray MainArray = new JSONArray(Mydata);
JSONObject Obj0 = MainArray.getJSONObject(0);
String Data = Obj0.getString("ReturnData");
JSONObject jsonObj = new JSONObject(Data);
JSONArray Arr = jsonObj.getJSONArray("GetHdItems");
for (int i = 0; i < Arr.length(); i++) {
JSONObject jsonObject = Arr.getJSONObject(i);
pList.add(new ProductList(jsonObject.getString("stock_code"),jsonObject.getString("stock_desc"),
jsonObject.getString("imglink"),"0"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return pList;
}
我还有一些数据存储在本地数据库 (SQLite) 中,我需要将其与上面的数组列表结合起来进行交叉检查。
因此,我正尝试在自定义 ArrayAdapter 中从 SQLite 获取数据,如下所示。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
..........
final ProductList Item = pList.get(position);
holder.Item_desc.setText(Item.getItemDesc());
String imglink = Item.getImgLink();
final String StockCode = Item.getItemCode();
String QtyNow = Item.getOrderQty();
String oldQty = db.mSelectOrederQty(StockCode);
if (oldQty == null)
oldQty="0";
final String mQty;
if (QtyNow != "0"){
mQty = QtyNow;
}else{
mQty = oldQty;
}
holder.txt_qty.setText(mQty);}
它工作正常,但似乎这是错误的,因为在滚动列表视图时,每次调用 SQLite。
请建议是否可以或任何其他解决方案。
在获取视图中进行任何数据库操作不是一个好习惯。因为它不是为它而设计的,它会减慢渲染和滚动的速度。
我建议遍历产品并填充 oldstockquantiy Hashmap,键为 StockCode,值为 oldQuantity。当您最初将适配器设置为列表视图时,您可以这样做。因此,您可以通过提供 StockCode 从 Hashmap 中读取,而不是在获取视图中从数据库中读取,这要快得多。
还有许多其他方法,但这种方法更简单。
希望对你有所帮助
我在下面有一个数组列表,其中数据来自云服务器。
protected ArrayList<ProductList> doInBackground(String... params) {
String sObject = params[0];
String result=null;
String mUrl = "http://xxxxxxxxxxxxxx";
ClassReadingWritngApi data_class = new ClassReadingWritngApi();
String Mydata = data_class.SendData(mUrl,sObject);
if (Mydata != null) {
try {
pList= new ArrayList<>();
JSONArray MainArray = new JSONArray(Mydata);
JSONObject Obj0 = MainArray.getJSONObject(0);
String Data = Obj0.getString("ReturnData");
JSONObject jsonObj = new JSONObject(Data);
JSONArray Arr = jsonObj.getJSONArray("GetHdItems");
for (int i = 0; i < Arr.length(); i++) {
JSONObject jsonObject = Arr.getJSONObject(i);
pList.add(new ProductList(jsonObject.getString("stock_code"),jsonObject.getString("stock_desc"),
jsonObject.getString("imglink"),"0"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return pList;
}
我还有一些数据存储在本地数据库 (SQLite) 中,我需要将其与上面的数组列表结合起来进行交叉检查。 因此,我正尝试在自定义 ArrayAdapter 中从 SQLite 获取数据,如下所示。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
..........
final ProductList Item = pList.get(position);
holder.Item_desc.setText(Item.getItemDesc());
String imglink = Item.getImgLink();
final String StockCode = Item.getItemCode();
String QtyNow = Item.getOrderQty();
String oldQty = db.mSelectOrederQty(StockCode);
if (oldQty == null)
oldQty="0";
final String mQty;
if (QtyNow != "0"){
mQty = QtyNow;
}else{
mQty = oldQty;
}
holder.txt_qty.setText(mQty);}
它工作正常,但似乎这是错误的,因为在滚动列表视图时,每次调用 SQLite。 请建议是否可以或任何其他解决方案。
在获取视图中进行任何数据库操作不是一个好习惯。因为它不是为它而设计的,它会减慢渲染和滚动的速度。
我建议遍历产品并填充 oldstockquantiy Hashmap,键为 StockCode,值为 oldQuantity。当您最初将适配器设置为列表视图时,您可以这样做。因此,您可以通过提供 StockCode 从 Hashmap 中读取,而不是在获取视图中从数据库中读取,这要快得多。
还有许多其他方法,但这种方法更简单。
希望对你有所帮助