Pregunta

I need to show in a listview a query result to the DB. I'm returning the query 2 values ​​("cod", "value"). I thought of using a SimpleAdapter to solve the problem, but it did not work.

this is my code:

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();
    }
}

On account of being utlizando the same codes ("cod", "value") to include values ​​in the HashMap, my listView is always showing the last item inserted. But in my statement SimpleAdapter'm using hard coded ("cod", "value") and whenever I put any value other than ("cod", "value") in the HashMap, the listView carries empty.

Can anyone help me?

¿Fue útil?

Solución

On account of being utlizando the same codes ("cod", "value") to include values ​​in the HashMap, my listView is always showing the last item inserted.

As you are creating HashMap object out of for loop, and adding values to that object only in the for loop,hence the previous values get erased and you get only last values.

To solve this you need to create the HashMap object in for loop corresponding to each row.

Try

        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);
        }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top