2012-06-15 42 views
-1

我已經得到了使用自定義BaseAdapter(MyBaseAdapter)填充一個ListView:添加一個新行到ListView使得它上面的那些消失

public class MyBaseAdapter extends BaseAdapter 
{ 

    private View renderer; 

    List<MyItemModel> items; 

    public MyBaseAdapter(View renderer) { 
     this.renderer = renderer; 
    } 

    public void setModel(List<MyItemModel> items) 
    { 

     this.items = items; 
     notifyDataSetChanged(); 
    } 


    @Override 
    public int getCount() 
    { 

     return items != null ? items.size() : 0; 
    } 


    @Override 
    public Object getItem(int position) 
    { 

     return items != null ? items.get(position) : null; 
    } 


    @Override 
    public long getItemId(int position) 
    { 

     return items != null ? items.get(position).id : -1; 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 

     if (convertView == null) 
     { 
      convertView = renderer; 
     } 
     MyItemModel item = items.get(position); 
     // replace those R.ids by the ones inside your custom list_item layout. 
     TextView timeText = (TextView) convertView.findViewById(R.id.timeText); 
     timeText.setText(item.time); 

     ToggleButton button = (ToggleButton) convertView.findViewById(R.id.toggleButton); 
     button.setOnClickListener(item.listener); 

     ImageView coinImage = (ImageView) convertView.findViewById(R.id.coinImage); 
     coinImage.setImageResource(R.drawable.ic_refresh); 

     return convertView; 
    } 

} 

它使用這些物品

public class MyItemModel 
{ 

String time; 
long id; 

OnClickListener listener = new OnClickListener(){ 
    @Override 
    public void onClick(View v) { 
     Log.d("app", "whatever"); 
    } 
}; 

} 
填充

我從我的主類使用代碼添加項目是這樣的:

private void setUpList() 
{ 

    myListModel = new ArrayList<MyItemModel>(); 

    MyItemModel item = new MyItemModel(); 
    item.time = "12:00"; 
    item.id = 0; 
    myListModel.add(item); 

    //if I add a few more items here, it still only shows the bottom one 

    LayoutInflater li = getLayoutInflater(); 
         //alarm_list_item is a table row 
    View view = li.inflate(R.layout.alarm_list_item, null); 

    adapter = new MyBaseAdapter(view); 
    adapter.setModel(myListModel); 
    setListAdapter(adapter); 
    listView = getListView(); 
    listView.setTextFilterEnabled(true); 

} 

而且工作正常unti l當發生這種情況時,我將其他項目添加到列表中(注意上面的行是空白的。如果我添加多行,除最後一行之外的所有行都是空白的。)

任何想法發生了什麼?

screenshot

回答

1

我不知道你的問題背景,但是會員渲染器在你的Adapter接縫有點奇怪。它使所有項目視圖在列表視圖中創建的第一個itemView。

我想你可以嘗試修改getView(INT位置,查看convertView,一個ViewGroup父)方法如下:

public class MyBaseAdapter extends BaseAdapter 
{ 

    //private View renderer; 
    private Context mContext; 


    List<MyItemModel> items; 

    public MyBaseAdapter(Context context) { 
     this.mContext = context; 
    } 

    public void setModel(List<MyItemModel> items) 
    { 

     this.items = items; 
     notifyDataSetChanged(); 
    } 


    @Override 
    public int getCount() 
    { 

     return items != null ? items.size() : 0; 
    } 


    @Override 
    public Object getItem(int position) 
    { 

     return items != null ? items.get(position) : null; 
    } 


    @Override 
    public long getItemId(int position) 
    { 

     return items != null ? items.get(position).id : -1; 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 

     if (convertView == null) 
     { 
      LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = li.inflate(R.layout.alarm_list_item, null); 
     } 
     MyItemModel item = items.get(position); 
     // replace those R.ids by the ones inside your custom list_item layout. 
     TextView timeText = (TextView) convertView.findViewById(R.id.timeText); 
     timeText.setText(item.time); 

     ToggleButton button = (ToggleButton) convertView.findViewById(R.id.toggleButton); 
     button.setOnClickListener(item.listener); 

     ImageView coinImage = (ImageView) convertView.findViewById(R.id.coinImage); 
     coinImage.setImageResource(R.drawable.ic_refresh); 

     return convertView; 
    } 

} 

,並在您setUpList(),通過您的主機活動作爲參數構建您的適配器時:

private void setUpList(){ 

    MyItemModel item = new MyItemModel(); 
    item.time = "12:00"; 
    item.id = 0; 
    myListModel.add(item); 

    LayoutInflater li = getLayoutInflater(); 
        //alarm_list_item is a table row 
    View view = li.inflate(R.layout.alarm_list_item, null); 

    //adapter = new MyBaseAdapter(view); 
    adapter = new MyBaseAdapter(Activity.this); 
    adapter.setModel(myListModel); 
    setListAdapter(adapter); 
    listView = getListView(); 
    listView.setTextFilterEnabled(true); 

} 

可以將它有助於

+0

非常感謝!我想我需要更多地瞭解適配器和渲染器等等! –

0

你可能要移動myListModel = new ArrayList<MyItemModel>();setUpList()方法外,你所擁有的方式,您要創建一個新的數組每次你打電話setUpList()這樣你只需要在陣列中的全部時間一項,如果將數組聲明移動到方法外部,則會將項目正確添加到陣列中

myListModel = new ArrayList<MyItemModel>(); 

private void setUpList(){ 

    MyItemModel item = new MyItemModel(); 
    item.time = "12:00"; 
    item.id = 0; 
    myListModel.add(item); 

    LayoutInflater li = getLayoutInflater(); 
        //alarm_list_item is a table row 
    View view = li.inflate(R.layout.alarm_list_item, null); 

    adapter = new MyBaseAdapter(view); 
    adapter.setModel(myListModel); 
    setListAdapter(adapter); 
    listView = getListView(); 
    listView.setTextFilterEnabled(true); 

} 
+0

謝謝你的提示,但我不認爲這是造成問題的原因。即使我複製'myListModel.add(item);'幾行,仍然會導致問題。 –

5

您究竟在幹什麼? /試圖做什麼?

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 

    if (convertView == null) 
    { 
     convertView = renderer; 
    } 
    //...// 
} 

只有你的最後一個項目才能得到渲染的視圖!

嘗試添加100個項目(啓用滾動)並滾動!你會看到它有多棒。


解決方案: 你將不得不擡高每次你convertView是空的新視圖。返回相同的視圖將顯示調用getView的最後一個位置。

+0

謝謝你指出我正確的方向,但宋的回答最終幫助我得到它的工作。 –

相關問題