2014-07-06 2 views
0

목록보기 항목을 끝에 추가하려고합니다. ListView 문제는 이전 목록을 대체한다는 것입니다. 이전에이 프로 시저 내에서 listview 변수를 정의했지만 지금은 공용으로 설정했지만 새 목록을 이전 목록에 추가하지는 않습니다. 내가 여기서 잘못하고있는 것은 무엇입니까? 내가 .notifyDataSetChanged()을 사용하도록 제안 그러나 여기안드로이드는 목록보기에 추가

@Override 
protected SimpleAdapter doInBackground(String... xmlData) { 
    StringReader reader = new StringReader(xmlData[0]); 

    MsgsXmlParser msgsXmlParser = new MsgsXmlParser(); 

    List<HashMap<String, String>> msgs = null; 

    try{ 
     msgs = msgsXmlParser.parse(reader); 
    }catch(Exception e){ 
     Log.d("Exception",e.toString()); 
    } 

    String[] from = {"Fld1", "Fld2","Fld3"}; 
    int[] to = {R.id.tvFld1, R.id.tvFld2, R.id.tvFld3}; 

    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), msgs, R.layout.lv_layout, from, to); 

    return adapter; 
} 

@Override 
protected void onPostExecute(SimpleAdapter adapter) { 

    lvMsgs.setAdapter(adapter); 

} 

및 후 전에 내 코드는 여기에 를 작동하지 않았다으로 시도

SimpleAdapter adapter; 
List<HashMap<String, String>> msgs = null; 
String[] from = {"Fld1", "Fld2","Fld3"}; 
int[] to to = {R.id.tvFld1, R.id.tvFld2, R.id.tvFld3}; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main); 

    try { 
     adapter = new SimpleAdapter(getBaseContext(), msgs, R.layout.lv_layout, from, to); 
} 



    @Override 
    protected SimpleAdapter doInBackground(String... xmlData) { 
     StringReader reader = new StringReader(xmlData[0]); 

     MsgsXmlParser msgsXmlParser = new MsgsXmlParser(); 


     try{ 
      msgs = msgsXmlParser.parse(reader); 
     }catch(Exception e){ 
      Log.d("Exception",e.toString()); 
     } 

     return adapter; 
    } 

    @Override 
    protected void onPostExecute(SimpleAdapter adapter) { 

     adapter.notifyDataSetChanged(); 
     lvMsgs.setAdapter(adapter); 
    } 

답변

1

당신의 SimpleAdapter 아마이 포함 (어댑터가 null) lvMsgs ListView 안에 표시 할 개체 목록에 대한 참조입니다. ListView data not showing on android sdk

// Lets assume you have some list of msgs and a adapter 
// You could store those as class variables 
List<String> msgs = new ArrayList<String>(); 
SimpleAdapter adapter = new SimpleAdapter(context, msgs, R.layout.lv_layout, from, to); 

// Point ListView to use previously created adapter 
ListView lvMsgs = (ListView) findViewById(R.id.lvMsgs); 
lvMsgs.setAdapter(adapter); 

// later in code do some updates to the list of objects 
// and notify the adapter that it should refresh its content 
msgs.add("SomeString"); 
adapter.notifyDataSetChanged(); 

호프 어떻게 든 도움이 : 당신이해야 할 무엇

개체의 목록에 새 항목을 추가 한 다음, 예를 들어 여기에 설명 된대로 어댑터notifyDataSetChanged를 호출하는 것입니다.

참고 사항 : ListView는 중 하나의 어댑터 만을 사용할 수 있습니다. 따라서 msgs 목록을 업데이트하고 표시되는 내용을 업데이트하도록 어댑터에 알리는 것이 좋습니다. 데이터가 변경 목록을 알려 드리겠습니다

adapter.notifyDatasetChanged(); 

을하고 목록 그에 따라 항목을 구축 할 것입니다 :

+0

감사합니다. 그러나 새 어댑터 목록을 원래 어댑터에 추가하는 방법은 무엇입니까? adapter = 새로운 SimpleAdapter (getBaseContext(), msgs, R.layout.lv_layout, from, to); – asmgx

+0

이 코드를 사용하여 msgs에 목록을 추가하려고했습니다. msgs.add (msgsXmlParser.parse (reader)); 이 오류가 발생했습니다. add (List >) 메서드에 대해 적절한 메소드를 찾을 수 없습니다. List.add (HashMap )를 적용 할 수 없습니다. – asmgx

+0

// --------- -이 시도했지만 여전히 작동하지 않습니다. SimpleAdapter saAdapter; @Override protected void onPostExecute (SimpleAdapter 어댑터) { saAdapter = 어댑터; if (lvMsgs.getCount() == 0) { lvMsgs.setAdapter (saAdapter); } else { saAdapter.notifyDataSetChanged(); } } – asmgx

0

대신 어댑터를 재설정, 단순히 호출합니다.

관련 문제