2014-04-19 6 views
0

일부 항목이있는 ListView가 있지만 데이터베이스를 업데이트 한 후 ListView를 "새로 고침"하고 싶습니다. 누구든지 나를 도울 수 있습니까?ListView 새로 고침 항목

편집 : populateListView이

public void populateListView() 
{ 
    String URL = config.getUrl_For_Query() + ",&nameq=Select&tipo=select"; // my URL 
    String jsonString = reading.execute_query(URL); // jsonString is formatted well 

    try 
    { 
     JSONObject jsonResponse = new JSONObject(jsonString); 
     JSONArray array = jsonResponse.getJSONArray("elenco"); 
     for (int i=0; i<array.length(); i++) // I scan all array 
     { 
      JSONObject nameObj = (JSONObject)array.get(i); 
      // I retrieve all information 
      allNames.add(nameObj.getString("name")); // Name 
      allLng.add(nameObj.getString("lng")); // Another information 

     } 
    } 
    catch (Exception e) 
    { e.printStackTrace(); } 

    List<String> Array = new ArrayList<String>(); 
    for(int i=0;i<allNames.size();i++) // I add all values 
    { 
     String value = allNames.get(i).toString() + ", \n\t" + allLng.get(i).toString(); 
     Array.add(value); // here I populate my Array 

    } 

    final ListView listView = (ListView) getActivity().findViewById(R.id.List); 
    listView.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Array)); 

    // 
    // Click 
    // 

} 

saveChanges를

public void saveChanges() 
{ 
    // I update a Database 
    // And then I'd like to refresh ListView's items 
    populateListView(); // Update ListView 
} 
+0

그리고 정확히 무엇이 문제입니까? –

+0

문제는 새 항목이 다른 항목 아래에 추가된다는 것입니다. – 1megg1

+0

매번 새로운 '어댑터'를 만들어 문제가 배열을 채우는 방법에 있어야합니다. 코드를 입력하십시오. 나는 우리가 그곳에서 오류를 발견 할 것이라고 확신한다. –

답변

0

Comparator 사용 ListView에 항목을 추가 할 수 있습니다. 여기서 비교 대상과 방법을 정의하면 compare() 메서드에서 인스턴스 중 두 개에서 반환 할 내용을 정의합니다. 다음은 StringComparator의 예입니다. 당신이 항목을 추가 할 때

Comparator myComparator = new Comparator<String>() { 
    public int compare(final String user1, final String user2) { 
    // This would return the ASCII representation of the first character of each string 
    return (int) user2.charAt(0) - (int) user1.charAt(0); 
    }; 
}; 

adapter.sort(myComparator); 

이 방법, 당신은 전체 Adapter을 다시하지 않지만 대신 정렬됩니다. 그러나 어댑터에 .notifyDataSetChanged()으로 전화하는 것을 잊지 마세요. 그러면 레이아웃을 새로 고침 할 수 있습니다.

+0

나는 낡은 아이템을 원하지 않지만 새로운 것은 saveChanges()로 얻는다. 그리고 .notifyDataSetChanged()를 어디에 추가해야합니까? – 1megg1

+0

'Adapter'를 변경 한 후에는'adapter.notifyDataSetChanged()'를 호출하십시오. – nKn

0

ArrayAdapter.insert 메서드를 사용하여 특정 인덱스에 개체를 삽입 해보십시오. 처음에는

+0

http://developer.android.com/reference/android/widget/ArrayAdapter.html#insert(T, int) –

0

안드로이드에 SQLite 데이터베이스에 대한 this 자습서를보십시오. 문제는 새 항목이 목록의 끝에 추가된다는 것입니다. 응? 이는 배열 변경에서 Adapter을 알리지 않았기 때문입니다.
ArrayAdapter<String> Adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Array);
첫 번째 해결 방법은 데이터베이스를 업데이트하기 전에 어댑터를 지우는 것입니다. Sth처럼 :
Adapter.clear(); 할 수 있습니다. 이렇게하면 데이터베이스를 업데이트하고 새 항목을 삽입하기 전에 어댑터가 비어 있습니다. 변경 사항에 대해서는 awaring 어댑터에 Adapter.notifyDataSetChanched();을 사용할 수 있습니다.
위의 튜토리얼에는 맞춤 어댑터가 있습니다. 그것은이 코드를 사용

List<String> Array = new ArrayList<String>();
Array = (ArrayList<String>) db.getAllContacts();
Adapter = new MyCustomAdapter(getActivity() [in fragment case or getApplicationContext() in Activity case], R.layout.simple_list_item_1, Array);

이 방법은 자동으로 해당 작업을 수행하기 때문에 어댑터를 지우는 필요가 없습니다. 이 방법을 사용할 때마다 업데이트 된 어댑터가 목록을 표시하는 데 사용됩니다.