0

Я застрял в этом за последние пару часов (к сожалению).ошибка поиска запроса андроида sqlite

Я создаю приложение с SQLite, где приложение может выполнять все гибкие команды SQL. Я в настоящее время зациклился на том, как искать запрос из столбцов, отличных от ID.

SQLiteDb:

public class SqliteDbTab extends SQLiteOpenHelper 
    { 
public static String DATABASENAME = "androidadvancesqlite"; 
public static String TABTABLE = "TABTABLE"; 

public static String ID = "ID"; 
public static String NAME = "name"; 
public static String PHONE = "PhOnE"; 
public static String MAIL = "mail"; 
public static String ADDRESS = "address"; 
public static String IMAGE = "image"; 

private ArrayList<Helperdb> cartList = new ArrayList<Helperdb>(); 
Context c; 

public SqliteDbTab(Context context) 
{ 
    super(context, DATABASENAME, null, 33); 
    c = context; 
} 

@Override 
public void onCreate(SQLiteDatabase db) 
{ 
    // TODO Auto-generated method stub 

    db.execSQL("CREATE TABLE if not exists TABTABLE(ID INTEGER PRIMARY KEY AUTOINCREMENT," 
      + NAME+ " text, " 
      + IMAGE + " BLOB , " 
      + PHONE + " text , " 
      + MAIL + " text , " 
      + ADDRESS + " TEXT)"); 
} 
@Override 
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) 
{ 
    // TODO Auto-generated method stub 
    db.execSQL("DROP TABLE IF EXISTS" + TABTABLE); 
    onCreate(db); 
} 

public void addContact(Helperdb productitem) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put("NAME", productitem.name); 
    contentValues.put("PHONE", productitem.phone); 
    contentValues.put("MAIL", productitem.mail); 
    contentValues.put("ADDRESS", productitem.address); 
    contentValues.put("IMAGE", productitem.image); 
    db.insert(TABTABLE, null, contentValues); 
    db.close(); 
} 

// update 
public void updateContact(Helperdb productList) 
{ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put("NAME", productList.name); 
    contentValues.put("PHONE", productList.phone); 
    contentValues.put("MAIL", productList.mail); 
    contentValues.put("ADDRESS", productList.address); 
    contentValues.put("IMAGE", productList.image); 
    db.update(TABTABLE, contentValues, "ID="+ productList.id, null); 
    db.close(); 
} 

public void emptyProduct() 
{ 
    try { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     db.execSQL(TABTABLE); 
     db.close(); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
} 

public void removeProduct(String id,String name, String phone, String mail,String address,byte[] blob) 
{ 
    try { 
     String[] args = { id }; 
     getWritableDatabase().delete(TABTABLE, "ID=?", args); 
    } catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

public ArrayList<Helperdb> getProudcts() 
{ 
    cartList.clear(); 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery("select * from TABTABLE", null); 
    if (cursor.getCount() != 0) 
    { 
     if (cursor.moveToFirst()) 
     { 
      do 
      { 
       Helperdb item = new Helperdb(); 
       item.id = cursor.getString(cursor.getColumnIndex("ID")); 
       item.name = cursor.getString(cursor.getColumnIndex("name"));      
       item.phone = cursor.getString(cursor.getColumnIndex("PhOnE")); 
       item.mail = cursor.getString(cursor.getColumnIndex("mail"));      
       item.address = cursor.getString(cursor.getColumnIndex("address"));     
       item.image = cursor.getBlob(cursor.getColumnIndex("image")); 

       cartList.add(item); 
      } 
      while (cursor.moveToNext()); 
     } 
    } 
    cursor.close(); 
    db.close(); 
    return cartList; 

} 

public ArrayList<Helperdb> getProudcts(String record) 
{ 
    cartList.clear(); 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor=db.query(true,TABTABLE,new String[]{"ID","name","PhOnE","mail","address","image"},"name"+"=?",new String[]{record},null,null,null,null); 
    if (cursor.getCount() != 0) 
    { 
     if (cursor.moveToFirst()) 
     { 
      do { 
       Helperdb item = new Helperdb(); 
       item.id = cursor.getString(cursor.getColumnIndex("ID")); 
       item.name = cursor.getString(cursor.getColumnIndex("name")); 
       item.phone = cursor.getString(cursor.getColumnIndex("PhOnE")); 
       item.mail = cursor.getString(cursor.getColumnIndex("mail"));      
       item.address = cursor.getString(cursor.getColumnIndex("address"));     
       item.image = cursor.getBlob(cursor.getColumnIndex("image")); 
       cartList.add(item); 
      } while (cursor.moveToNext()); 
     } 
    } 
    cursor.close(); 
    db.close(); 
    return cartList; 
} 
} 

Java файл:

package com.example.simpletablist; 
public class Tab3 extends Activity implements TextWatcher 
{ 
EditText _searchbox; 
private ProgressBar showprogress; 
searchtask dotask; 
private ArrayList<Helperdb> _productList; 
ListView _listview; 
SqliteDbTab db; 
public AutoCompleteTextView myAutoComplete; 
private ArrayList<Helperdb> _productList_Temp; 
String query = ""; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activitytab3); 
    _searchbox = (EditText) findViewById(R.id.txtsearchproduct); 
    showprogress = (ProgressBar) findViewById(R.id.showprogress); 
    _listview = (ListView) findViewById(R.id.searchlistview); 
    _searchbox.addTextChangedListener(textwatcher); 
} 

