2012-08-24 172 views
0

我在更新ListView中的ArrayAdapter中的數據時甚至在listAdapter.notifyDataSetChanged()上更改都沒有反映在ListView中。Android ListView不更新,適配器更改

有趣的是,初始加載是好的,我得到在ListView中呈現的數據的完整列表。這是當我第二次加載活動,其中緩存的ArrayList傳遞給setDataList()方法時不會呈現。生成的ListView僅包含onCreate()方法中的單個默認條目設置。

我已經調試了代碼,並通過可運行的步驟,它似乎運行,因爲我會有預期的,只是沒有最終結果。

如果有人可以幫助我,我真的很感激它,我已經GOOGLE了死吧!

我的代碼片段:

public class CriteriaOriginIncludeActivity extends Activity { 

private CriteriaOriginIncludeController m_controller; 
private CriteriaOriginIncludesAdapter m_listAdapter; 
private ArrayList<OriginRowData> m_listData; 
private ListView m_originList; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.criteria_origin_includes);  
    ArrayList<OriginRowData> list = new ArrayList<OriginRowData>(); 
    list.add(new OriginRowData(new Origin(0,""), false)); 

    m_listAdapter = new CriteriaOriginIncludesAdapter(this, 
                   R.layout.origin_include_list_row, 
                   R.id.lst_origin_include, 
                   list); 

    m_originList = (ListView) this.findViewById(R.id.lst_origin_include); 
    m_originList.setAdapter(m_listAdapter); 

    m_controller = new CriteriaOriginIncludeController(this); 

} 

public void setOriginList(ArrayList<OriginRowData> list) { 

    if (list != null && list.size() > 0) { 
     m_listData = list; 
    } else { 
     m_listData = new ArrayList<OriginRowData>(); 
    } 

    runOnUiThread(returnRes); 
} 

private Runnable returnRes = new Runnable() { 

public void run() { 

    m_listAdapter.clear(); 
    if(m_listData != null && m_listData.size() > 0){ 
     m_listAdapter.notifyDataSetChanged(); 
     for(int i=0;i<m_listData.size();i++) 
      m_listAdapter.add(m_listData.get(i)); 
    } 
    m_listAdapter.notifyDataSetChanged(); 
} 
}; 
} 

和適配器:

public class CriteriaOriginIncludesAdapter extends ArrayAdapter<OriginRowData> { 
private ArrayList<OriginRowData> m_items; 
    private Context m_context; 

    public CriteriaOriginIncludesAdapter(Context context, int resourceId, int   textViewResourceId, ArrayList<OriginRowData> list) { 
     super(context, resourceId, textViewResourceId, list); 
     this.m_context = context; 
     m_items = list; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
     LayoutInflater vi = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.origin_include_list_row, null); 
     } 
     OriginRowData o = m_items.get(position); 
     if (o != null) { 
     TextView txtName = (TextView) v.findViewById(R.id.txt_origin_include_name); 
     TextView txtId = (TextView) v.findViewById(R.id.txt_origin_include_id); 
     CheckBox chkOrigin = (CheckBox) v.findViewById(R.id.chk_origin); 

     if (txtName != null) { 
      txtName.setText(o.getOrigin().getOrigin());        
     } 
     if (txtId != null) { 
      txtId.setText(Integer.toString(o.getOrigin().getId()));        
     } 
     if (chkOrigin != null) { 
      chkOrigin.setChecked(o.getInclude()); 

      chkOrigin.setOnClickListener(new OriginIncludeOnClickListener(position)); 
     } 

     } 

     return v; 
    } 

} 
+0

TestAdapter可能被破壞或者m_listData.size()是0 – zapl

+0

您可以發佈完整的代碼嗎? –

+0

已發佈完整代碼。另外m_listData.size()大約有100條記錄。調試listAdapter.add()時會多次調用 – DMac

回答

0

你需要重寫ArrayAdapterAdd()方法。

由於您使用自己的私人private ArrayList<OriginRowData> m_items;,所以默認Add()方法不知道它必須將數據添加到此數組列表中。

而在你的getView中你使用的是m_items數組,所以需要更新。默認的Add()方法會將這些項目添加到ArrayAdapter的內置