2021年2月22日 记账本开发05

今天完成了详情页面的布局和跳转

新建SelectAct:

package bjfu.it.sun.cashbook;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;

public class SelectAct extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select); //加载视图
        System .out.println(getIntent() .getIntExtra(CashDB.ID ,0) ) ;
    }
}

新建的详情页面布局activity_select.xml:这是之前主页面的布局,还没改

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SelectAct">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/context"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="内容"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/coast"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="花费"
            android:inputType="textPersonName" />

        <Button
            android:id="@+id/submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="保存" />

        <Button
            android:id="@+id/cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="取消" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity更新为:

package bjfu.it.sun.cashbook;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button addbtn;
    //创建Intent 标识从MainActivity跳转到AddContent
    private Intent i;
    private ListView lv;
    private MyAdapter adapter;
    private CashDB cashDB;
    private SQLiteDatabase dbReader;
    Cursor cursor;

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

    //初始化操作,初始化view
    public void initView(){
        lv = (ListView) findViewById(R.id.list2);

        addbtn=findViewById(R.id.addbtn);//初始化
        addbtn .setOnClickListener(this);//添加监听事件

        cashDB=new CashDB(this) ;
        dbReader = cashDB.getReadableDatabase();//获取数据库读取的权限

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                cursor.moveToPosition(position );//获取当前cursor所指定的当前position
                //通过intent把当前数据传到另一个页面(详情页面)
                Intent i=new Intent(MainActivity .this,SelectAct.class) ;//第一个是当前页面,第二个是跳转页面

                i.putExtra(CashDB.ID ,cursor.getInt(cursor .getColumnIndex(CashDB.ID) ) );//传递的内容
                i.putExtra(CashDB.CONTENT  ,cursor .getString(cursor .getColumnIndex(CashDB.CONTENT) ) );
                i.putExtra(CashDB.COAST   ,cursor .getString(cursor .getColumnIndex(CashDB.COAST ) ) );
                i.putExtra(CashDB.TIME   ,cursor .getString(cursor .getColumnIndex(CashDB.TIME) ) );

                startActivity(i) ;//执行跳转
            }
        }) ;

    }




    //添加一个复写方法
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.addbtn:
                i = new Intent(this, AddContent.class);
                i.putExtra("flag", "1");//传递的内容
                startActivity(i);//执行跳转
                break;
            case R.id.button2:
                i = new Intent(this, list_item.class);
                i.putExtra("flag", "2");//传递的内容
                startActivity(i);//执行跳转
                break;

        }
    }
    public void selectDB(){

        //Cursor cursor = dbReader.query(CashDB.TABLE_NAME, null, null, null, null, null, null);
        cursor=dbReader .query(CashDB .TABLE_NAME ,null,null,null,null,null,null);
        adapter = new MyAdapter(this, cursor) ;
        lv.setAdapter(adapter);
        }


    @Override
    protected void onResume() {
        super.onResume();
        selectDB();
    }

    /*
    //创建方法用于添加具体内容
    public void addDB(){
        ContentValues cv=new ContentValues() ;
        cv.put(CashDB.CONTENT ,"Hello");
        cv.put(CashDB.COAST  ,"33");
        cv.put(CashDB.TIME  ,getTime());
        //写入数据库
        dbWriter.insert(CashDB.TABLE_NAME ,null,cv) ;

    }
    */

/*
    //获取当前设备的时间
    public String getTime(){
        SimpleDateFormat format =new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss") ;
        Date curData=new Date();//实例化
        String str =format .format(curData);//获取时间
        return str;

    }
    */

}

 试验成功!!

原文地址:https://www.cnblogs.com/j-y-s/p/14461573.html