[英]how I can get the data from the selected item in the listView from my database and view it on alertdialog?
I am having trouble with a ListView I created: 我在创建的ListView时遇到麻烦:
I have a food database but how I can get the data from the selected item in the listView from my database and view it on alertdialog? 我有一个食物数据库,但是如何从数据库的listView中的所选项目中获取数据并在alertdialog上查看呢?
I can view the data but when I click any of it, my application will stop working. 我可以查看数据,但是当我单击任何数据时,我的应用程序将停止运行。
My code for this looks like: 我的代码如下:
public class MainActivity extends AppCompatActivity {
ListView listView;
EditText editText;
private DatabaseHelperList dbHelper;
Adapter adapter;
ArrayList<Item> arrayList = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//loadDatabase
dbHelper = new DatabaseHelperList(this);
try {
dbHelper.checkAndCopyDatabase();
dbHelper.openDatabase();
} catch (SQLException e) {
}
try {
Cursor cursor = dbHelper.QueryData("select *from menulist");
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
Item item = new Item();
item.setId(cursor.getString(0));
item.setFood(cursor.getString(1));
item.setCalories(cursor.getString(2));
arrayList.add(item);
} while (cursor.moveToNext());
}
}
} catch (SQLException e) {
}
adapter = new Adapter(this, R.layout.custom_list_item, arrayList);
listView = (ListView) findViewById(R.id.list_item);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
Cursor res = dbHelper.getAllData("select " + position + " from menulist");
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("Weight: " + res.getString(1) + " kg \n");
buffer.append("BMI: " + res.getString(2) + "\n");
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
builder.setTitle("Confirmation:");
builder.setMessage(res.getInt(res.getColumnIndex("id")));
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent startNewActivity = new Intent(MainActivity.this, SQLiteDemoActivity.class);
startActivity(startNewActivity);
}
}).setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.show();
}
}
);
}
}
This the log: 这条日志:
12-31 23:50:27.734 15775-15775/com.example.hidir.mnutrihealth E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hidir.mnutrihealth, PID: 15775
android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
at com.example.hidir.mnutrihealth.MainActivity$1.onItemClick(MainActivity.java:76)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1162)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2953)
at android.widget.AbsListView$3.run(AbsListView.java:3708)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
My first advice : 我的第一条建议:
Create a class DAO for all the resquet into a database. 为所有重新创建数据库创建类DAO。 Here, it will become unreadable if you add more than one call. 在这里,如果您添加多个呼叫,它将变得不可读。
Second : 第二:
Are you sure that : 您确定:
while (res.moveToNext()) {
buffer.append("Weight: " + res.getString(1) + " kg \n");
buffer.append("BMI: " + res.getString(2) + "\n");
} }
Weight is a String and BMI a String too ? 重量也是字符串,而BMI也是字符串吗?
If Weight is a int or a double, i think you have to use res.getInt(1) or res.getDouble(1). 如果Weight是int或double,我认为您必须使用res.getInt(1)或res.getDouble(1)。 Check the real method, i don't remember is you have to specify the "column name" 检查实际方法,我不记得您必须指定“列名”
Edit : I think i found the issue : 编辑:我认为我发现了问题:
while (res.moveToNext()) {
buffer.append("Weight: " + res.getString(1) + " kg \n");
buffer.append("BMI: " + res.getString(2) + "\n");
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
builder.setTitle("Confirmation:");
builder.setMessage(res.getInt(res.getColumnIndex("id")));
look at the last line. 看最后一行。 You would like to have the integer of something, but the cursor is now a the end of the list of item that were asking before. 您想要一个整数,但是光标现在位于之前询问的项目列表的末尾。
You do : 你做 :
A while loop for putting all the element on a StringBuffer, and when it finish, you ask a another time to access to a element that doesn't exist anymore. 将所有元素放在StringBuffer上的while循环,完成后,您需要再等待一次以访问不再存在的元素。
Answer is in the Log. 答案在日志中。 It says, CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
at com.example.hidir.mnutrihealth.MainActivity$1.onItemClick(MainActivity.java:76)
. 它说, CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
com.example.hidir.mnutrihealth.MainActivity$1.onItemClick(MainActivity.java:76)
CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
。
The problem is here, 问题在这里,
builder.setMessage(res.getInt(res.getColumnIndex("id")));
At the time when you call the method res.getInt()
, size of cursor is 5 (means index values from 0 to 4) but it is pointing at index 5. That's why it is throwing CursorIndexOutOfBoundsException
. 在调用方法res.getInt()
,游标的大小为5(表示索引值从0到4),但是它指向索引5。这就是为什么它抛出CursorIndexOutOfBoundsException
的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.