SQLite和自定义ListView。我做错了什么?

时间:2017-09-28 18:24:45

标签: java android sqlite

编辑了更多信息:

这是数据库架构:

 private static final String SQL_CREATE =
        "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
        COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COLUMN_WINE_NAME + " TEXT, " +
        COLUMN_WINE_PRICE + " TEXT, " +
        COLUMN_WINE_DESCRIPTION + " TEXT, " +
        COLUMN_WINE_TEMP + " TEXT, " +
        COLUMN_WINE_STORE + " TEXT);";

所以我制作了一个列表,我希望每一行都有名称和描述。

数据来自应用程序生成的SQLite数据库。数据在数据库中正确。

问题是目前我收到错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jeremy.sqlwine/com.example.jeremy.sqlwine.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist

这很奇怪,因为该列肯定存在于数据库中。

此刻我感到有些失落。我查了一堆指南,看了一些关于这些东西的youtube教程,但我不能完全连接点。

我能获得一些指导吗?

以下是相关活动。

主要活动:

public class MainActivity extends AppCompatActivity {

DatabaseHelper mDatabaseHelper;
WineCursorAdapter mCursorAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intentGoToAdd = new Intent(MainActivity.this, AddActivity.class);
            startActivity(intentGoToAdd);
        }
    });
    mDatabaseHelper = new DatabaseHelper(this);

    SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
    ListView wineListView = (ListView) findViewById(R.id.list);

    View emptyView = findViewById(R.id.empty_view);
    wineListView.setEmptyView(emptyView);

    //Selecting what details I want to put into each row in the list (just the name and the description)
    String[] projection ={
            WineContract.WineEntry.COLUMN_WINE_NAME,
            WineContract.WineEntry.COLUMN_WINE_DESCRIPTION,};

    //This should select only the current cursor's name and description columns
    Cursor cursor = db.query(WineContract.WineEntry.TABLE_NAME, projection, null, null, null, null,null);

    mCursorAdapter = new WineCursorAdapter(this, cursor);
    wineListView.setAdapter(mCursorAdapter);

}

@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_deleteDB) {
        mDatabaseHelper.deleteDatabase();
        Toast.makeText(this, "Database deleted", Toast.LENGTH_SHORT).show();
        onResume();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

光标适配器:

public class WineCursorAdapter extends CursorAdapter {

public WineCursorAdapter(Context context, Cursor c) {
    super(context, c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    //Populate the views
    TextView textViewName = (TextView) view.findViewById(R.id.wineName);
    TextView textViewDescription = (TextView) view.findViewById(R.id.wineDescription);

    //get the info from cursor
    String wineName = cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_NAME));
    String wineDescription = cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_DESCRIPTION));

    textViewName.setText(wineName);
    textViewDescription.setText(wineDescription);
}

}

WineContract:

public class WineContract {

public WineContract() {
}

public static class WineEntry implements BaseColumns{

    public static final String TABLE_NAME = "wines";
    public static final String COLUMN_ID = "_ID";
    public static final String COLUMN_WINE_NAME = "NAME";
    public static final String COLUMN_WINE_PRICE = "PRICE";
    public static final String COLUMN_WINE_DESCRIPTION = "DESCRIPTION";
    public static final String COLUMN_WINE_TEMP = "TEMP";
    public static final String COLUMN_WINE_STORE = "STORE";

}

}

这里是list_item(列表中的每一行包含的内容)xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/wineName"
    android:text="Name"
    android:textSize="16dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/wineDescription"
    android:text="Description"
    android:textSize="16dp"/>

1 个答案:

答案 0 :(得分:0)

我最终解决了这个问题。

出于某种原因,Android Studio专门针对“_id”。我不得不改变这行代码:

public static final String COLUMN_ID = "_ID";

public static final String COLUMN_ID = "_id";

这解决了这个问题。我发誓我之前完成了_ID并且它有效。但无论哪种方式,它现在都在运作。

希望这可以帮助任何有同样问题的人!