搜索数据库不起作用

时间:2016-06-09 20:53:54

标签: java android sqlite

所以我有一个非常奇怪的问题。基本上我是在制作一个应用程序,在数据库中搜索字段' country'并根据搜索到的国家/地区显示信息。现在奇怪的是,它只适用于少数几个国家,通过这些字段都写得一样,而在其他活动中使用相同的代码,它可以完美地工作。您认为问题是什么?

Database.java

public class DBResep2 extends SQLiteOpenHelper {
final static String DB_NAME = "db_resep2";

public DBResep2(Context context) {
    super(context, DB_NAME, null, 46);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE IF NOT EXISTS resep2(_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,luna TEXT,tara TEXT,oras TEXT,durata TEXT,facil TEXT,bani TEXT,desc TEXT,img BLOB)";
    db.execSQL(sql);

    ContentValues values = new ContentValues();

    values.put("_id","1");
    values.put ("name","Fitness Trainer ");
    values.put ("oras","Alexandria,Egypt ");
    values.put ( "durata","1 iulie -30 septembrie ");
    values.put ( "desc"," You will have to: -Demonstrate how to carry out various exercises and routines. -Observe participants and inform them of corrective measures necessary to improve their skills. -Be self-motivated, energetic, organized and responsible. -Be a good communicator and attentive. Experience for this opportunity is required. ");
    values.put ( "country", " egipt ");
    values.put ("luna","Iulie,August,Septembrie");
    values.put("img",R.drawable.egipt);
    values.put("bani", "3000 dolari + 50% commission on private lesson ");
    db.insert("resep2", "_id", values);



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS resep2");
    onCreate(db);

}

Main.java

public class MainActivity2 extends Activity {
    protected ListView lv;
    protected ListAdapter adapter;
    SQLiteDatabase db;
    Cursor cursor;
    AutoCompleteTextView et_db;
    AutoCompleteTextView et_db2;


String[] arr2 = {"pakistan", "rusia", "cehia", "egipt", "turcia", "germania", "anglia", "india", "tunisia", "suedia", "cehia"};

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    db = (new DBResep2(this)).getWritableDatabase();
    lv = (ListView) findViewById(R.id.lv);
    et_db = (AutoCompleteTextView) findViewById(R.id.et);
    et_db2 = (AutoCompleteTextView) findViewById(R.id.et2);




    try {
        cursor = db.rawQuery("SELECT * FROM resep2 ORDER BY name ASC", null);
        adapter = new SimpleCursorAdapter(this, R.layout.isi_lv2, cursor,
                new String[] { "name", "oras" , "img" }, new int[] {
                R.id.tv_name, R.id.tvOras, R.id.imV });
        lv.setAdapter(adapter);
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                detail(position);

            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>
            (this, android.R.layout.select_dialog_item, arr);
    //Getting the instance of AutoCompleteTextView
    AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.et2);
    actv.setThreshold(1);//will start working from first character
    actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView

    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>
            (this, android.R.layout.select_dialog_item, arr2);
    //Getting the instance of AutoCompleteTextView
    AutoCompleteTextView actv2 = (AutoCompleteTextView) findViewById(R.id.et);
    actv2.setThreshold(1);//will start working from first character
    actv2.setAdapter(adapter2);//setting the adapter data into the AutoCompleteTextView
}
@SuppressWarnings("deprecation")
public void search_db(View v) {
    String edit_db = et_db.getText().toString();
    if (!edit_db.equals("")) {
        try {
            cursor = db.rawQuery("SELECT * FROM resep2 WHERE country LIKE ?",
                    new String[] { "%" + edit_db + "%" });
            adapter = new SimpleCursorAdapter(
                    this,
                    R.layout.isi_lv2,
                    cursor,
                    new String[] { "name", "oras", "img" },
                    new int[] { R.id.tv_name, R.id.tvOras, R.id.imV });
            if (adapter.getCount() == 0) {
                Toast.makeText(
                        this,
                        "Nu am putut gasi niciun proiect in " + edit_db
                                + "", Toast.LENGTH_SHORT).show();

            } else {
                lv.setAdapter(adapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        try {
            cursor = db.rawQuery("SELECT * FROM resep2 ORDER BY name ASC",
                    null);
            adapter = new SimpleCursorAdapter(
                    this,
                    R.layout.isi_lv2,
                    cursor,
                    new String[] { "name", "oras", "img" },
                    new int[] { R.id.tv_name, R.id.tvOras, R.id.imV });
            lv.setAdapter(adapter);
            lv.setTextFilterEnabled(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案