Runnable runn = new Runnable() 
{ 
    @Override 
    public void run() 
    { 
     // TODO Auto-generated method stub 
     handlersearch.sendEmptyMessage(0); 
    } 
}; 
TextWatcher textwatcher = new TextWatcher() 
{ 
    public void onTextChanged(CharSequence s, int start, int before,int count) 
    { 
     Log.i("---onTextChanged ----", "---------onTextChanged ----"); 
     if (_searchbox.getText().toString().length() > 2) 
     { 
      query = _searchbox.getText().toString().replace(" ", "%20"); 
      handlersearch.removeCallbacks(runn); 
      handlersearch.post(runn); 
     } else 
     { 
      showprogress.setVisibility(View.GONE); 
      if (dotask != null) 
      { 
       if (dotask.getStatus().equals(AsyncTask.Status.RUNNING)) 
       { 
        dotask.cancel(true); 
       } 
      } 

      handlersearch.removeCallbacks(runn); 
      _productList = new ArrayList<Helperdb>(); 
      _productList.clear(); 
      _listview.setAdapter(new CustomBaseAdapter(Tab3.this,_productList)); 
     } 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count,int after) 
    { 
     // TODO Auto-generated method stub 
    } 

    public void afterTextChanged(Editable s) 
    { 
     // TODO Auto-generated method stub 
    } 
}; 

Handler handlersearch = new Handler() 
{ 
    public void handleMessage(android.os.Message msg) 
    { 
     dotask = new searchtask(); 
     dotask.execute(); 
    }; 
}; 

private class searchtask extends AsyncTask<Void, Void, Void> 
{ 
    protected void onPreExecute() 
    { 
     showprogress.setVisibility(View.VISIBLE); 
    }; 

    protected void onPostExecute(Void param) 
    { 
     // animation.dismiss(); 
     showprogress.setVisibility(View.GONE); 
     if (_productList == null) 
     return; 
     ArrayList<String> item = new ArrayList<String>(); 
     for (int i = 0; i < _productList.size(); i++) 
     { 
      item.add(_productList.get(i).name); 
     } 

     myAutoComplete = (AutoCompleteTextView) findViewById(R.id.myautocomplete); 

     myAutoComplete.addTextChangedListener(Tab3.this); 

     myAutoComplete.setAdapter(new ArrayAdapter<String>(Tab3.this,android.R.layout.simple_dropdown_item_1line, item)); 

     _productList_Temp = new ArrayList<Helperdb>(); 
     for (int i = 0; i < _productList.size(); i++) 
     { 
      _productList_Temp.add(_productList.get(i)); 
     } 
     _listview.setAdapter(new CustomBaseAdapter(Tab3.this,_productList_Temp)); 
    } 

    @Override 
    protected Void doInBackground(Void... params) 
    { 
     db = new SqliteDbTab(getApplicationContext()); 
     db.getWritableDatabase(); 
     ArrayList<Helperdb> product_list = db.getProudcts(query); 

     for (int i = 0; i < product_list.size(); i++) 
     { 
      String tidno = product_list.get(i).getid(); 
      System.out.println("tidno>>>>>" + tidno); 
      String tname = product_list.get(i).getName(); 
      String tphone = product_list.get(i).getPhone(); 
      Helperdb _ProductModel = new Helperdb(); 
      _ProductModel.setid(tidno); 
      _ProductModel.setName(tname); 
      _ProductModel.setPhone(tphone); 
      _productList.add(_ProductModel); 
     } 
    // _productList = _parser.getProductList(); 
     return null; 
    } 
} 

