以下是一个简单的布局代码示例,用于在 Android 开发中保存新闻到 SQLite 数据库:

activity_main.xml(主活动布局)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingTop="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editTextTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入新闻标题" />

    <EditText
        android:id="@+id/editTextContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/editTextTitle"
        android:layout_marginTop="16dp"
        android:hint="请输入新闻内容" />

    <Button
        android:id="@+id/buttonSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editTextContent"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:text="保存" />

</RelativeLayout>

MainActivity.java(主活动代码)

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText editTextTitle;
    private EditText editTextContent;
    private Button buttonSave;

    private NewsDatabaseHelper databaseHelper;

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

        editTextTitle = findViewById(R.id.editTextTitle);
        editTextContent = findViewById(R.id.editTextContent);
        buttonSave = findViewById(R.id.buttonSave);

        databaseHelper = new NewsDatabaseHelper(this);

        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveNews();
            }
        });
    }

    private void saveNews() {
        String title = editTextTitle.getText().toString().trim();
        String content = editTextContent.getText().toString().trim();

        if (title.isEmpty() || content.isEmpty()) {
            Toast.makeText(this, '请输入新闻标题和内容', Toast.LENGTH_SHORT).show();
            return;
        }

        SQLiteDatabase db = databaseHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put('title', title);
        values.put('content', content);

        long newRowId = db.insert('news', null, values);

        if (newRowId != -1) {
            Toast.makeText(this, '新闻保存成功', Toast.LENGTH_SHORT).show();
            editTextTitle.setText("");
            editTextContent.setText("");
        } else {
            Toast.makeText(this, '新闻保存失败', Toast.LENGTH_SHORT).show();
        }

        db.close();
    }
}

NewsDatabaseHelper.java(数据库帮助类)

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

public class NewsDatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = 'news.db';
    private static final int DATABASE_VERSION = 1;

    public NewsDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableQuery = 'CREATE TABLE news (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT)';
        db.execSQL(createTableQuery);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String dropTableQuery = 'DROP TABLE IF EXISTS news';
        db.execSQL(dropTableQuery);
        onCreate(db);
    }
}

以上代码示例中,我们创建了一个包含标题和内容的新闻表(news)来保存新闻数据。在点击保存按钮时,会将输入的新闻标题和内容保存到数据库中。

Android 开发新闻系统 - 使用 SQLite 数据库保存新闻数据

原文地址: http://www.cveoy.top/t/topic/f331 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录