【发布时间】:2015-01-04 09:45:59
【问题描述】:
package at.thesis.ticmip;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Mainadminvdictionary extends ListActivity {
private ArrayList<String> results = new ArrayList<String>();
private String ddct = Databaseadapter.dtbldctnry;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_vdictionary);
SQLiteDatabase db= openOrCreateDatabase(Databaseadapter.DATABASE_NAME,MODE_PRIVATE, null);
try {
Cursor c= db.rawQuery("select * from Ddictionary", null);
//Looping through all rows
if (c != null ) {
if (c.moveToFirst()) {
do {
String dss = c.getString(c.getColumnIndex("disease"));
results.add(dss);
}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
db.close();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
}
我在这里有我的代码,它将显示数据并在列表视图中显示我现在的问题是如何通过其 ID 单击项目?我尝试使用 protected void onListItemClick(ListView l, View v, int position, long id) {} 但它不起作用
【问题讨论】:
-
覆盖onListItemClick是你需要的,你只需要弄清楚为什么不起作用
-
“id”是什么意思?在
ListView或数据库中的主键上的位置? -
“我尝试使用 protected void onListItemClick(...) {} 但它不起作用”:不要只说“它不起作用”之类的话 - 发布您为该方法尝试的代码。
-
protected void onListItemClick(AdapterView> l, View v, int position, long id) { String item = (String) getListAdapter().getItem(position); if (position == 0) { Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); Intent intent = new Intent(this, Mainuserstop10.class);开始活动(意图); } }
-
请不要在 cmets 中发布代码 - 您的问题左下方有一个“编辑”选项 - 使用它来编辑问题并添加代码。另外,解释一下“不起作用”是什么意思——它是崩溃还是什么都不做?