Experiment 8 SQLite Database operations

Source: Internet
Author: User
Tags sqlite sqlite database


Lab Report

Course Name

Mobile Internet development based on Android platform

Date of experiment

2016/5/6

Name of the experimental project

SQLite database Operations

Location of the experiment

General Laboratory

Type of experiment

-Verification Type √ design type-comprehensive type

Hours

2

The purpose and requirements of the experiment (the knowledge points which are involved in this experiment and are required to be mastered)

1. Design a Personal Address book, mastering the development of the database under the Android platform, the personal Address Book mainly includes the contact list and contact details and other interfaces.

2. The main interface of the program is the directory of the contacts to display the name of the contact on the phone. Click the contact's name to display the contact's details. The menu bar pops up after you press the MEMU key. Click the buttons on the menu bar to add contacts and delete contacts.

second, the experimental environment (the hardware equipment and related software used in this experiment)

(1) PC Machine

(2) Operating system: Windows XP

(3) Software: Eclipse, jdk1.6,android Sdk,adt

Third, the contents and steps of the experiment

1) Determine the data structure of the database

2) The icon to be used to copy the program into the RES/DRAWABLE-MDPI directory

3) Define string resource String.xml

4) Development layout file Activity_main.xml to display the contact list

5) Create a new detail.xml in the layout directory to display contact details

6) Develop database auxiliary class Myopenhelper class, create a new Myopenhelper.java

7) The next step into the development of the mainactivity end, to achieve database additions, deletions, change records and other operations

8) Create a new activity name called Detailactivity.java, realize the contact details display function

Four, the experimental results (the experimental source program list and operating results or experimental conclusions, experimental design drawings)


Code:



Mainactivity part of the code:


 
package com.example.tongxunlu;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
    Button btn1,btn2;
    ListView listview;
    public static MyOpenHelper dbhelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=(Button)findViewById(R.id.button1);
        btn2=(Button)findViewById(R.id.button2);
        listview=(ListView)findViewById(R.id.lv);
        dbhelper=new MyOpenHelper(MainActivity.this, "myDict.db10", 1);
        Cursor cursor=dbhelper.getReadableDatabase().rawQuery("select * from contacts", null);
        inflateList(cursor);
        btn1.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this, BaocunActivity.class);
                startActivity(intent);
                MainActivity.this.finish();
            }
        });
        
       btn2.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this, shanchuActivity.class);
                startActivity(intent);
                MainActivity.this.finish();
            }
        });
        


        
    }
     private void inflateList(Cursor cursor) {
            SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter(MainActivity.this,
                    R.layout.lian, cursor, new String[]{"name","phone"}, 
                    new int[]{R.id.text1,R.id.text2}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
            listview.setAdapter(simpleCursorAdapter);
            listview.setOnItemClickListener(new ItemClickListener()); 
        }
     private final class ItemClickListener implements OnItemClickListener{  
          
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
                ListView listview = (ListView) parent; 
                Cursor cursor1 = (Cursor) listview.getItemAtPosition(position);
                String personid = cursor1.getString(cursor1.getColumnIndex("name"));
                String personid1 = cursor1.getString(cursor1.getColumnIndex("phone")); 
                String personid2 = cursor1.getString(cursor1.getColumnIndex("mobile"));
                String personid3 = cursor1.getString(cursor1.getColumnIndex("email"));
                String personid4 = cursor1.getString(cursor1.getColumnIndex("post"));
                String personid5 = cursor1.getString(cursor1.getColumnIndex("addr"));
                String personid6 = cursor1.getString(cursor1.getColumnIndex("comp"));
                Intent intent=new Intent();
                intent.putExtra("username1",personid); 
                intent.putExtra("userphone1",personid1);
                intent.putExtra("usermobile1",personid2);
                intent.putExtra("useremail1",personid3);
                intent.putExtra("userpost1",personid4);
                intent.putExtra("useraddr1",personid5);
                intent.putExtra("usercomp1",personid6);
                intent.setClass(MainActivity.this, DetailActivity.class);
                startActivity(intent);
                MainActivity.this.finish();
                  
            }  
        }   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}








Run Result: ()











Five, Experimental summary (analysis of the results of the experiment, experience and improvement of experimental ideas)

This experiment feels very difficult, does not have the satisfactory, repeatedly appears the breakdown, did not appear the mistake, quite hard to find the problem.

Experimental reviews

Experimental results

Guide Teacher Signature: Date


Experiment 8 SQLite Database operations


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.