ListView显示列表项两次

时间:2014-04-18 12:18:37

标签: java android android-listview android-adapter android-loadermanager

我是Android新手。我正在开发一个记事本应用程序.NoteList显示每个列表项两次。我不知道为什么会这样。我认为它与onLoadFinished方法有关,但我不确定。这是我的代码:

** NoteList.java **

public class NoteList extends ListActivity implements LoaderManager.LoaderCallbacks < Cursor > {

private SimpleCursorAdapter dataAdapter;

private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;

private static final int DELETE_ID = Menu.FIRST;
private NotesDbAdapter mDbHelper;
private ArrayList < NoteItem > notes;
private NoteClassAdapter aa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notelist);
    notes = new ArrayList < NoteItem > ();
    int resID = R.layout.notes_row;
    aa = new NoteClassAdapter(this, resID, notes);
    setListAdapter(aa);
    getLoaderManager().initLoader(0, null, this);
    //registerForContextMenu(getListView());
    Button addnote = (Button) findViewById(R.id.addnotebutton);
    addnote.setOnClickListener(new View.OnClickListener() {@
        Override
        public void onClick(View v) {
            createNote();
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}


private void createNote() {
    Intent i = new Intent(this, NoteEdit.class);
    Bundle bundle = new Bundle();
    bundle.putString("mode", "add");
    i.putExtras(bundle);
    startActivityForResult(i, ACTIVITY_CREATE);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Cursor cursor = (Cursor) l.getItemAtPosition(position);
    long row_id = cursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_ROWID);
    Intent i = new Intent(this, NoteEdit.class);
    Bundle bundle = new Bundle();
    bundle.putString("mode", "update");
    bundle.putLong(NotesDbAdapter.KEY_ROWID, row_id);
    i.putExtras(bundle);
    //  i.putExtra(NotesDbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case DELETE_ID:
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        // mDbHelper.deleteNote(info.id);

        Uri uri = Uri.parse(NotesDbAdapter.CONTENT_URI + "/" + info.id);
        getContentResolver().delete(uri, null, null);
        return true;
    }
    return super.onContextItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
}

protected void onResume() {
    super.onResume();
    //Starts a new or restarts an existing Loader in this manager
    getLoaderManager().restartLoader(0, null, this);
}

// This is called when a new Loader needs to be created.
@Override
public Loader < Cursor > onCreateLoader(int id, Bundle args) {
    CursorLoader cursorLoader = new CursorLoader(this,
        NotesDbAdapter.CONTENT_URI, null, null, null, null);
    return cursorLoader;
}

@Override
public void onLoadFinished(Loader < Cursor > loader, Cursor cursor) {

    // dataAdapter.swapCursor(cursor);
    int keyTaskIndex = cursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE);
    notes.clear();
    while (cursor.moveToNext()) {
        NoteItem newItem = new NoteItem(cursor.getString(keyTaskIndex));

        notes.add(newItem);
    }
    aa.notifyDataSetChanged();
}

@Override
public void onLoaderReset(Loader < Cursor > loader) {
    // This is called when the last Cursor provided to onLoadFinished()
    // above is about to be closed.  We need to make sure we are no
    // longer using it.
    //  dataAdapter.swapCursor(null);
}

}

NoteClassAdapter.java

    public class NoteClassAdapter extends ArrayAdapter < NoteItem > {

    int resource;

    private static class ViewHolder {
        TextView text;
        TextView date;
    }

    public NoteClassAdapter(Context context, int resource, ArrayList < NoteItem > items) {
        super(context, resource, items);
        this.resource = resource;
    }

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

        ViewHolder viewholder;
        NoteItem item = getItem(position);

        String taskString = item.getTask();
        Date createdDate = item.getCreated();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
        String dateString = sdf.format(createdDate);

        if (convertView == null) {
            viewholder = new ViewHolder();

            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.notes_row, null);
            viewholder.date = (TextView) convertView.findViewById(R.id.date);
            viewholder.text = (TextView) convertView.findViewById(R.id.text1);
            convertView.setTag(viewholder);
        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }
        viewholder.date.setText(taskString);
        viewholder.text.setText(dateString);
        return convertView;
    }
}

NoteItem.java

public class NoteItem {

 String task;
  Date created;

  public String getTask() {
    return task;
  }

  public Date getCreated() {
    return created;
  }

  public NoteItem(String _task) {
    this(_task, new Date(java.lang.System.currentTimeMillis()));
  }

  public NoteItem(String _task, Date _created) {
    task = _task;
    created = _created;
  }

  @Override
  public String toString() {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
    String dateString = sdf.format(created); 
    return "(" + dateString + ") " + task;
  }
}

1 个答案:

答案 0 :(得分:1)

原因可能是getLoaderManager().initLoader(0, null, this);中的onCreate()getLoaderManager().restartLoader(0, null, this);中的onResume()

我认为getLoaderManager().initLoader(0, null, this);onCreate()就足够了。