i have an activity that list clients on a listview from a sql server database, and i've had implement the searchview.. but i dont know how to implement the query to search the client names from the listview.. can someone help me??
here is my the code from the activity where is the list:
package com.example.hp13_b200.testedesign;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class listar_clientes extends ActionBarActivity {
private ListView list;
private TextView nome, abv, esaldo, telefone, mail, morada;
private SearchView search;
private ProgressDialog pDialog;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_listar_clientes, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
// Associate searchable configuration with the SearchView
}
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "http://ciemarco.dyndns.org:90/androidapp/listar_clientes.php";
// JSON Node names
private static final String TAG_CLIENTE= "clientes";
private static final String TAG_NOME = "nome";
private static final String TAG_ABV = "nome2";
private static final String TAG_SALDO = "esaldo";
private static final String TAG_TELEFONE = "telefone";
private static final String TAG_MORADA = "morada";
private static final String TAG_MAIL = "email";
// products JSONArray
JSONArray products = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listar_clientes);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
/*if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
}*/
nome = (TextView)findViewById(R.id.name);
abv = (TextView)findViewById(R.id.abv);
telefone = (TextView)findViewById(R.id.telefone);
morada = (TextView)findViewById(R.id.morada);
mail = (TextView)findViewById(R.id.mail);
esaldo = (TextView)findViewById(R.id.esaldo);
list = (ListView)findViewById(android.R.id.list);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Log.v("COUNT-->",productsList.get(0).toString());
// Get listview
/* list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// getting values from selected ListItem
String nome = ((TextView) view.findViewById(R.id.name)).getText()
.toString();
Log.d("Cod_conta: ", ""+nome);
// Starting new intent
Intent in = new Intent(listar_clientes.this,
detalhes_cliente.class);
// sending pid to next activity
in.putExtra(TAG_NOME, nome);
//in.putExtra(TAG_ABV, abv);
Log.d("entrou: ", "vai entrar");
//startActivityForResult(in,100);
// starting new activity and expecting some response back
startActivity(in);
}
} );*/
// launching Edit Product Screen
// lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
//
// // Starting new intent
// Intent in = new Intent(getApplicationContext(),
// list_item.class);
// // sending pid to next activity
// in.putExtra(TAG_PID, pid);
//
// // starting new activity and expecting some response back
// startActivityForResult(in, 100);
// }
// });
/* ListAdapter adapter = new SimpleAdapter(
encomendas_user.this, productsList,
R.layout.activity_list_item, new String[] { TAG_IDUSER,
TAG_TITULO},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);*/
// on seleting single product
// launching Edit Product Screen
}
// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(listar_clientes.this);
pDialog.setMessage("A Carregar dados. Por favor Aguarde...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
// int success = json.getInt(TAG_IDUSER);
//Log.d("NAMSUCCESSE: ", ""+success);
//if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_CLIENTE);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_NOME);
String abv = c.getString(TAG_ABV);
String telefone = c.getString(TAG_TELEFONE);
String morada = c.getString(TAG_MORADA);
String mail = c.getString(TAG_MAIL);
String esaldo = c.getString(TAG_SALDO);
Log.d("NAME TESTE: ", name);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_NOME, name);
map.put(TAG_ABV, abv);
map.put(TAG_TELEFONE, telefone);
map.put(TAG_MORADA, morada);
map.put(TAG_MAIL, mail);
map.put(TAG_SALDO, esaldo);
// if(UserInfo.userID==(Integer.parseInt(name))){
// adding HashList to ArrayList
productsList.add(map);
// }
}
/* } else {
// no products found
// Launch Add New product Activity
//Intent i = new Intent(getApplicationContext(),
// NewProductActivity.class);
// Closing all previous activities
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// startActivity(i);
}*/
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
listar_clientes.this, productsList,
R.layout.activity_list_item_clientes, new String[]{TAG_NOME, TAG_MAIL, TAG_TELEFONE, TAG_MORADA, TAG_SALDO},
new int[]{R.id.name, R.id.mail, R.id.telefone, R.id.morada, R.id.esaldo});
// updating listview
list.setAdapter(adapter);
}
});
}
}
public void listar(View v){
LinearLayout vwParentRow = (LinearLayout)v.getParent();
TextView nome=(TextView)vwParentRow.getChildAt(1);
TextView telefone=(TextView)vwParentRow.getChildAt(4);
TextView morada=(TextView)vwParentRow.getChildAt(3);
TextView mail=(TextView)vwParentRow.getChildAt(5);
TextView esaldo=(TextView)vwParentRow.getChildAt(6);
// Starting new intent
Intent in = new Intent(getApplicationContext(),
detalhes_cliente.class);
// sending pid to next activity
in.putExtra(TAG_NOME, nome.getText().toString());
in.putExtra(TAG_TELEFONE, telefone.getText().toString());
in.putExtra(TAG_MORADA, morada.getText().toString());
in.putExtra(TAG_MAIL, mail.getText().toString());
in.putExtra(TAG_SALDO, esaldo.getText().toString());
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.id.action_settings);
item.setVisible(false);
super.onPrepareOptionsMenu(menu);
return true;
}
}
and here is the code from the searchresultactivity:
package com.example.hp13_b200.testedesign;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class SearchResultsActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_search_results, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}