编辑 Android 日记条目

Posted

技术标签:

【中文标题】编辑 Android 日记条目【英文标题】:Editing Android diary entries 【发布时间】:2021-10-04 16:51:22 【问题描述】:

只是一个简单的问题,我正在创建一个 android 日记应用程序,其中主页包含三个按钮:“新建”用于创建新条目,“全部”用于查看数据库中的所有条目,以及“搜索”搜索匹配某些文本的条目。

当我单击“全部”时,它会将我带到一个新的 Intent,它在 ListView 中显示所有条目。但是,当我单击一个单元格时,它会让我回到“新建”将引导我的意图,但它不能编辑条目,它只显示空白字段,如果我输入,只会创建另一个条目在列表视图中。有人对如何做到这一点有任何想法吗?代码如下:

./Diary Main 用于主页

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

public class DiaryMain extends AppCompatActivity 

    private Button showEntries, newEntry, searchEntries;


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

        showEntries = findViewById(R.id.displayAllBtn);
        newEntry = findViewById(R.id.newEntryBtn);
        searchEntries = findViewById(R.id.searchBtn);

        loadFromDBToMemory();

    


    public void displayEntries(View view) 
        Intent btnClick = new Intent(this, DiaplayAllEntries.class);
        startActivity(btnClick);
    

    public void newEntry(View view) 
        Intent btnClick = new Intent(this, NoteDetailActivity.class);
        startActivity(btnClick);
    

    public void searchEntries(View view) 
        Intent btnClick = new Intent(this, SearchEntries.class);
        startActivity(btnClick);
    

    private void loadFromDBToMemory() 
        SecondDataBaseHelper secondDataBaseHelper = SecondDataBaseHelper.databaseInstance(this);

        secondDataBaseHelper.populateDiaryListArray();
    


./NoteDetailActivity,用户可以在其中添加新的日记条目:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

public class NoteDetailActivity extends AppCompatActivity 

    private EditText editID, editSubject, editContent, editDate, editLatitude, editLongitude;
    private Diary selectedDiary;
    SecondDataBaseHelper secondDataBaseHelper;

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

        editID = findViewById(R.id.IDEditText);
        editSubject = findViewById(R.id.SubjectEditText);
        editContent = findViewById(R.id.contentEditText);
        editDate = findViewById(R.id.dateEditText);
        editLatitude = findViewById(R.id.latitudeEditText);
        editLongitude = findViewById(R.id.longitudeEditText);

        checkForEdit();
    

    private void checkForEdit() 
        Intent previousIntent = getIntent();

        int passedDiaryID = previousIntent.getIntExtra(Diary.DIARY_EDIT_EXTRA, -1);

        selectedDiary = Diary.getDiaryForID(passedDiaryID);

        if (selectedDiary != null) 
            editID.setText(selectedDiary.getId());
            editSubject.setText(selectedDiary.getSubject());
            editContent.setText(selectedDiary.getContent());
            editDate.setText(selectedDiary.getDate());
            editLatitude.setText(selectedDiary.getLatitude());
            editLongitude.setText(selectedDiary.getLongitude());
         
    

    public void saveEntry(View view) 
        SecondDataBaseHelper secondDataBaseHelper = SecondDataBaseHelper.databaseInstance(this);
        String id = String.valueOf(editID.getText());
        String subject = String.valueOf(editSubject.getText());
        String content = String.valueOf(editContent.getText());
        String date = String.valueOf(editDate.getText());
        String latitude = String.valueOf(editLatitude.getText());
        String longitude = String.valueOf(editLongitude.getText());

        if (selectedDiary == null) 
            Diary newDiary = new Diary(id, subject, content, date, latitude, longitude);
            Diary.diaryArrayList.add(newDiary);
            secondDataBaseHelper.addEntryToDatabase(newDiary);
         else 
            selectedDiary.setId(id);
            selectedDiary.setSubject(subject);
            selectedDiary.setContent(content);
            selectedDiary.setDate(date);

            secondDataBaseHelper.addEntryToDatabase(selectedDiary);
        
        finish();
    

