我在从数据库表中返回所有不同的名称时遇到问题。该表包含以下列:
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_BETRAG = "betrag";
public static final String KEY_GRUND = "grund";
public static final String KEY_DATUM = "datum";
public static final String KEY_ICONID = "icon";
public static final int COL_ROWID = 0;
public static final int COL_NAME = 1;
public static final int COL_BETRAG = 2;
public static final int COL_GRUND = 3;
public static final int COL_DATUM = 4;
public static final int COL_ICONID = 5;
public static final String[] ALL_KEYS = new String[] {KEY_ID, KEY_NAME, KEY_BETRAG,KEY_GRUND, KEY_DATUM, KEY_ICONID};
public static final String[] ALL_DIFFERENTNAMES = new String[] {KEY_NAME};
private static final String DATABASE_CREATE_TABLE1 =
"create table " + DATABASE_TABLE1
+ " (" + KEY_ID + " integer primary key autoincrement, "
+ KEY_NAME + " text not null, "
+ KEY_BETRAG + " real not null, "
+ KEY_GRUND + " text not null, "
+ KEY_DATUM + " text not null, "
+ KEY_ICONID + " integer not null"
+ ");";
使用以下方法,我想返回所有不同的名称,但它毕竟不起作用
public Cursor getAllDifferentNames1() {
// String where = "SELECT DISTINCT " + KEY_NAME + " FROM " + DATABASE_TABLE1;
Cursor c = db.query(true, DATABASE_TABLE1, ALL_DIFFERENTNAMES, null, null, null, null, null, null);
if(c != null) {
c.moveToFirst();
}
return c;
}
当我运行应用程序时,收到此错误消息:
01-04 16:18:54.878: E/AndroidRuntime(28638): FATAL EXCEPTION: main
01-04 16:18:54.878: E/AndroidRuntime(28638): java.lang.IllegalArgumentException: column '_id' does not exist
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:302)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:333)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:81)
01-04 16:18:54.878: E/AndroidRuntime(28638): at fragments.Schuldet_mir.populateListViewFromDB(Schuldet_mir.java:243)
01-04 16:18:54.878: E/AndroidRuntime(28638): at fragments.Schuldet_mir.onResume(Schuldet_mir.java:99)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.support.v4.app.Fragment.performResume(Fragment.java:1543)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:963)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.View.measure(View.java:15284)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.View.measure(View.java:15284)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.widget.LinearLayout.measureVertical(LinearLayout.java:861)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.widget.LinearLayout.onMeasure(LinearLayout.java:602)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.View.measure(View.java:15284)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-04 16:18:54.878: E/AndroidRuntime(28638): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2367)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.View.measure(View.java:15284)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1867)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1292)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1009)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4236)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.Choreographer.doFrame(Choreographer.java:525)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.os.Handler.handleCallback(Handler.java:615)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.os.Handler.dispatchMessage(Handler.java:92)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.os.Looper.loop(Looper.java:137)
01-04 16:18:54.878: E/AndroidRuntime(28638): at android.app.ActivityThread.main(ActivityThread.java:4872)
01-04 16:18:54.878: E/AndroidRuntime(28638): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 16:18:54.878: E/AndroidRuntime(28638): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 16:18:54.878: E/AndroidRuntime(28638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-04 16:18:54.878: E/AndroidRuntime(28638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-04 16:18:54.878: E/AndroidRuntime(28638): at dalvik.system.NativeStart.main(Native Method)
有人可以告诉我,我做错了吗?
修改
Schuldet_mir课程第99行的方法:
populateListViewFromDB();
......
public void populateListViewFromDB() { //ListView mit Werten aus der db befüllen
getActivity().startManagingCursor(cursor = mainActivity.myDBHandler.getAllDifferentNames1());
String[] dbSpaltenNamen = new String[] {DataBaseHandler.KEY_NAME, //die spalteneinträge wie
DataBaseHandler.KEY_BETRAG, //angeordnet in die jeweiligen
DataBaseHandler.KEY_DATUM, //id's des custom listzeile zuordnen
DataBaseHandler.KEY_ICONID};
int[] zuListViewIDs = new int[] {R.id.txt_name, //hier die id's. name zu name
R.id.txt_betrag, //betrag zu betrag...
R.id.datum,
R.id.list_userImage};
SimpleCursorAdapter myCursorAdapter
= new SimpleCursorAdapter
(getActivity(), R.layout.list_zeile, cursor, dbSpaltenNamen, zuListViewIDs);
verliehenesGeld.setText("" + mFormater.formatDecimal(mainActivity.myDBHandler.getColBetragInTable1()));
if(mainActivity.myDBHandler.getContactsCountInTable1() > 0) {
anzahlEinträge.setText("Einträge: " + mainActivity.myDBHandler.getContactsCountInTable1());
}
else {
anzahlEinträge.setText("Keine Einträge");
}
listView.setAdapter(myCursorAdapter);
}
答案 0 :(得分:0)
所有列表视图都需要“_id”列。您用于生成游标的查询不会要求该列,因此listadapter在尝试处理它时会失败。以下内容应该有效:
public static final String[] ALL_DIFFERENTNAMES = new String[] {KEY_ID, KEY_NAME};
答案 1 :(得分:0)
您发布的代码没有显示,但我认为您正尝试访问从getAllDifferentNames1()返回的游标上的_id列。那个光标没有那个列。
更改
public static final String[] ALL_DIFFERENTNAMES = new String[] {KEY_NAME};
致:
public static final String[] ALL_DIFFERENTNAMES = new String[] {KEY_ID,KEY_NAME};
编辑
看到方法populateListViewFromDB()
后,您应该将ALL_DIFFERENTAMES
数组更改为:
public static final String[] ALL_DIFFERENTNAMES =
new String[] {KEY_ID, KEY_NAME, KEY_BETRAG, KEY_DATUM, KEY_ICONID};
你混合了两件不同的东西 有一件事是你想在listview中的列,其他东西是你想要显示的行(数据)。
行(数据)是游标,取决于查询集。您的查询似乎是正确的。
如果您只想在列表视图中显示列name
,则应将用于listview
的视图更改为只有一个textview
,数组dbSpaltenNamen
应仅包含DataBaseHandler.KEY_NAME
DIFFERENTNAMES
并使用您原来的{{1}}
有关详细信息,请参阅this tutorial