我需要在listview中向DB显示查询结果。我将返回查询2个值(" cod"," value")。我想过使用SimpleAdapter来解决问题,但它没有用。
这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.techcharacteristic);
PopulateTechCharacteristicList populateList = new PopulateTechCharacteristicList(
this);
populateList.execute();
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.techcharacteristic_rows,
new String[] {"cod", "value"}, new int[] {
R.id.techCharacteristic, R.id.techCharacteristicName });
setListAdapter(adapter);
}
public class PopulateTechCharacteristicList extends
AsyncTask<Integer, String, Integer> {
ProgressDialog progress;
Context context;
public PopulateTechCharacteristicList(Context context) {
this.context = context;
}
protected void onPreExecute() {
progress = ProgressDialog.show(TechCharacteristicList.this,
getResources().getString(R.string.Wait), getResources()
.getString(R.string.LoadingOperations));
}
protected Integer doInBackground(Integer... paramss) {
ArrayList<TechCharacteristic> arrayTechChar = new ArrayList<TechCharacteristic>();
TechCharacteristicWSQueries techCharWSQueries = new TechCharacteristicWSQueries();
try {
arrayTechChar = techCharWSQueries
.selectTechCharacteristicByAsset("ARCH-0026");
HashMap<String, String> temp = new HashMap<String,
for(TechCharacteristic strAux : arrayTechChar)
{
temp.put("cod", strAux.getTechCharacteristic() + " - " + strAux.getTechCharacteristicName());
temp.put("value", strAux.getTechCharacteristicValue());
list.add(temp);
}
} catch (QueryException e) {
e.printStackTrace();
return 0;
}
return 1;
}
protected void onPostExecute(Integer result) {
if(result == 1)
progress.dismiss();
}
}
由于使用相同的代码(&#34; cod&#34;,&#34; value&#34;)来包含HashMap中的值,我的listView始终显示最后插入的项目。但是在我的声明中,SimpleAdapter使用硬编码(&#34; cod&#34;,&#34;值&#34;),每当我放置除(&#34; cod&#34;,& #34;值&#34;)在HashMap中,listView带有空。
任何人都可以帮助我吗?
答案 0 :(得分:2)
由于使用相同的代码(“cod”,“value”)而来 包含HashMap中的值,我的listView始终显示 最后一项插入。
当您从HashMap
创建for loop
对象时,仅在for循环中向该对象添加值,因此先前的值将被删除,并且您只获得最后的值。
要解决此问题,您需要在HashMap
中创建与每行对应的for loop
对象。
尝试
HashMap<String, String> temp = null;
for(TechCharacteristic strAux : arrayTechChar)
{
temp = new HashMap<String,String>();
temp.put("cod", strAux.getTechCharacteristic() + " - " + strAux.getTechCharacteristicName());
temp.put("value", strAux.getTechCharacteristicValue());
list.add(temp);
}