./DisplayAllEntries,用户可以在这里看到所有的日记条目,每个条目都是一个可点击的 ListView 单元格:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class DiaplayAllEntries extends AppCompatActivity 

    private ListView entryListView;

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

        entryListView = findViewById(R.id.diaryListView);

        setDiaryAdapter();
        setOnClickListener();
    



    private void setDiaryAdapter() 
        DiaryAdapter diaryAdapter = new DiaryAdapter(getApplicationContext(), Diary.diaryArrayList);
        entryListView.setAdapter(diaryAdapter);
    

    private void setOnClickListener() 
        entryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                Diary selectedEntry = (Diary) entryListView.getItemAtPosition(position);
                Intent editNoteIntent = new Intent(getApplicationContext(), NoteDetailActivity.class);
                editNoteIntent.putExtra(Diary.DIARY_EDIT_EXTRA, selectedEntry.getId());
                startActivity(editNoteIntent);

            
        );
    


./Diary,是个人日记条目的信息,有getters/setters/etc.

import java.util.ArrayList;

public class Diary 
    private String id;
    private String subject;
    private String content;
    private String date;
    private String latitude;
    private String longitude;

    public static ArrayList<Diary> diaryArrayList = new ArrayList<>();
    public static String DIARY_EDIT_EXTRA = "diaryEdit";

    public Diary(String id, String subject, String content, String date, String latitude, String longitude) 
        this.id = id;
        this.subject = subject;
        this.content = content;
        this.date = date;
        this.latitude = latitude;
        this.longitude = longitude;
    

    public static Diary getDiaryForID(int passedDiaryID) 
        for (Diary diary : diaryArrayList) 
            if (Integer.parseInt((diary.getId())) == passedDiaryID) 
                return diary;
            
        

        return null;
    

    public String getId() 
        return id;
    

    public void setId(String id) 
        this.id = id;
    

    public String getSubject() 
        return subject;
    

    public void setSubject(String subject) 
        this.subject = subject;
    

    public String getContent() 
        return content;
    

    public void setContent(String content) 
        this.content = content;
    

    public String getDate() 
        return date;
    

    public void setDate(String date) 
        this.date = date;
    

    public String getLatitude() 
        return latitude;
    

    public void setLatitude(String latitude) 
        this.latitude = latitude;
    

    public String getLongitude() 
        return longitude;
    

    public void setLongitude(String longitude) 
        this.longitude = longitude;
    

./SecondDataBaseHelper 是项目的数据库:

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

public class SecondDataBaseHelper extends SQLiteOpenHelper 

    private static SecondDataBaseHelper secondDataBaseHelper;

    SQLiteDatabase db = this.getWritableDatabase();

    public final static String DATABASE_NM ="RECORD_OF_USER";
    public final static String TABLE_NM ="DATA_OF_USER";
    public final static String COL_1 = "ID";
    public final static String COL_2 = "SUBJECT";
    public final static String COL_3 = "ENTRY";
    public final static String COL_4 = "DATE";
    public final static String COL_5 = "LATITUDE";
    public final static String COL_6 = "LONGITUDE";


    public SecondDataBaseHelper(Context context) 
        super(context, DATABASE_NM, null, 1);
    

    public static SecondDataBaseHelper databaseInstance(Context context) 
        if (secondDataBaseHelper == null) 
            secondDataBaseHelper = new SecondDataBaseHelper(context);
        

        return secondDataBaseHelper;
    

    @Override
    public void onCreate(SQLiteDatabase db) 

        db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NM +" (ID TEXT, SUBJECT TEXT, ENTRY TEXT, DATE TEXT, LATITUDE TEXT, LONGITUDE TEXT)");

    

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 

    

    public void addEntryToDatabase(Diary entry) 
        ContentValues contentValues = new ContentValues();

        contentValues.put(COL_1, entry.getId());
        contentValues.put(COL_2, entry.getSubject());
        contentValues.put(COL_3, entry.getContent());
        contentValues.put(COL_4, entry.getDate());
        contentValues.put(COL_5, entry.getLatitude());
        contentValues.put(COL_6, entry.getLongitude());

        db.insert(TABLE_NM, null, contentValues);
    

    public void populateDiaryListArray() 
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();

        try (Cursor result = sqLiteDatabase.rawQuery("SELECT * FROM " + TABLE_NM, null)) 
            if (result.getCount() != 0) 
                while (result.moveToNext()) 
                    String id = result.getString(1);
                    String subject = result.getString(2);
                    String content = result.getString(3);
                    String date = result.getString(4);
                    String latitude = result.getString(5);
                    String longitude = result.getString(6);

                    Diary diary = new Diary(id, subject, content, date, latitude, longitude);
                    Diary.diaryArrayList.add(diary);
                
            
        
    


    public void updateDiaryInDB(Diary entry) 
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put(COL_1, entry.getId());
        contentValues.put(COL_2, entry.getSubject());
        contentValues.put(COL_3, entry.getContent());
        contentValues.put(COL_4, entry.getDate());
        contentValues.put(COL_5, entry.getLatitude());
        contentValues.put(COL_6, entry.getLongitude());

        sqLiteDatabase.update(TABLE_NM, contentValues, COL_1 + " ?= ", new String[]String.valueOf(entry.getId()));
    

