如何使用Searchview从数据库SQlite搜索数据并在ListView中显示结果?

时间:2015-10-10 14:58:05

标签: android sqlite listview database-connection searchview

我是Android工作室的新手,请给我下面给出的代码解决方案,现在我想以列表形式在SearchView下方显示我的结果,它应该是可点击的

这是我的Main_Activity.java代码

package com.example.jawa.pos;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

/**
 * Created by jawa on 10/7/2015.
 */
public class AddNewUser extends Activity {
    DATA_BASE data_base;
    EditText accountnumber,personalname,email,phonenmber,comment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addnewuser);


        data_base=  new DATA_BASE(this);


        accountnumber = (EditText) findViewById(R.id.accountnumber);
        personalname = (EditText) findViewById(R.id.customernumber);
        phonenmber = (EditText) findViewById(R.id.phonenumber);
        email = (EditText) findViewById(R.id.email);
        comment= (EditText) findViewById(R.id.comment);

    }
    public void register(View view) {

        int Accountnumber = Integer.parseInt(accountnumber.getText().toString());
        String Personalname = personalname.getText().toString();
        int Phonenumber = Integer.parseInt(phonenmber.getText().toString());
        String Email = email.getText().toString();
        String Comment = comment.getText().toString();

        //Add some Customer data as a sample
        long id = data_base.createCustomer(Accountnumber, Personalname, Phonenumber, Email, Comment);

        if(id <0){
            Toast.makeText(this,"Unsuccessful",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"Successful",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


}

这是我的DATA_BASE_Adapter.java文件

package com.example.jawa.pos;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.method.DateTimeKeyListener;
import android.util.Log;
import android.widget.Toast;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;


/**
 * Created by jawa on 10/9/2015.
 */
public class DATA_BASE_Adapter {

    DATA_BASE data_base;
    Context context;
    SQLiteDatabase db;

    public DATA_BASE_Adapter(Context context) {
        data_base = new DATA_BASE(context);
    }

    public long addUaser(int accno,String name,int phonenumber,String email,String comment ){

        SQLiteDatabase db = data_base.getWritableDatabase();

        ContentValues contentValues = new ContentValues();

        contentValues.put(DATA_BASE.DATE_TIME, System.currentTimeMillis());
        contentValues.put(DATA_BASE.ACCOUNT_NO, accno);
        contentValues.put(DATA_BASE.CUSTOMER_NAME, name);
        contentValues.put(DATA_BASE.PHONE_NO, phonenumber);
        contentValues.put(DATA_BASE.EMAIL, email);
        contentValues.put(DATA_BASE.COMMENT, comment);

        long id = db.insert(DATA_BASE.TABLE_NAME1,null, contentValues);

        return id;
    }

    public String showAlldetails(){

        SQLiteDatabase db = data_base.getWritableDatabase();
        String[] colums = {data_base.DATE_TIME,
                data_base.ACCOUNT_NO,
                data_base.CUSTOMER_NAME,
                data_base.PHONE_NO,
                data_base.EMAIL,
                data_base.COMMENT};
        Cursor cursor = db.query(DATA_BASE.TABLE_NAME1, colums, null, null, null, null, null);

        StringBuffer buffer = new StringBuffer();

        while(cursor.moveToNext()){

            int datetime = cursor.getInt(0);
            int cacc =  cursor.getInt(1);
            String name = cursor.getString(2);
            String phoneno = cursor.getString(3);
            String email = cursor.getString(4);
            String comment = cursor.getString(5);
            buffer.append(datetime+" "+cacc + " " + name + " " + phoneno+" "+email+" "+ comment+"\n" );
        }
        return buffer.toString();
    }

    public String selectionCondition(String name) throws SQLException{
        SQLiteDatabase db = data_base.getWritableDatabase();
        String[] colums = {data_base.DATE_TIME,
                data_base.ACCOUNT_NO,
                data_base.CUSTOMER_NAME,
                data_base.PHONE_NO,
                data_base.EMAIL,
                data_base.COMMENT};
        Cursor cursor = db.query(DATA_BASE.TABLE_NAME1, colums, data_base.CUSTOMER_NAME+"='"+name+"'",
                null, null, null, null);

        StringBuffer buffer = new StringBuffer();

        while(cursor.moveToNext()){

            int Index0 = cursor.getColumnIndex(data_base.DATE_TIME);
            int Index2 = cursor.getColumnIndex(data_base.CUSTOMER_NAME);
            int Index1 = cursor.getColumnIndex(data_base.ACCOUNT_NO);
            int Index3 = cursor.getColumnIndex(data_base.PHONE_NO);
            int Index4 = cursor.getColumnIndex(data_base.EMAIL);
            int Index5 = cursor.getColumnIndex(data_base.COMMENT);

            int date=  cursor.getInt(Index0);
            int cacc =  cursor.getInt(Index1);
            String cname = cursor.getString(Index2);
            String phoneno = cursor.getString(Index3);
            String email = cursor.getString(Index4);
            String comment = cursor.getString(Index5);

            buffer.append(date+" "+cacc + " " + cname + " " + phoneno+" "+email+" "+ comment+"\n" );
        }
        return buffer.toString();
    }
    static class DATA_BASE extends SQLiteOpenHelper  {

        private static final String DATABASE_NAME = "coldstore";
        private static final int DATABASE_VERSION = 1;
        private static final String TABLE_NAME1 = "new_customer";
        private static final String ACCOUNT_NO = "_Account No";
        private static final String CUSTOMER_NAME = "Customer Name";
        private static final String PHONE_NO = "Phone";
        private static final String EMAIL  = "Mail Id";
        private static final String COMMENT= "Comment";
        private static final String DATE_TIME= "Date/Time";
        private static final String CREATE_TABLE_NEW_CUSTOMER = "CREATE TABLE "+TABLE_NAME1+"" +
                "("+DATE_TIME+" INTEGER," +
                ""+ACCOUNT_NO+" INTEGER PRIMARY KEY AUTOINCREMENT," +
                ""+CUSTOMER_NAME+" VARCHAR(25) NOT NULL," +
                ""+PHONE_NO+" INTEGER NOT NULL," +
                ""+EMAIL+" VARCHAR(130) NOT NULL," +
                ""+COMMENT+" VARCHAR(250) NOT NULL;";
        private static final String DROP_TABLE = "DROP TABLE IF EXISTS "+TABLE_NAME1+";";
        private Context context;

        public DATA_BASE(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.context = context;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            try {
                Toast.makeText(context,"OnCreate Table new Customer",Toast.LENGTH_SHORT).show();
                db.execSQL(CREATE_TABLE_NEW_CUSTOMER);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            try {
                Toast.makeText(context,"OnUpgrade Table new Customer ",Toast.LENGTH_SHORT).show();
                db.execSQL(DROP_TABLE);
                onCreate(db);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

    public DATA_BASE_Adapter open() throws SQLException {
        data_base = new DATA_BASE(context);
         db = data_base.getWritableDatabase();
        return this;
    }
    public void close() {
        if (data_base!= null) {
            data_base.close();
        }
    }
    public boolean deleteAllCustomers() {

        int doneDelete = 0;
        doneDelete = db.delete(DATA_BASE.TABLE_NAME1, null , null);
        return doneDelete > 0;

    }
}

现在我想通过适配器将SearchView提供给此数据库,并且此reslut应显示在Listview中。

请有人帮帮我 从最近两周尝试破解,但无法找到解决方案。

0 个答案:

没有答案