这是List的实现,其中列出了所有联系人。 我想知道如何在此实现搜索功能。
1] MainActivity [CustomerList.java]
public class CustomerList extends Activity {
private String TAG = CustomerList.class.getSimpleName();
private ProgressDialog progressDialog;
EditText search_item;
private ListView listView;
// ListViewAdapter listViewAdapter;
ArrayList<HashMap<String, String>> getAllCustomers;
static String CUSTOMER_ID= "customer_id";
static String CUSTOMER_CONTACT_NUMBER="customer_contact_number";
static String CUSTOMER_NAME="customer_name";
static String ZONE="zone";
static String CUSTOMER_EMAIL="customer_email";
private static String url = "http://103.229.245.235/cb.selectnetworks.in/getAllCustomers";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
getAllCustomers = new ArrayList<>();
listView = (ListView) findViewById(R.id.ListViewCustomerList);
search_item=(EditText)findViewById(R.id.EditTextSearch);
new GetCustomerData().execute();
}
public class GetCustomerData extends AsyncTask<URL, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog=new ProgressDialog(CustomerList.this);
progressDialog.setMessage("please wait");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(URL... urls) {
getAllCustomers= new ArrayList<HashMap<String,String>>();
HttpHandler httpHandler= new HttpHandler();
String jsonStr=httpHandler.makeServiceCall(url);
Log.e(TAG,"Response from url: " +jsonStr);
if(jsonStr !=null){
try {
JSONObject jsonObject= new JSONObject(jsonStr);
JSONArray arr= jsonObject.getJSONArray("data");
for(int i=0;i<arr.length();i++){
JSONObject obj = arr.optJSONObject(i);// (JSONObject) arr.get(i);
String customer_id = obj.getString("customer_id");
String customer_name = obj.getString("customer_name");
String customer_contact_number = obj.getString("customer_contact_number");
String zone = obj.getString("zone_id");
String customer_email = obj.getString("customer_email");
// Toast.makeText(CustomerList.this,customer_id,Toast.LENGTH_LONG).show();
HashMap<String,String> customData = new HashMap<String, String>();
customData.put("customer_id",customer_id);
customData.put("customer_name",customer_name);
customData.put("customer_contact_number",customer_contact_number);
customData.put("zone",zone);
customData.put("customer_email",customer_email);
getAllCustomers.add(customData);
}
} catch (final JSONException e) {
Log.e(TAG,"json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"json error : " + e.getMessage(),Toast.LENGTH_LONG).show();
Log.e(TAG,"json data :" + e.getMessage());
}
});
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(progressDialog.isShowing())
progressDialog.dismiss();
listView = (ListView)findViewById(R.id.ListViewCustomerList);
// Toast.makeText(CustomerList.this,getAllCustomers.toString(),Toast.LENGTH_LONG).show();
final ListAdapter listAdapter = new SimpleAdapter(
CustomerList.this,getAllCustomers,R.layout.activity_listviewitem,new String[]{"customer_id","customer_name",
"customer_contact_number", "zone","customer_email"}, new int[]
{R.id.TextViewcustomer_id,R.id.TextVIewcustomer_name,R.id.TextViewcustomer_mobile,R.id.TextViewcustomer_zone,R.id.TextViewcustomer_email});
listView.setAdapter(listAdapter);
/* listViewAdapter = new ListViewAdapter(CustomerList.this,getAllCustomers,R.layout.activity_listviewitem,new String[]
{"customer_id", "customer_name","customer_contact_number","zone"},
new int[] {R.id.TextViewcustomer_id,R.id.TextVIewcustomer_name,
R.id.TextViewcustomer_mobile,R.id.TextViewcustomer_zone});
listView.setAdapter(listViewAdapter); */
// Search Functionality
search_item.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String text= search_item.getText().toString().toLowerCase(Locale.getDefault());
}
});
}
}
}
2] HttpHandler类
public class HttpHandler {
public static final String TAG= HttpHandler.class.getSimpleName();
public HttpHandler(){
}
public String makeServiceCall(String reqUrl){
String response=null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
3] Activity_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:hint="Search Items"
android:padding="10dp"
android:background="@drawable/edittextstyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/EditTextSearch"
/>
<ListView
android:id="@+id/ListViewCustomerList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
4] Listviewitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Customer Id"
android:id="@+id/TextViewcustomer_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="@color/colorPrimaryDark"
android:textSize="14sp"
/>/ android:textStyle="bold" />
<TextView
android:textStyle="bold"
android:text="Customer Name"
android:id="@+id/TextVIewcustomer_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
/>// android:textColor="@color/colorAccent" />
<TextView
android:text="Customer Mno"
android:id="@+id/TextViewcustomer_mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
<TextView
android:text="Customer Zone"
android:id="@+id/TextViewcustomer_zone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
<TextView
android:text="Customer Email"
android:id="@+id/TextViewcustomer_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
</LinearLayout>
答案 0 :(得分:0)
尝试将textchange侦听器添加到搜索过滤器文本域
search_item.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
选中此示例以获取完整示例:search in listview