gpt4 book ai didi

java - ListView 没有出现在我的应用程序的 MainActivty 中(附图片)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:31:52 28 4
gpt4 key购买 nike

在我的 Android 应用程序中保存笔记后,笔记的笔记(或 ListView)没有出现在 MainActivity 中。我的应用的 MainActivity 类是:

package com.twitter.i_droidi.mynotes;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {

ListView lv;
NotesDataSource nDS;
List<NotesModel> notesList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

nDS = new NotesDataSource(this);
lv = (ListView) findViewById(R.id.lv);

nDS.open();
notesList = nDS.getAllNotes();
nDS.close();

String[] notes = new String[notesList.size()];

for (int i = 0; i < notesList.size(); i++) {
notes[i] = notesList.get(i).getTitle();
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,
android.R.id.text1, notes);
lv.setAdapter(adapter);

registerForContextMenu(lv);
lv.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent nView = new Intent(this, Second.class);
nView.putExtra("id", notesList.get(position).getId()); // Check...!!!
startActivity(nView);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_delete, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
nDS.open();
nDS.deleteNote(lv.getId()); // Check...!!!
nDS.close();
Toast nDelete = Toast.makeText(this, "Deleted.", Toast.LENGTH_LONG);
nDelete.show();
return true;
}
return super.onContextItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mainMenuNewNote:
Intent nNote = new Intent(this, Second.class);
startActivity(nNote);
return true;

case R.id.mainMenuAbout:
AlertDialog.Builder aboutDialog = new AlertDialog.Builder(this);
aboutDialog.setTitle("About the app");
aboutDialog.setMessage("The Simplest app for notes!\n\n" +
"Developed by: Abdulaziz\n" +
"Twitter: @i_Droidi\n" +
"Telegram: MrGlitch\n\n" +
"Special Thanks to who tested the app before upload it on Play Store and to who use it now! :)");
aboutDialog.setIcon(R.mipmap.ic_launcher);
aboutDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface aboutDialog, int witch) {
// Do Not Do Anything.
}
});

aboutDialog.show();
return true;

case R.id.mainMenuExit:
AlertDialog.Builder exDialog = new AlertDialog.Builder(this);
exDialog.setTitle("Exit?");
exDialog.setMessage("Are you sure to exit?");
exDialog.setIcon(R.mipmap.ic_launcher);
exDialog.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface exDialog, int which) {
finish();
}
});
exDialog.setPositiveButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface exDialog, int which) {
// Do Not Do Anything.
}
});

exDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
}

我的应用程序的activity_main(xml/布局文件)是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>

</LinearLayout>

我的应用的第二类是:

package com.twitter.i_droidi.mynotes;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class Second extends ActionBarActivity {

NotesDataSource nDS;
EditText noteTitle;
EditText noteBody;
int id;
DB db;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);

Intent in = getIntent();
id = in.getIntExtra("id", 0);

noteTitle = (EditText) findViewById(R.id.note_title);
noteBody = (EditText) findViewById(R.id.note);
nDS = new NotesDataSource(this);

nDS.open();
NotesModel note = nDS.getNote(id);
nDS.close();

noteTitle.setText(note.getTitle());
noteBody.setText(note.getBody());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_second, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.secondMenuSave:
if (!noteTitle.getText().toString().isEmpty() && !noteBody.getText().toString().isEmpty()) {
nDS.open();
nDS.updateNote(id, noteTitle.getText().toString(), noteBody.getText().toString());
nDS.close();
Toast nSave = Toast.makeText(this, "Saved.", Toast.LENGTH_LONG);
nSave.show();
finish();
} else {
Toast notSave = Toast.makeText(this, "The title and content of the note CANNOT be empty!", Toast.LENGTH_LONG);
notSave.show();
}
return true;

case R.id.secondMenuBack:
AlertDialog.Builder baDialog = new AlertDialog.Builder(this);
baDialog.setTitle("Back?");
baDialog.setMessage("Do you want to back to the main page before saving the note?");
baDialog.setIcon(R.mipmap.ic_launcher);
baDialog.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface baDialog, int which) {
finish();
}
});
baDialog.setPositiveButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface baDialog, int which) {
// Do Not Do Anything.
}
});

baDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
}

我的应用的 NotesDataSource 类是:

package com.twitter.i_droidi.mynotes;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class NotesDataSource {

DB myDB;
SQLiteDatabase sql;

String[] getAllColumns = new String[]{DB.ID, DB.TITLE, DB.BODY};

public NotesDataSource(Context context) {
myDB = new DB(context);
}

public void open() {
try {
sql = myDB.getWritableDatabase();
} catch (Exception ex) {
Log.d("Error in your database!", ex.getMessage());
}
}

public void close() {
sql.close();
}

public void createNote(String title, String body) {
ContentValues note = new ContentValues();
note.put(myDB.TITLE, title);
note.put(myDB.BODY, body);
sql.insert(myDB.TABLE_NAME, null, note);
}

public NotesModel getNote(int id) {
NotesModel note = new NotesModel();

Cursor cursor = sql.rawQuery("SELECT * FROM " + DB.TABLE_NAME + " WHERE " + DB.ID + " = ?", new String[]{id + ""});

if (cursor.getCount() > 0) {
cursor.moveToFirst();
note.setId(cursor.getInt(0));
note.setTitle(cursor.getString(1));
note.setBody(cursor.getString(2));
cursor.close();
}
return note;
}

public void updateNote(int id, String title, String body) {
ContentValues note = new ContentValues();
note.put(myDB.TITLE, title);
note.put(myDB.BODY, body);
sql.update(myDB.TABLE_NAME, note, myDB.ID + " = " + id, null);
}

public void deleteNote(Object id) {
sql.delete(myDB.TABLE_NAME, myDB.ID + " = " + id, null);
}

public List<NotesModel> getAllNotes() {
List<NotesModel> notesList = new ArrayList<NotesModel>();

Cursor cursor = sql.query(myDB.TABLE_NAME, getAllColumns, null, null, null, null, null);
cursor.moveToFirst();

while (!cursor.isAfterLast()) {
NotesModel notes = new NotesModel();
notes.setId(cursor.getInt(0));
notes.setTitle(cursor.getString(1));
notes.setBody(cursor.getString(2));

notesList.add(notes);
cursor.moveToNext();
}

cursor.close();
return notesList;
}
}

我的应用的数据库类是:

package com.twitter.i_droidi.mynotes;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DB extends SQLiteOpenHelper {

private static final String DB_NAME = "MyNotes";
private static final int DB_VERSION = 1;

public static final String TABLE_NAME = "MyNotes";
public static final String ID = "id";
public static final String TITLE = "title";
public static final String BODY = "body";

private static final String DB_CREATE = "create table " + TABLE_NAME + " (" + ID + " integer primary key autoincrement, " +
TITLE + " text not null, " + BODY + " text not null)";

public DB(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}

我的应用 (MainActivity) 的屏幕截图,保存笔记后(没有显示!):

我该如何解决这个问题?!

或者任何人都可以写出正确的代码?!

谢谢你帮助我。

最佳答案

问题是 onCreate 在生命周期中只被调用一次。当您切换 Activity 时,Main 只是隐藏并暂停。当您返回 Activity 时,onCreate 不会再次触发。

覆盖 onStart 方法并在其中设置您的列表适配器。当您返回到 View 时,它将在 onCreate() 之后立即被调用。这是 Android 生命周期:

http://developer.android.com/training/basics/activity-lifecycle/starting.html

关于java - ListView 没有出现在我的应用程序的 MainActivty 中(附图片),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30813978/

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