每日总结 3.6

今天上了软件工程的课,王老师为我们讲述了代码格式规范。代码缩进、变量命名规范和注释的书写。

还有在进行代码编写时要进行单元测试。

课后有进行Android的学习:

 

package com.example.clock;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cursoradapter.widget.SimpleCursorAdapter;

import android.content.ContentValues;
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.EditText;
import android.widget.ListView;

import com.example.clock.db.Help;

public class Seesome extends AppCompatActivity {
    private SimpleCursorAdapter mAdapter;
    private ListView listView;

    private Button btn_add;
    private Button btn_delete;

    private Button btn_back;
    private EditText et_date;
    private EditText et_gj;
    private EditText et_zj;
    private EditText et_jc;
    private EditText et_max;

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

        btn_add = (Button) findViewById(R.id.btn_add);
        btn_delete = (Button) findViewById(R.id.btn_delete);
        listView = (ListView) findViewById(R.id.list);

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ad1dData();
                refleshListView();
            }
        });

        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteData();
                refleshListView();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(MainActivity.this,""+position,Toast.LENGTH_SHORT).show();
                int positions = position + 1;
                Help db = new Help(Seesome.this);
                SQLiteDatabase dbWrite = db.getWritableDatabase();
                dbWrite.delete("daka","_id=?",new String[]{""+positions});
                dbWrite.close();
                refleshListView();
            }
        });
    }

    public void addData() {
        btn_back = (Button) findViewById(R.id.btn_back);
        et_date = (EditText) findViewById(R.id.timeET);
        et_gj = (EditText) findViewById(R.id.key_word);
        et_zj = (EditText) findViewById(R.id.zongjie);
        et_jc = (EditText) findViewById(R.id.jianchi);
        et_max = (EditText) findViewById(R.id.max_day);

        btn_add = (Button) findViewById(R.id.btn_add);
        btn_delete = (Button) findViewById(R.id.btn_delete);
        listView = (ListView) findViewById(R.id.list);

        btn_add.setVisibility(View.GONE);
        btn_delete.setVisibility(View.GONE);
        listView.setVisibility(View.GONE);

        btn_back.setVisibility(View.VISIBLE);
        et_date.setVisibility(View.VISIBLE);
        et_gj.setVisibility(View.VISIBLE);
        et_zj.setVisibility(View.VISIBLE);
        et_jc.setVisibility(View.VISIBLE);
        et_max.setVisibility(View.VISIBLE);

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_add.setVisibility(View.VISIBLE);
                btn_delete.setVisibility(View.VISIBLE);
                listView.setVisibility(View.VISIBLE);

                btn_back.setVisibility(View.GONE);
                et_date.setVisibility(View.GONE);
                et_gj.setVisibility(View.GONE);
                et_zj.setVisibility(View.GONE);
                et_jc.setVisibility(View.GONE);
                et_max.setVisibility(View.GONE);

                String date = et_date.getText().toString(),
                        keyw =et_gj.getText().toString(),
                        zj = et_zj.getText().toString(),
                        max = et_max.getText().toString(),
                jc =et_jc.getText().toString();


                Help db = new Help(Seesome.this);
                SQLiteDatabase dbWrite = db.getWritableDatabase();

                ContentValues cv = new ContentValues();
                cv.put("date", date);
                cv.put("keyword", keyw);
                cv.put("zj",zj );
                cv.put("jc",jc );
                cv.put("max",max );
                dbWrite.insert("daka", null, cv);

                dbWrite.close();

                refleshListView();
            }
        });

//        String id = et_id.getText().toString(),
//               name =et_name.getText().toString(),
//               sex = et_sex.getText().toString();
//
//
//        Db db = new Db(MainActivity.this);
//        SQLiteDatabase dbWrite = db.getWritableDatabase();
//
//        ContentValues cv = new ContentValues();
//        cv.put("_id",id);
//        cv.put("name", name);
//        cv.put("sex", sex);
//        dbWrite.insert("user2", null, cv);
//
//        dbWrite.close();
    }

    public void deleteData() {
        Help db = new Help(Seesome.this);
        SQLiteDatabase dbWrite = db.getWritableDatabase();
        dbWrite.delete("daka",null,new String[]{});
        dbWrite.close();
    }

    public void refleshListView() {
        Help db = new Help(Seesome.this);
        SQLiteDatabase dbRead = db.getReadableDatabase();
        Cursor c = dbRead.query("daka", null, null, null, null, null, null);

        mAdapter = new SimpleCursorAdapter(Seesome.this, R.layout.fragment_one_item, c,
                new String[]{"date","keyword"}, new int[]{R.id.tv_expense_date,R.id.tv_expense_gj});

        listView.setAdapter(mAdapter);

        dbRead.close();
    }

}

之后又进行了查找的代码学习。

在csdn读了别人的代码

searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
String name1=name.getText().toString();
show.setText(null);
if(name1.equals("")){
// show.setText("姓名");
// showAge.setText("年龄");
// Cursor cursor = db.rawQuery("select * from information",null);
// while (cursor.moveToNext()) {
// String newName = cursor.getString(cursor.getColumnIndex("name"));
// int newAge = cursor.getInt(cursor.getColumnIndex("age"));
// show.setText(show.getText() + "\n" + newName);
// showAge.setText(showAge.getText()+"\n" + newAge);
// }
myShow();
db.close();
}else {
show.setText("姓名");
showAge.setText("年龄");
Cursor cursor = db.rawQuery("select * from information where name = ? ", new String[]{name1});

while (cursor.moveToNext()) {
String newName = cursor.getString(cursor.getColumnIndex("name"));
int newAge = cursor.getInt(cursor.getColumnIndex("age"));
// show.setText(show.getText() + "\n" + newName + "\t" + newAge);
show.setText(show.getText() + "\n" + newName);
showAge.setText(showAge.getText()+"\n" + newAge);
}
cursor.close();
db.close();
name.setText(null);
age.setText(null);
}
}
});

posted @ 2023-03-06 19:29  一个小虎牙  阅读(10)  评论(0编辑  收藏  举报