vendredi 29 avril 2016

SQLite filter data

i want to filter my SQLite database and i want to ask in which way it is done? When i used searchView, after one search, i can not add items to database. Should i use editText to filter?

My code: DBAdapter.java

static final String ROW_ID ="id";
static final String NAME ="name";
static final String TAG = "DBAdapter";

static final String DBNAME="m_DB";
static final String TBNAME="m_TB";
static final int DBVERSION='1';

static final String CREATE_TB="CREATE TABLE m_TB(id INTEGER PRIMARY KEY AUTOINCREMENT,"
        + "name TEXT NOT NULL);";

final Context c;
SQLiteDatabase db;
DBHelper helper;
public DBAdapter(Context ctx) {
    this.c = ctx;
    helper = new DBHelper(c);
}


private static class DBHelper extends SQLiteOpenHelper {
    public DBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(CREATE_TB);
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("DBAdapter","Upgrading DB");
        db.execSQL("DROP TABLE IF EXISTS m_TB");
        onCreate(db);
    }
}
public DBAdapter openDB()
{
    try {
        db=helper.getWritableDatabase();
    } catch (SQLException e)
    {
        Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show();
    }
    return this;
}

public void close()
{
    helper.close();
}

public long add(String name)
{
    try {
        ContentValues cv = new ContentValues();
        cv.put(NAME,name);
        return db.insert(TBNAME,ROW_ID,cv);
    } catch (SQLException e)
    {
        e.printStackTrace();
    }
    return 0;
}

public Cursor getAllNames()
{
    String[] columns={ROW_ID,NAME};
    return db.query(TBNAME,columns,null,null,null,null,null);
}

MainActivity.java

public class MainActivity extends AppCompatActivity {


    ListView lv;
    EditText nameTxt;
    Button savebtn,retrievebtn;
    ArrayList<String> books = new ArrayList<String>();
    ArrayAdapter<String> adapter;

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

        nameTxt=(EditText)findViewById(R.id.editText);

        savebtn=(Button)findViewById(R.id.saveBtn);
        retrievebtn=(Button)findViewById(R.id.retrieveBtn);

        lv = (ListView)findViewById(R.id.listView1);

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_selectable_list_item,books);
        final DBAdapter db = new DBAdapter(this);

        savebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                db.openDB();
                long result = db.add(nameTxt.getText().toString());

                if(result > 0)
                {
                    nameTxt.setText("");
                }else
                {
                    Toast.makeText(getApplicationContext(),"Failure", Toast.LENGTH_SHORT).show();
                }
                db.close();
            }
        });

        retrievebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                books.clear();
                db.openDB();
                Cursor c = db.getAllNames();

                while (c.moveToNext())
                {
                    String name = getString(1);
                    books.add(name);
                }
                lv.setAdapter(adapter);
                db.close();
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),books.get(position), Toast.LENGTH_SHORT).show();
            }
        });
    }

Aucun commentaire:

Enregistrer un commentaire