TBD.my

Full Version: [Help] Display from database in ListActivity
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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() + "%';";
(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
(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
Code:
"like '%" + wa.txtWnta.getText().toString() + "%'";
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.
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.