为什么它没有获取任何数据?

huangapple go评论145阅读模式
英文:

Why doesn't it get any data?

问题

Main:

  1. ColegiListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  2. @Override
  3. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  4. Intent intent = new Intent(MainActivity.this, ColegDataEditActivity.class);
  5. intent.putExtra("Id", Integer.parseInt(Long.toString(id)));
  6. Log.i("Id", id + "");
  7. startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());
  8. }
  9. });

DATABASE HELPER:

  1. public Cursor getSpecificData(int id) {
  2. SQLiteDatabase db = this.getWritableDatabase();
  3. Cursor data = db.rawQuery("SELECT * FROM " + TableName + " WHERE ID =" + id, null);
  4. return data;
  5. }

SECOND ACTIVITY:

  1. Intent intent = getIntent();
  2. int id = intent.getIntExtra("Id", 0);
  3. Cursor data = mDatabase.getSpecificData(id);

Hi! I am a beginner with SQL and I don't know what I did wrong. I have a database with multiple columns and I need to get some data from it so then I can put those values in some TextViews. The problem is that the data I need needs to be a single row with the specific id from an item from a ListView (yeah, I know about row id, I solved that problem). Any ideas why isn't it working?

英文:

Main:

  1. ColegiListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  2. @Override
  3. public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  4. Intent intent = new Intent(MainActivity.this, ColegDataEditActivity.class);
  5. intent.putExtra(&quot;Id&quot;, Integer.parseInt(Long.toString(id)));
  6. Log.i(&quot;Id&quot;,id+&quot;&quot;);
  7. startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());
  8. }
  9. });

DATABASE HELPER:

  1. public Cursor getSpecificData(int id){
  2. SQLiteDatabase db = this. getWritableDatabase();
  3. Cursor data = db.rawQuery(&quot;SELECT * FROM &quot;+ TableName + &quot; WHERE ID =&quot;+id, null);
  4. return data;
  5. }

SECOND ACTIVITY:

  1. Intent intent = getIntent();
  2. int id =intent.getIntExtra(&quot;Id&quot;,0);
  3. Cursor data = mDatabase.getSpecificData(id);

Hi! I am a beginner with SQL and I don't know what I did wrong. I have a database with multiple columns and I need to get some data from it so then I can put those values in some TextViews. The problem is that the data I need needs to be a single row with the specific id from an item from a ListView (yeah, I now about row id, I solved that problem). Any ideas why isn't it working?

答案1

得分: 1

Your Database Helper

  1. public Cursor getSpecificData(int id){
  2. SQLiteDatabase db = this.getWritableDatabase();
  3. String query = "SELECT * FROM " + TableName + " WHERE ID = " + id;
  4. Cursor res = db.rawQuery(query, null);
  5. return res;
  6. }

In Activity make a setViews function

  1. public void setViews() {
  2. Cursor cursor = mDatabase.getSpecificData(id);
  3. try {
  4. if (cursor.getCount() != 0) {
  5. if (cursor.moveToFirst()) {
  6. textview1.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_1_NAME)));
  7. textview2.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_2_NAME)));
  8. //and so on
  9. }
  10. }
  11. } finally {
  12. if (cursor != null) {
  13. cursor.close();
  14. }
  15. }
  16. }
英文:

Your Database Helper

  1. public Cursor getSpecificData(int id){
  2. SQLiteDatabase db = this.getWritableDatabase();
  3. String query = &quot;SELECT * FROM &quot;+ TableName + &quot; WHERE ID = &quot;+id;
  4. Cursor res = db.rawQuery(query, null);
  5. return res;
  6. }

In Activity make a setViewsfunction

  1. public void setViews() {
  2. Cursor cursor = mDatabase.getSpecificData(id);
  3. try {
  4. if (cursor.getCount() != 0) {
  5. if (cursor.moveToFirst()) {
  6. textview1.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_1_NAME)));
  7. textview2.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_2_NAME)));
  8. //and so on
  9. }
  10. }
  11. } finally {
  12. if (cursor != null) {
  13. cursor.close();
  14. }
  15. }
  16. }

huangapple
  • 本文由 发表于 2020年4月10日 22:39:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61142618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定