I am new to android and want to parse json data in a listview and then add search functionally to it, but I don't know how to add search.
The Java class is:
public class AndroidJSONParsingActivity extends ListActivity {
// url to make request
private static String url = "http://10.0.2.2/quick/punk.php";
// JSON Node names
private static final String TAG_CUSTOMER = "Customer";
private static final String TAG_CUSTOMER_CODE = "customer_code";
private static final String TAG_CUSTOMER_NAME = "customer_name";
private static final String TAG_CUSTOMER_MOBILE = "customer_mobile";
private static final String TAG_CUSTOMER_ADDRESS = "customer_address";
// contacts JSONArray
JSONArray Customer = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
Customer = json.getJSONArray(TAG_CUSTOMER);
// looping through All Contacts
for(int i = 0; i < Customer.length(); i++){
JSONObject c = Customer.getJSONObject(i);
// Storing each json item in variable
String customer_code = c.getString(TAG_CUSTOMER_CODE);
String customer_name = c.getString(TAG_CUSTOMER_NAME);
String customer_mobile = c.getString(TAG_CUSTOMER_MOBILE);
String customer_address = c.getString(TAG_CUSTOMER_ADDRESS);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CUSTOMER_CODE, customer_code);
map.put(TAG_CUSTOMER_NAME, customer_name);
map.put(TAG_CUSTOMER_MOBILE, customer_mobile);
map.put(TAG_CUSTOMER_ADDRESS, customer_address);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_CUSTOMER_CODE, TAG_CUSTOMER_NAME,TAG_CUSTOMER_MOBILE,TAG_CUSTOMER_ADDRESS}, new int[] {
R.id.code, R.id.name,R.id.mobile,R.id.address});
setListAdapter(adapter);
}
}
The xml file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<Button
android:id="@+id/button1"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="+" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
What I want is that when a user types something in the textbox, it should be searched in the data that is there in the list. Can any one tell me how to implement it?
Use a Custom List adapter to display items in listview. Display a editText on the top. For custom Listview http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/.
For searching listview. How can I filter ListView data when typing on EditText in android.
For custom search implement search on a custom listview. I suggest you to go with search on custom listview with filters.
Edit
activity_main.xml
MainActivity Class
list.xml to be inflated in custom list adapter
NewData Class- Holds all data in hashmap
Modify so that you add your json data in the for loop. I have made search criteria here customer id. You can change this according to your needs. This code is tested and it works. Hope someone can give a better code. Hope this helps you.