2

At this time I need help from u guys, I have a question how to get a single data from database into TextView?

Here is the code from 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;

}

And here the code from SearchResultActivity, In this case, I want to call method getPoint in this Activity, but the method getPoint has an parameter, right? The parameter is parsing with the query. The code is below:

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

        }
 }
}

Can anyone help me?

mmBs
  • 8,421
  • 6
  • 38
  • 46
ndr_sd
  • 115
  • 1
  • 14

2 Answers2

0

this is my code may be it will help u

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

and its my getautobackupSettings() methos in database which give all data

public Cursor getautobackupSettings(int userId)
    {
        return db.rawQuery("select * from autobackupSettings where user_id="+userId, null);
    }
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
0

Since you say you want it displayed in a ListView and TextView, I suppose you mean: display data in a TextView in a ListView. But you also say "display a single data from database". Which is it? Display list or just one text item?

For displaying a list of items you will have to use an Adapter for displaying your data.

I've just created a rudimentary logic for you. You will have to read up on ListActivities here: http://developer.android.com/reference/android/app/ListActivity.html and also any converting to an ArrayList if you want this solution, since this particular ArrayListAdapter takes just that.

If you want a more advanced Adapter, you can find info here: Custom Adapter for List View

The code has a lot of comments since the requirements are a bit unclear.

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

}

Change your handleIntent(Intent intent) to

 private String handleIntent(Intent intent){

  ...
  return name;
 }
Community
  • 1
  • 1
VonSchnauzer
  • 912
  • 11
  • 17