试图创建一个sqlite数据库搜索应用程序

时间:2015-01-11 23:43:20

标签: java android eclipse sqlite

我是Android新手,已经在这个应用程序上工作了一段时间。我正在使用3列将初始值加载到sqlite数据库中:_id,product和ingredients。我有一个EditText框供用户搜索产品,看看它们是否含有某种成分。然后打印出列表视图,显示具有这些成分的产品。我在使用这段代码时遇到了一些问题,特别是当我运行它时,它会显示“无法从logcat读取第0行第1列”。

更新:感谢托德我修复了原来的“无法读取第0行和第1列”的问题,但现在当我点击带有编辑文本输入“Apple”的搜索按钮(应输出一个产品)时,应用程序没有t更新并保持静态。任何帮助将不胜感激!

MySQLiteHelper.java

package com.lapetit;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {

  public static final String TABLE_PRODUCTS = "products";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_PRODUCT = "product";
  public static final String COLUMN_INGREDIENTS = "ingredients";
  private SQLiteDatabase database;

  private static final String DATABASE_NAME = "products.db";
  private static final int DATABASE_VERSION = 1;

  // Database creation sql statement
  private static final String DATABASE_CREATE = "create table "
      + TABLE_PRODUCTS + "(" + COLUMN_ID
      + " integer primary key autoincrement, " + COLUMN_PRODUCT
      + " text not null, " + COLUMN_INGREDIENTS + " text not null);";

  public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);

    Cursor check = database.rawQuery("select * from products",null);

    //First Time we open Database, add default Values
    if ( check.getCount() < 1 )
    {

        database.execSQL("insert into " + TABLE_PRODUCTS + "(" + COLUMN_ID + ","
                + COLUMN_PRODUCT + "," + COLUMN_INGREDIENTS + ")" + "values(1,'Le Mieux Retinol Serum', 'Apples,Bananas,Carrots')");

        database.execSQL("insert into " + TABLE_PRODUCTS + "(" + COLUMN_ID + ","
                + COLUMN_PRODUCT + "," + COLUMN_INGREDIENTS + ")" + "values(2,'Le Mieux Essence Toner', 'Apricots, Beets, Cats')");

        database.execSQL("insert into " + TABLE_PRODUCTS + "(" + COLUMN_ID + ","
                + COLUMN_PRODUCT + "," + COLUMN_INGREDIENTS + ")" + "values(3,'Le Mieux Body Wash', 'Alcohol, Marijuana, Meth')");

    }
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(MySQLiteHelper.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
    onCreate(db);
  }


}

Products.java

package com.lapetit;

public class Products {
      private long id;
      private String product;
      private String ingredient;

      public long getId() {
        return id;
      }

      public void setId(long id) {
        this.id = id;
      }

      public String getProduct() {
        return product;
      }

      public void setProduct(String product) {
        this.product = product;
      }

      public String getIngredients() {
            return product;
      }

      public void setIngredients(String ingredient) {
          this.ingredient = ingredient;
      }

      // Will be used by the ArrayAdapter in the ListView
      @Override
      public String toString() {
        return product;
      }

    } 

ProductsDataSource.java

    package com.lapetit;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ProductsDataSource {

  // Database fields
  private SQLiteDatabase database;
  private MySQLiteHelper dbHelper;
  private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
      MySQLiteHelper.COLUMN_PRODUCT,
      MySQLiteHelper.COLUMN_INGREDIENTS};

  public ProductsDataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

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


  public List<Products> getAllProducts() {
    List<Products> products = new ArrayList<Products>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_PRODUCTS,
        allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Products product = cursorToProduct(cursor);
      products.add(product);
      cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return products;
  }



  //Search queries
  public List<Products> getSearchedProducts(String search) {
      List<Products> products = new ArrayList<Products>();

      String[] args = new String[1];
      args[0] = "%"+search+"%";
      Cursor cursor = database.rawQuery("SELECT product FROM products, _id WHERE ingredients like ?", args);

      cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
          Products product = cursorToProduct(cursor);
          products.add(product);
          cursor.moveToNext();
        }

      cursor.close();
      return products;
  }


  private Products cursorToProduct(Cursor cursor) {
    Products product = new Products();
    product.setId(cursor.getLong(0));
    product.setProduct(cursor.getString(1));
    return product;
  }


} 

WithActivity.java

package com.lapetit;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.widget.ImageView;


public class WithActivity extends ListActivity {
      private ProductsDataSource datasource;
      private ArrayAdapter adapter;

      List<Products> values = new ArrayList<Products>();

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.with);


        EditText inputSearch = (EditText) findViewById(R.id.text);

        datasource = new ProductsDataSource(this);
        datasource.open();




        ArrayAdapter<Products> adapter = new ArrayAdapter<Products>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

      }

      public void onClick(View view) {

            ArrayAdapter<Products> adapter = (ArrayAdapter<Products>) getListAdapter();
            Products product = null;

            switch (view.getId()) {
            case R.id.search:

                EditText inputSearch = (EditText) findViewById(R.id.text);

                String stringinput = (String)inputSearch.getText().toString();
                List<Products> values = datasource.getSearchedProducts(stringinput);


              break;
            }

            adapter.notifyDataSetChanged();
          }


      @Override
      protected void onResume() {
        datasource.open();
        super.onResume();
      }

      @Override
      protected void onPause() {
        datasource.close();
        super.onPause();
      }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.splash, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

with.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/text"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:hint="Search products.."
        android:inputType="text" />

    <Button
            android:id="@+id/search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search" 
            android:onClick="onClick"/>


  <ListView
      android:id="@android:id/list"
      android:layout_width="fill_parent"
      android:layout_height="400dp" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

  

无法从包含3行1列的CursorWindow中读取第0行第1列。

我认为您的查询需要返回更多列...

getSearchedProducts()中,您只选择产品列:

// Select a single column.
Cursor cursor = database.rawQuery("SELECT product FROM products WHERE ingredients like ?", args);

但在cursorToProduct()中你读了两列(ID和Product):

product.setId(cursor.getLong(0));
product.setProduct(cursor.getString(1));

您似乎需要将Id添加到查询中。

Cursor cursor = database.rawQuery("SELECT id, product FROM products WHERE ingredients like ?", args);