调试一切,我认为问题是 ./DisplayAllEntries 中的 .onItemClick() 或 ./NoteDetailActivity 中的 checkForEdit()。相反,这可能是一个糟糕的架构,所以如果有人对如何单击单个单元格进行编辑有任何想法,我将非常感激!

【问题讨论】:

editNoteIntent.putExtra(Diary.DIARY_EDIT_EXTRA, selectedEntry.getId()); - 你的Diary#getId() 方法返回一个String,所以你在Intent 上添加了一个String。但是,在NoteDetailActivity 中,您正在检查int 额外:int passedDiaryID = previousIntent.getIntExtra(Diary.DIARY_EDIT_EXTRA, -1);。将 ID 作为数字处理可能更有意义。 ...我在笑和哭之间,因为这正是我所需要的。哈哈,新手多多指教。同样,我需要使用 Google Maps API 来显示基于纬度/经度输入字符串的地图。我将不得不在应用程序中将它们重新实现为双打,但如果我在显示所有日记条目的“全部”意图中添加一个“地图”按钮,我会遵循类似的内容,其中一旦我点击“地图”,我是否可以自动将这些纬度/经度坐标输入到将它们提交给 API 并返回 ImageView 的新意图中? 在那种情况下,也许吧?这取决于你的意思,具体来说。如果您想开始另一个包含实际地图的Activity,那么是的,您可以使用相同的机制将坐标传递给下一个Activity,然后在那里检索它们并将它们交给MapView(这是 Maps API 的基本 UI 组件,顺便说一句)。相反,如果您只想在同一个Activity 中弹出一个小对话框或窗口,则根本不需要弄乱Intent,尽管显然会涉及其他一些设置。有意义吗? 当然,Fragment 是一个选项,并且有一些设计为“弹出窗口”的基类可能对您的设计有意义;例如,DialogFragmentBottomSheetDialogFragment 等。您也可以以某种方式将MapView 整合到Activity 的内容中;例如,直接作为主布局中的抽屉,或者作为底部的工作表等。随着您的使用,您会更好地了解适合给定设置的设置。 嘿,这是一些非常棒的信息。我认为 DialogFragment 可能正是我正在寻找的。再次感谢您的宝贵建议! 【参考方案1】:

Diarys id 是String

public String getId() 
    return id;

并且你在Bundles extra 中传递了这个值:

editNoteIntent.putExtra(Diary.DIARY_EDIT_EXTRA, selectedEntry.getId());

但在NoteDetailActivity 中,您试图将其读作Integer

int passedDiaryID = previousIntent.getIntExtra(Diary.DIARY_EDIT_EXTRA, -1);

你应该在这里使用getStringExtra方法,最终如果你需要它作为int使用Integer.parseInt(...)

【讨论】:

以上是关于编辑 Android 日记条目的主要内容,如果未能解决你的问题,请参考以下文章

Android 日记应用 - 搜索 SQLite 数据库并在 ListView 中显示结果

在单独的 ViewController 中编辑 tableView 条目,并将编辑后的数据传回 tableView

android studio 学习日记 报错

android日记

android日记

2016年3月10日Android实习日记