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

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

Why doesn't it get any data?

问题

Main:

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

DATABASE HELPER:

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

SECOND ACTIVITY:

    Intent intent = getIntent();
    int id = intent.getIntExtra("Id", 0);
    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:

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

DATABASE HELPER:

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

SECOND ACTIVITY:

Intent intent = getIntent();
    int id =intent.getIntExtra(&quot;Id&quot;,0);
        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

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

In Activity make a setViews function

public void setViews() {
    Cursor cursor = mDatabase.getSpecificData(id);
    try {
        if (cursor.getCount() != 0) {
            if (cursor.moveToFirst()) {
                textview1.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_1_NAME)));
                textview2.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_2_NAME)));
                //and so on
            }
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
英文:

Your Database Helper

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

In Activity make a setViewsfunction

public void setViews() {
            Cursor cursor = mDatabase.getSpecificData(id);
            try {
                if (cursor.getCount() != 0) {
                    if (cursor.moveToFirst()) {
                        textview1.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_1_NAME)));
                        textview2.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_2_NAME)));
                        //and so on
    
                    }
    
                }
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        }

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:

确定