作业

第一段界面布局代码,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context="com.example.mpyypm.qwer.MainActivity">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="35sp" />
    android:id="@+id/w"
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<Button
    android:textSize="20sp"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:onClick="onClick"
    android:layout_height="wrap_content"
    android:text="增加一条新记录呀"/>
    android:id="@+id/a"
<Button
    android:textSize="20sp"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:onClick="onClick"
    android:layout_height="wrap_content"
    android:text="删除第一条记录呀"
    android:id="@+id/d"/>
</LinearLayout>

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    android:id="@+id/t"
</ListView>
</LinearLayout>



布局效果如图:

再写一个Listview界面,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="horizontal">

<TextView
        android:id="@+id/n"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
        android:id="@+id/n"
</LinearLayout>



其次是Java代码部分:

package com.example.mpyypm.qwer;
    import android.database.Cursor;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
    private EditText w;
    private ListView t;
    private MyCursorAdapter myCursorAdapter;
    private PersonDAO personDAO;
    private Cursor cursor;


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

    w = (EditText)findViewById(R.id.w);

    t = (ListView)findViewById(R.id.t);
    
    personDAO = new PersonDAO(this);
    cursor = personDAO.selectAll();
    if(cursor!=null){
        myCursorAdapter = new MyCursorAdapter(MainActivity.this,cursor);
        t.setAdapter(myCursorAdapter);
    }


}

public void onClick(View view) {
    switch(view.getId()){
        case R.id.a:
           
            String name = w.getText().toString();
            personDAO.insert(name);
            cursor = personDAO.selectAll();
            if(cursor!=null){
                myCursorAdapter = new MyCursorAdapter(this,cursor);
                t.setAdapter(myCursorAdapter);
            }
            break;
        case R.id.d:
           
            if(cursor!=null){
                if(cursor.moveToFirst()){
                    personDAO.d(cursor.getString(cursor.getColumnIndex("_id")));
                    cursor = personDAO.selectAll();
                }
            }
            myCursorAdapter = new MyCursorAdapter(this,cursor);
            t.setAdapter(myCursorAdapter);
            break;
    }

 }
}


第二部分Java代码personDAO:


package com.example.mpyypm.qwer;

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


public class PersonDAO {

    private MyDBHelper dbHelper;
    private SQLiteDatabase sqLiteDatabase;

public PersonDAO(Context context){
    dbHelper = new MyDBHelper(context);
}

public void insert(String name){
    sqLiteDatabase = dbHelper.getWritableDatabase();
    String sql = "insert into person(name) values('"+n+"')";
    sqLiteDatabase.execSQL(sql);
}

public Cursor selectAll() {
    sqLiteDatabase = dbHelper.getReadableDatabase();
    Cursor cursor = sqLiteDatabase.query("person", null, null, null, null, null, null);
    return cursor;
}

public void delete(String id){
    sqLiteDatabase = dbHelper.getWritableDatabase();
    String sql = "delete from person where _id="+String.valueOf(id);//"delete from student where _id="+id
    sqLiteDatabase.execSQL(sql);
   }
}

第三部分Java代码:



package com.example.mpyypm.qwer;

    import android.content.Context;
    import android.database.Cursor;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.CursorAdapter;
    import android.widget.TextView;



public class MyCursorAdapter extends CursorAdapter {

public MyCursorAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
    return LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView n = (TextView) view.findViewById(R.id.n);
    n.setText(cursor.getString(cursor.getColumnIndex("n")));
    }
}
posted @ 2017-05-16 21:10  曹小小曹  阅读(179)  评论(0编辑  收藏  举报