1

我需要在列表视图中向数据库显示查询结果。我正在返回查询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();
    }
}

由于 utlizando 使用相同的代码(“cod”、“value”)在 HashMap 中包含值,我的 listView 总是显示最后插入的项目。但是在我的声明中,SimpleAdapter'm 使用硬编码(“cod”,“value”),并且每当我在 HashMap 中放入除(“cod”,“value”)以外的任何值时,listView 都为空。

谁能帮我?

4

1 回答 1

2

由于 utlizando 使用相同的代码(“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);
        }
于 2013-09-03T11:58:59.300 回答