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.
this is from my DBmanager:
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.
here is my sample code. i got no problem in getting all rows from database.
PHP:
public class Namelist extends Activity {
ListView listView;
DbManager db;
ImageView btnImageSearch;
String SName;
EditText name;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.namelist);
db = new DbManager(this);
listView = (ListView)findViewById(R.id.listView1);
btnImageSearch = (ImageView)findViewById(R.id.imgSearch);
name = (EditText)findViewById(R.id.editText1);
displaySearch();
btnImageSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SName = name.getText().toString();
displaySearch();
}
});
}
private void displaySearch() {
// TODO Auto-generated method stub
ArrayList<ArrayList<Object>> data = db.getAllRowsAsArrays();
final ArrayList<String> items = new ArrayList<String>();
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
for (int position = 0; position < data.size(); position++) {
ArrayList<Object> row = data.get(position);
items.add("Id : " + row.get(0).toString() + "\nFirst Name : "
+ row.get(1).toString() + "\nLast Name : "
+ row.get(2).toString());
aa.notifyDataSetChanged();
}
listView.setAdapter(aa);
}
this is from my DBmanager:
PHP:
private SQLiteDatabase db;
private final String DBNAME = "studentsdb";
private final int DBVER = 1;
private final String TBLNAME = "students";
final static String SID = "id";
final static String SNAME = "name";
final static String SCOURSE = "course";
public ArrayList<ArrayList<Object>> getAllRowsAsArrays()
{
ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
Cursor cursor;
try
{
cursor = db.query(
TBLNAME,
new String[]{SID, SNAME, SCOURSE},
null, null, null, null, null
);
cursor.moveToFirst();
if (!cursor.isAfterLast())
{
do
{
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cursor.getLong(0));
dataList.add(cursor.getString(1));
dataList.add(cursor.getString(2));
dataArrays.add(dataList);
}
while (cursor.moveToNext());
}
}
catch (SQLException e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
return dataArrays;
}
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.