添加或删除行后复制列表

时间:2011-12-19 16:31:39

标签: android android-listview

我有一个从sql表填充的列表视图。它工作得很好,除了当我尝试删除一行或插入一个新行时它复制所有的东西。

所以在任何改变之前它看起来像是

第1项

第2项

插入或删除1行后,它变为

第1项

第2项

第1项

第2项

每次列表更改时,我都使用fillData来设置适配器。

对于fillData()的第一次调用似乎工作正常,但是成功调用只是读取了现有列表之上的整个列表。

以下是一些代码:

private void fillData() {   
int a=0;
todo = dbHelper.fetchAllTodos();        
startManagingCursor(todo);
todo.moveToFirst();  
while (!todo.isAfterLast()) {           
    String title=todo.getString(todo.getColumnIndexOrThrow(DbTodoAdapter.KEY_SUMMARY));
    String detail=todo.getString(todo.getColumnIndexOrThrow(DbTodoAdapter.KEY_DESCRIPTION));
    String priority=todo.getString(todo.getColumnIndexOrThrow(DbTodoAdapter.KEY_CATEGORY));
    String alarm=todo.getString(todo.getColumnIndexOrThrow(DbTodoAdapter.KEY_ALARM));
    if(alarm!=null) a=1; else a=0;
    if(priority.contains("Urgent"))             
        rd = new RowData(0, title, detail,a,alarm);
    if(priority.contains("Medium"))
        rd = new RowData(1, title, detail,a,alarm);
    if(priority.contains("Low"))
        rd = new RowData(2, title, detail,a,alarm);
    data.add(rd);
    todo.moveToNext();              
}       
CustomAdapter adapter = new CustomAdapter(this, R.layout.todo_list_row, R.id.title, data);      
setListAdapter(adapter);        
getListView().setTextFilterEnabled(true);   
//adapter.notifyDataSetChanged();

}

private class CustomAdapter extends ArrayAdapter<RowData> {

  public CustomAdapter(Context context, int resource, int textViewResourceId, List<RowData> objects) {               
          super(context, resource, textViewResourceId, objects);          
      }              
  @Override
   public View getView(int position, View convertView, ViewGroup parent) {    
      View view = convertView;
      ViewHolder holder=null;
      TextView title = null;
      TextView detail = null;
      ImageView i11=null;
      ImageView i12=null;
      TextView alarm=null;
      RowData rowData= getItem(position);
      if(convertView == null){               
         convertView = mInflater.inflate(R.layout.todo_list_row, null);
   holder = newViewHolder(convertView);                                 
         convertView.setTag(holder);                 
      }else{
       mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
         holder = (ViewHolder) convertView.getTag();
      }
         title = holder.gettitle();
         title.setText(rowData.mTitle);
         detail = holder.getdetail();
                                                                                                    detail.setText(rowData.mDetail);              
         i11=holder.getImage();
         i11.setImageResource(imgid[rowData.mId]);
         i12=holder.getImageAl();
         i12.setImageResource(imgida[rowData.mIda]);
         alarm = holder.getalarm();
         alarm.setText(rowData.mAlarm);                
         return convertView;
  }

1 个答案:

答案 0 :(得分:0)

在fillData()的开头,执行以下操作..

data.clear();