gpt4 book ai didi

java - ListView 两次显示列表项

转载 作者:行者123 更新时间:2023-11-30 02:56:41 28 4
gpt4 key购买 nike

我是 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;
}
}

最佳答案

可能是因为您在 onCreate()getLoaderManager().restartLoader( 0, null, this);onResume() 中?

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

关于java - ListView 两次显示列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23153983/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com