private class CustomBaseAdapter extends BaseAdapter 
{ 
    LayoutInflater _inflater; 
    List<Helperdb> productList; 

    public CustomBaseAdapter(Context context, List<Helperdb> productList) 
    { 
     _inflater = LayoutInflater.from(context); 
     this.productList = productList; 
    } 

    public int getCount() 
    { 
     // TODO Auto-generated method stub 
     return productList.size(); 
    } 

    public Object getItem(int position) 
    { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public long getItemId(int position) 
    { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     ViewHolder _holder; 
     if (convertView == null) 
     {    
      convertView = _inflater.inflate(R.layout.contactlist, null); 
      _holder = new ViewHolder(); 
      _holder.name = (TextView) convertView.findViewById(R.id.txt_title_text); 
      _holder.phone = (TextView) convertView.findViewById(R.id.txt_price); 
     convertView.setTag(_holder); 
     } else 
     { 
      _holder = (ViewHolder) convertView.getTag(); 
     } 

     _holder.name.setText(productList.get(position).name.trim()); 
     _holder.phone.setText(productList.get(position).phone); 
     return convertView; 
    } 

    private class ViewHolder 
    { 
     TextView name; 
     TextView phone; 
    } 
} 

@Override 
public void afterTextChanged(Editable arg0) 
{ 
    // TODO Auto-generated method stub 
} 

@Override 
public void beforeTextChanged(CharSequence s, int start, int count,int after) 
{ 
    // TODO Auto-generated method stub 
} 

@Override 
public void onTextChanged(CharSequence s, int start, int before, int count) 
{ 
    // TODO Auto-generated method stub 
} 
    } 

LogCat файл:

12-13 10:47:50.269: I/---onTextChanged ----(2307): ---------onTextChanged ---- 
12-13 10:47:50.489: I/---onTextChanged ----(2307): ---------onTextChanged ---- 
12-13 10:47:50.729: I/---onTextChanged ----(2307): ---------onTextChanged ---- 
    12-13 10:47:51.569: I/---onTextChanged ----(2307): ---------onTextChanged ---- 
    12-13 10:47:51.769: I/---onTextChanged ----(2307): ---------onTextChanged ---- 
    12-13 10:47:51.909: I/---onTextChanged ----(2307): ---------onTextChanged ---- 
    12-13 10:47:52.649: I/---onTextChanged ----(2307): ---------onTextChanged ---- 
    12-13 10:47:53.749: I/---onTextChanged ----(2307): ---------onTextChanged ---- 
    12-13 10:47:53.808: I/---onTextChanged ----(2307): ---------onTextChanged ---- 
    12-13 10:47:55.478: I/---onTextChanged ----(2307): ---------onTextChanged ---- 
    12-13 10:47:55.598: I/System.out(2307): tidno>>>>>5 

пожалуйста, помогите мне: я делаю поиск с извлечением полной name..not получая при I введено только первое письмо ...

! [Поиск UI]

+3

Что именно проблема? – Szymon

+0

У вас есть ошибки? если да, пожалуйста, напишите logcat. – Andrain

+0

нет ошибок, выполняющих поиск, когда я ввел полное имя, затем его поиск .. это не принимает первый алфавит .. – Hemanth11MS

ответ

0

Я думаю, вы должны попробовать "имя" + "= ' "+ запись +" %'" вы можете попробовать запрос, как этот

String SEARCH_QUERY = "SELECT * FROM TABTABLE WHERE name like '"+record+"%'"; 

Cursor cursor = db.rawQuery(SEARCH_QUERY, null); 
cursor.moveToFirst(); 
if (cursor.getCount() > 0) 
{ 
    do { 
       Helperdb item = new Helperdb(); 
       item.id = cursor.getString(cursor.getColumnIndex("ID")); 
       item.name = cursor.getString(cursor.getColumnIndex("name")); 
       item.phone = cursor.getString(cursor.getColumnIndex("PhOnE")); 
       item.mail = cursor.getString(cursor.getColumnIndex("mail"));      
       item.address = cursor.getString(cursor.getColumnIndex("address"));     
       item.image = cursor.getBlob(cursor.getColumnIndex("image")); 
       cartList.add(item); 
    } while (cursor.moveToNext()); 
} 
cursor.close(); 
+0

спасибо vaibhav – Hemanth11MS

+0

приветствую брата .. Happy Coding :) –

+0

..yes ... все еще не принимаю Первый алфавит .. он принимает после 2 алфавитов. – Hemanth11MS

Смежные вопросы