2013-09-03 2 views
1

DB에 질의 결과를리스트 뷰에 표시해야합니다. 쿼리 2 값 ("cod", "value")을 반환합니다. SimpleAdapter를 사용하여 문제를 해결할 생각 이었지만 작동하지 않았습니다.하위 항목이있는 ListView입니다. (안드로이드)

는 ("대구는", "값")은 HashMap의 값을 포함하는 동일한 코드 utlizando되는 계정에
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(); 
    } 
} 

, 내 목록보기는 항상 마지막 항목을 보여주고있다 :

내 코드입니다 삽입. 그러나 하드 코드 된 ("cod", "value")를 사용하는 내 진술 SimpleAdapter에서 및 ("cod", "value"이외의 다른 값을 HashMap에 둘 때마다 listView는 비어 있습니다.

아무도 도와 줄 수 있습니까? 에 동일한 코드 ("대구", "값") utlizando되는 계정에

+1

을 시도하고 같은지도를 사용하지 목적. – Luksprog

+0

postexecute() 메소드에서 어댑터 및 setListAdapter를 초기화 하시겠습니까? – SKT

+0

해결됨, thx 얘들 아! – user1792343

답변

2

은 HashMap의의 값은, 내 목록보기에 항상 삽입 된 마지막 항목을 보여주고 있습니다.

for loop에서 HashMap 객체를 생성하고, 루프 만에 그 객체에 값을 추가 할 때, 따라서 이전 값이 지워 얻을 당신은 단지 마지막 값을 얻을.

이 문제를 해결하려면 각 행에 해당하는 for loopHashMap 개체를 만들어야합니다.

** 당신은 ** 새로운 **`HashMap` **마다 내리는 데 필요한`doInBackground` 방법에 그`for` 루프에서

 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); 
     } 
관련 문제