1. Check out our companion app, Forums for Android! Download from Google Play

searching SQLlite DB and list view

Discussion in 'Application Development' started by elbren, Jul 8, 2013.

  1. elbren

    elbren New Member
    Thread Starter
    5

    Jul 2, 2013
    3
    0
    5
    anyone can help on searching from database on a listview.
    here is my sample code. i got no problem in getting all rows from database.

    PHP:
    1. public class Namelist extends Activity {
    2.     ListView listView;
    3.     DbManager db;
    4.     ImageView btnImageSearch;
    5.     String SName;
    6.     EditText name;
    7.     protected void onCreate(Bundle savedInstanceState) {
    8.         super.onCreate(savedInstanceState);
    9.         setContentView(R.layout.namelist);
    10.        
    11.         db = new DbManager(this);
    12.         listView = (ListView)findViewById(R.id.listView1);
    13.         btnImageSearch = (ImageView)findViewById(R.id.imgSearch);
    14.         name = (EditText)findViewById(R.id.editText1);
    15.         displaySearch();
    16.        
    17.         btnImageSearch.setOnClickListener(new OnClickListener() {
    18.            
    19.             @Override
    20.             public void onClick(View v) {
    21.                 SName = name.getText().toString();
    22.                
    23.                 displaySearch();
    24.                
    25.             }
    26.         });
    27.     }
    28.  
    29.        private void displaySearch() {
    30.             // TODO Auto-generated method stub
    31.             ArrayList<ArrayList<Object>> data = db.getAllRowsAsArrays();
    32.             final ArrayList<String> items = new ArrayList<String>();
    33.             final ArrayAdapter<String> aa;
    34.             aa = new ArrayAdapter<String>(this,
    35.             android.R.layout.simple_list_item_1, items);
    36.             for (int position = 0; position < data.size(); position++) {
    37.             ArrayList<Object> row = data.get(position);
    38.             items.add("Id : " + row.get(0).toString() + "\nFirst Name : "
    39.             + row.get(1).toString() + "\nLast Name : "
    40.             + row.get(2).toString());
    41.             aa.notifyDataSetChanged(); 
    42.             }
    43.             listView.setAdapter(aa);
    44.             }
    this is from my DBmanager:

    PHP:
    1.  
    2.         private SQLiteDatabase db;
    3.         private final String DBNAME = "studentsdb";
    4.         private final int DBVER = 1;
    5.         private final String TBLNAME = "students";
    6.         final static String SID = "id";
    7.         final static String SNAME = "name";
    8.         final static String SCOURSE = "course";
    9.  
    10.         public ArrayList<ArrayList<Object>> getAllRowsAsArrays()
    11.        
    12.         {
    13.             ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
    14.             Cursor cursor;
    15.             try
    16.             {
    17.                 cursor = db.query(
    18.                 TBLNAME,
    19.                 new String[]{SID, SNAME, SCOURSE},
    20.                 null, null, null, null, null
    21.                 );
    22.                 cursor.moveToFirst();
    23.             if (!cursor.isAfterLast())
    24.             {
    25.             do
    26.                 {
    27.                     ArrayList<Object> dataList = new ArrayList<Object>();
    28.                     dataList.add(cursor.getLong(0));
    29.                     dataList.add(cursor.getString(1));
    30.            
    31.                     dataList.add(cursor.getString(2));
    32.                     dataArrays.add(dataList);
    33.                 }
    34.             while (cursor.moveToNext());
    35.             }
    36.             }
    37.             catch (SQLException e)
    38.             {
    39.             Log.e("DB Error", e.toString());
    40.             e.printStackTrace();
    41.             }
    42.             return dataArrays;
    43.         }
    44.  
    That's all on how to get the content of my database. my question is "HOW CAN I GET THE SELECTED RECORD IN A DATABASE SHOWING IN LISTVIEW USING LIKE STATEMENT SEARCHING FROM FIRSTNAME OR IN A LASTNAME"

    please help.
     

    Advertisement

  2. elbren

    elbren New Member
    Thread Starter
    5

    Jul 2, 2013
    3
    0
    5
    ahh its almost a week. no reply to the admin or any of the master of android. thanks to this site. try to find another forum.
     
  3. redbulletnl

    redbulletnl Member
    36

    Dec 27, 2011
    19
    1
    36

Share This Page

Loading...