android - 如何在 TextView 中从 SQLite 中检索单个数据?

标签 android database android-sqlite

这个时候我需要你们的帮助,我有一个问题如何从数据库中获取单个数据到TextView

这是来自 DBDataSource.java 的代码

package com.example.database.search;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.DatePicker;

public class DBDataSource {
private SQLiteDatabase database;
private DBHelper dbHelper;
private String[] allColumns = { DBHelper.COLUMN_ID,
          DBHelper.COLUMN_NAMA_SMA, 
          DBHelper.COLUMN_ALAMAT_SMA, 
          DBHelper.COLUMN_LATITUDE_SMA, 
          DBHelper.COLUMN_LONGTITUDE_SMA,
          DBHelper.COLUMN_TANGGAL_BERDIRI_SMA, 
          DBHelper.COLUMN_EMAIL_SMA, 
          DBHelper.COLUMN_WEBSITE_SMA, 
          DBHelper.COLUMN_FACEBOOK,
          DBHelper.COLUMN_TWITTER };
public DBDataSource(Context context)
{
    dbHelper = new DBHelper(context);
}

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}
private Sma cursorToSma(Cursor cursor)
{

    Sma sekolah = new Sma();
    Log.v("info", "The getLONG "+cursor.getLong(0));
    Log.v("info", "The setLatLng "+cursor.getString(1)+","+cursor.getString(2));

    sekolah.setId(cursor.getLong(0));
    sekolah.setNama(cursor.getString(1));
    sekolah.setAlamat(cursor.getString(2));
    sekolah.setLatitude(cursor.getLong(3));
    sekolah.setLongitude(cursor.getLong(4));
    sekolah.setTgl_berdiri(cursor.getString(5));
    sekolah.setEmail(cursor.getString(6));
    sekolah.setWebsite(cursor.getString(7));
    sekolah.setFacebook(cursor.getString(8));
    sekolah.setTwitter(cursor.getString(9));


    return sekolah;
}

public ArrayList<Sma> getPoint(String name)
{
    ArrayList<Sma> daftarPoint = new ArrayList<Sma>();

    Sma sekolah= new Sma(); //inisialisasi barang
    String query = "select COLUMN_NAMA_SMA, COLUMN_LATITUDE_SMA, COLUMN_LONGTITUDE_SMA    from sma where COLUMN_NAMA_SMA ='" + name + "'";
    Cursor c = database.rawQuery(query, null);
    c.moveToFirst();
    while (!c.isAfterLast()) 
    {
      sekolah = cursorToSma(c);
      daftarPoint.add(sekolah);
      c.moveToNext();
    }
    c.close();
    return daftarPoint;

}

这里是 SearchResultActivity 的代码,在这种情况下,我想在这个 Activity 中调用方法 getPoint,但是方法 getPoint 有一个参数,对吧?该参数正在与查询一起解析。代码如下:

package com.example.search;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import com.example.database.search.*;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SearchResultsActivity extends ListActivity {

  private DBDataSource dataSource; 
  private ArrayList<Sma> values;
  private TextView txtQuery;
 @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);

        dataSource = new DBDataSource(this);
          // buka kontroller
          dataSource.open();

        // get the action bar
        ActionBar actionBar = getActionBar();

        // Enabling Back navigation on Action Bar icon
        actionBar.setDisplayHomeAsUpEnabled(true);

        txtQuery = (TextView) findViewById(R.id.txtQuery);
        handleIntent(getIntent());

 }
 @Override
    protected void onNewIntent(Intent intent) 
 {
        setIntent(intent);
        handleIntent(intent);
 }
 /**
     * Handling intent data
     */
 private void handleIntent(Intent intent) 
 { 
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) 
        {
            String query = intent.getStringExtra(SearchManager.QUERY);

            //I WANT CALL THE METHOD getPoint right here by parsing the query
                //and I WANT TO SHOW IT IN TEXTVIEW
            /**
             * Use this query to display search results like 
             * 1. Getting the data from SQLite and showing in listview 
             * 2. Making webrequest and displaying the data 
             * For now we just display the query only
             */
            txtQuery.setText("Search Query: " + query);

        }
 }
}

谁能帮帮我?

最佳答案

这是我的代码,可能对你有帮助

db.open();
            Cursor c = db.getautobackupSettings(session.getUserId());
            if(c.getCount()>0)
            {
                c.moveToPosition(0);
                int camerabackup = c.getInt(c.getColumnIndex("your colomn name"));


                }
            db.close

及其在数据库中提供所有数据的我的 getautobackupSettings() 方法

public Cursor getautobackupSettings(int userId)
    {
        return db.rawQuery("select * from autobackupSettings where user_id="+userId, null);
    }

关于android - 如何在 TextView 中从 SQLite 中检索单个数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22343724/

相关文章:

android - Firebase isEmailVerified 无法正常工作 kotlin android

java - 删除 SQLite DB 表中的最后一个元素

SQLite:双主键和自动增量

android - ndk-build 输出 ‘error adding symbols. File in wrong format’

android - 在协调器布局中首先隐藏中间 View

android - 必须点击两次才能调用edittext android的onclick方法

database - 根据 IANA 时区数据库,是否有位置 -> 时区名称的数据库

mysql - mysql 错误(错误 1136)

android - 如果 SQLite 游标没有结果,应用程序会崩溃

android - 在 Android 中备份 SQLite 数据库