如何从TextView中的SQLite中检索单个数据?

时间:2014-03-12 06:33:40

标签: 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的代码,在这种情况下,我想在此getPoint中调用方法Activity,但方法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);

        }
 }
}

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

这是我的代码可能会帮助你

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);
    }

答案 1 :(得分:0)

因为你说它希望它显示在ListView和TextView中,我想你的意思是:在ListView中的TextView中显示数据。但是你也说&#34; 显示来自数据库的单个数据&#34;。这是什么?显示列表还是只显示一个文本项?

要显示项目列表,您必须使用适配器来显示数据。

我刚刚为你创造了一个基本的逻辑。你必须在这里阅读ListActivities:http://developer.android.com/reference/android/app/ListActivity.html以及任何转换为​​ArrayList,如果你想要这个解决方案,因为这个特定的ArrayListAdapter就是这样。

如果您需要更高级的适配器,可以在此处找到信息:Custom Adapter for List View

代码有很多评论,因为要求有点不清楚。

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_result);
    txtQuery = (TextView) findViewById(R.id.txtQuery);
    dataSource = new DBDataSource(this);
    // buka kontroller
    dataSource.open();
    // get the action bar
    ActionBar actionBar = getActionBar();
    DBDataSource db = new DBDataSource(this);
    //Get that "name" from intent
    String qparam = handleIntent(getIntent());
    ArrayList<Sma> list = db.getPoint(qparam);
    ArrayList<String> convertedList = convert(list); //a method you write, or you simply return an ArrayList<String> from your db.getPoint(String qparam);

    //ArrayAdapter - however, you can use most any adapter if you need more 
    //advanced data. if all you have is a list of strings, this is fine. 

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
             this, 
             android.R.layout.<your text view layout for row>,
             list );

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

    //handleIntent(getIntent());

}

将handleIntent(Intent intent)更改为

 private String handleIntent(Intent intent){

  ...
  return name;
 }