我已经阅读了很多主题,但是我仍然找不到如何从ListView中的数据库中获取光标的答案。当我将Log.d日志输出到控制台时,但我无法理解如何在ListView中引入“string_word”。我试着用onItemClick做它,但我不知道我需要在方法中写什么:(这是代码:
public class MainActivity extends Activity implements OnClickListener {
final String LOG_TAG = "myLogs";
IdDB idh;
SynDB sqh;
SQLiteDatabase sqdb, iddb;
Button btnOk;
EditText etWord;
String eWord;
public String string_word;
public String[] syns;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etWord = (EditText) findViewById(R.id.etWord);
btnOk = (Button) this.findViewById(R.id.btnOk);
ListView lvMain = (ListView) findViewById(R.id.lvMain);
btnOk.setOnClickListener(btnOkListener);
//initialize our class-cover IdDB
idh = new IdDB(this);
// initialize our class-cover SynDB
sqh = new SynDB(this);
// we need db to read and write
sqdb = sqh.getWritableDatabase();
iddb = idh.getWritableDatabase();
// создаем адаптер
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, syns);
// присваиваем адаптер списку
lvMain.setAdapter(adapter);
setContentView(lvMain);
lvMain.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
}
});
}
public OnClickListener btnOkListener = new OnClickListener(){
@Override
public void onClick(View v) {
eWord = etWord.getText().toString();
switch (v.getId()){
case R.id.btnOk:
String query = // Long SQL query out "word"-> string_word
Log.d(LOG_TAG, query);
Log.d(LOG_TAG, "--- Rows in mytable: ---");
// делаем запрос всех данных из таблицы words, получаем Cursor
Cursor cursor = sqdb.rawQuery(query, null);
int wordColIndex = cursor.getColumnIndex(SynDB.Word);
while (cursor.moveToNext()) {
string_word = cursor.getString(wordColIndex);
Log.d(LOG_TAG, "word = "+ string_word);
syns = new String[] {string_word};
}
cursor.close();
break;
default:
break;
} // close switch
} // close onClick
};
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
// закрываем соединения с базой данных
sqdb.close();
sqh.close();
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View view) {
}
}
logcat的:
5-10 10:51:19.678 1642-1642/ru.psdevteam.syndicttest.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb57db4f0)
05-10 10:51:19.678 1642-1642/ru.psdevteam.syndicttest.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.psdevteam.syndicttest.app/ru.psdevteam.syndicttest.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at java.util.Arrays$ArrayList.<init>(Arrays.java:47)
at java.util.Arrays.asList(Arrays.java:169)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
at ru.psdevteam.syndicttest.app.MainActivity.onCreate(MainActivity.java:57)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
答案 0 :(得分:1)
public String[] syns;
你的syns
未初始化。您应该在创建ArrayAdapter之前初始化它
答案 1 :(得分:0)
Try to add this pieace of code :
// создаем адаптер
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, syns);
// присваиваем адаптер списку
lvMain.setAdapter(adapter);
After this code:
while (cursor.moveToNext()) {
string_word = cursor.getString(wordColIndex);
Log.d(LOG_TAG, "word = "+ string_word);
syns = new String[] {string_word};
}
cursor.close();
答案 2 :(得分:0)
您可以将活动的内容视图设置两次:
setContentView(R.layout.main);
setContentView(lvMain);
在我在这里提到的第二行中,您将列表视图设置为活动视图,这是不正确的。每个活动的ContentView应该引用布局,而不仅仅是列表视图。 只需从代码中删除第二行并重建它。
您可以在refrence上检查setContentView的定义: