Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Help] Display from database in ListActivity
06-02-2013, 03:03 PM, (This post was last modified: 06-02-2013, 03:03 PM by kayman120.)
Post: #1
[Help] Display from database in ListActivity
salam...aku ada masalah nak display data from android SQLite db...bila aku nak try display, dia kuar error..

masa press button, aku grab string dekat editText class ni...untuk pass ke class ListWanitaActivity.java

Quote:WanitaActivity.java

Code:
package com.example.m_hajj;

import com.example.m_hajj.database.OpenHelper;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.w3c.dom.Text;

import com.example.m_hajj.database.DBAdapter;
import com.example.m_hajj.database.OpenHelper;
import com.example.m_hajj.database.OpenHelper.*;

import android.R.integer;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.SyncStateContract.Helpers;
import android.renderscript.Sampler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class WanitaActivity extends Activity implements OnClickListener{

    Button btnWanita;
    EditText txtWnta;
    TextView lblwanita;

    private SQLiteDatabase database;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wanitapage);
        
        txtWnta = (EditText)findViewById(R.id.txtWnta);
        btnWanita = (Button)findViewById(R.id.btnWnta);    
        lblwanita = (TextView)findViewById(R.id.lblwanita);
        String kWanita;
        OpenHelper helper = new OpenHelper(getApplicationContext());
        database = helper.getWritableDatabase();    
        
        btnWanita.setOnClickListener(this);
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        getMenuInflater().inflate(R.menu.menuhajj, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        
        if(v == btnWanita)
        {
            Intent i = new Intent(WanitaActivity.this, ListWanitaActivity.class);
            i.putExtra(OpenHelper.KEY_WANITA, txtWnta.getText().toString());
            startActivity(i);
            
        }
        
            
        
    }
    
    

}





aku nak display data2 from database dekat class ni..

Quote:LisWanitaActivity.java

Code:
package com.example.m_hajj;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.CursorJoiner.Result;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.m_hajj.database.OpenHelper;

public class ListWanitaActivity extends ListActivity{
    
    private SQLiteDatabase database;
    WanitaActivity wa = new WanitaActivity();
    ArrayList results = new ArrayList();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
    try{
        OpenHelper helper = new OpenHelper(getApplicationContext());
        database = helper.getWritableDatabase();
        
        String sql = "select * from " +
                    OpenHelper.DB_TABLE_WANITA + " where " + OpenHelper.KEY_WANITA +
                    " like " + "'%" + wa.txtWnta.getText().toString() + "%';";
        Cursor c = database.rawQuery(sql, null);
        
        if(c!= null)
        {
            c.moveToFirst();
            int soalanColumn =   c.getColumnIndex(OpenHelper.MASALAH_WANITA);
            if(c.isFirst())
            {
                int i = 0;
                do
                {
                    String soalan = c.getString(soalanColumn);
                    results.add(soalan);
                }while(c.moveToNext());
            }
            
        }
    }
        
    catch(SQLException e)
    {
            
    }
        
        this.setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, results));
        
    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        
    }
    
}

so, error dia dekat class ListWanitaActivity.java highlight dekat sini....hopefully korang bleh tolong...tq...

Quote:" like " + "'%" + wa.txtWnta.getText().toString() + "%';";
Kalau tiada angin masakan pokok bergoyang
Reply
06-02-2013, 03:19 PM, (This post was last modified: 06-02-2013, 03:19 PM by lamki.)
Post: #2
RE: [Help] Display from database in ListActivity
(06-02-2013, 03:03 PM)kayman120 Wrote:
Quote:" like " + "'%" + wa.txtWnta.getText().toString() + "%';";

maybe sbb ( ' ) tu escape character, jadi xleh ltak camtu je...

try,
Code:
" like " + "\'%" + wa.txtWnta.getText().toString() + "%\';";

Hmm
Revenge 101 :
Spoiler:

1.Pee in plate
2.Freeze the pee(in 1.)
3.Slide frozen 'pee disc' under the victim's door(room or house, any will do)
4.The pee disc will now melt.
5.???
6.Profit

Hmm
Reply
06-02-2013, 03:25 PM,
Post: #3
RE: [Help] Display from database in ListActivity
(06-02-2013, 03:19 PM)lamki Wrote:
(06-02-2013, 03:03 PM)kayman120 Wrote:
Quote:" like " + "'%" + wa.txtWnta.getText().toString() + "%';";

maybe sbb ( ' ) tu escape character, jadi xleh ltak camtu je...

try,
Code:
" like " + "\'%" + wa.txtWnta.getText().toString() + "%\';";

Hmm


still error...uhuhuhu... T_T
Kalau tiada angin masakan pokok bergoyang
Reply
06-02-2013, 04:05 PM,
Post: #4
RE: [Help] Display from database in ListActivity
Code:
"like '%" + wa.txtWnta.getText().toString() + "%'";
Sleepy
Reply
26-02-2013, 07:29 PM, (This post was last modified: 26-02-2013, 07:30 PM by phoenix.)
Post: #5
RE: [Help] Display from database in ListActivity
kalau aku, wa.txtWnta.getText().toString() aku declacre as string dulu.xdela nmpk serabut sgt.huhu
try buat ganti dengan double single qoute.
Code:
"like ' '%" + wa.txtWnta.getText().toString() + "%' ' ";
aku jarak double single qoute nk bg nmpk.
Jack of all trades, master of none. Facepalm
Reply
27-02-2013, 12:43 AM,
Post: #6
Re: [Help] Display from database in ListActivity
Like tak boleh gune dlm sqlite kot. Cube gune algoritm sql query yg lain. Klo xleh gak, ko try potong block tgk brp maximum torque.
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Netbean - langkah nak buat aplikasi bergui + database stranger12 4 744 01-09-2012, 06:40 PM
Last Post: stranger12
  extract database from mysql crashman_xx 3 791 28-05-2012, 02:14 PM
Last Post: crashman_xx

Forum Jump: