Android integrate searchview with localhost and implement in BaseAdapter



i'm trying to implement SearchView functionality in ActionBar using the documentation. I want to show matchable users image and name in ListView from localhost with BaseAdapter and Volley to show image. UserProfileTab class extends FragmentActivity where i put menu code



@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.user_menu, menu);

SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));

return true;

}


XML for menu



<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://ift.tt/nIICcg" >

<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />


In my Manifest i used following code to bind FriendSearchResultsActivity



<activity
android:name="com.wpeden.social.circle.search.FriendSearchResultsActivity"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|screenSize" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>

<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>


I added Searchable.xml in xml folder



<?xml version="1.0" encoding="utf-8"?><searchable xmlns:android="http://ift.tt/nIICcg"
android:hint="@string/search_hint"
android:label="@string/app_name" />


My search result activity FriendSearchResultsActivity is



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wpedenyo_friend_search_result);


txtQuery = (ListView) findViewById(R.id.wpeden_yo_searchresults);

adapter = new SearchfriendAdapter(this, friendList);
listView.setAdapter(adapter);

handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}

/**
* Handling intent data
*/
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
String url = null;

url = "http://ipaddress/wordpress_question_answer?type=find_all_friends&user_id=";
url = url.concat(PrefUserName);
url = url.concat("&search_data=");
url = url.concat(query);
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());

// Parsing json
for (int i = 0; i < response.length(); i++) {
try {

JSONObject obj = response.getJSONObject(i);
SearchFriend friend = new SearchFriend();
friend.setTitle(obj.getString("name"));
friend.setThumbnailUrl(obj
.getString("image"));

// adding movie to movies array
friendList.add(friend);

} catch (JSONException e) {
e.printStackTrace();
}

}

// notifying list adapter about data changes
// so that it renders the list view with updated
// data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());

}
});

// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);

}

}


And FriendListAdapter is



public class FriendListAdapter extends BaseAdapter {

private Activity activity;
private LayoutInflater inflater;
private List<Friend> friendList;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public FriendListAdapter(Activity activity, List<Friend> movieItems) {
this.activity = activity;
this.friendList = movieItems;
}

@Override
public int getCount() {
return friendList.size();
}

@Override
public Object getItem(int location) {
return friendList.get(location);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);

if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.wpedenyo_friend_image);
TextView name = (TextView) convertView.findViewById(R.id.wpedenyo_friend_name);

Friend m = friendList.get(position);

thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

name.setText(m.getName());
return convertView;
}


}


Server Code



if ((isset($_POST['type']) && $_POST['type'] == 'find_all_friends') && isset($_POST['user_id']) && isset($_POST['search_data'])) {
$user_name = $_POST['user_id'];
$search_data = $_POST['search_data'];
$data = $this->get_all_friends($user_name, $search_data);
if (is_array($data) && !empty($data)) {
echo json_encode($data);
die();
} else {
$error[]['message'] = 'No friends found';
echo json_encode($error);
die();
}
}
function get_all_friends($user_name, $matched_friend) {
global $wpdb;
$table_name = $wpdb->prefix . 'usertable';
$friends = $wpdb->get_results("SELECT id FROM $table_name WHERE id !='$user_name' AND id LIKE $matched_friend");
$friends_list = array();
if (!empty($friends)) {
foreach ($friends as $key => $friend) {
if ($friend->id != $user_name) {
$friends_list[]['name'] = $friend->id;
$friends_list[]['image'] = $friend->image;
$verify_friend = $this->get_my_friend($user_name, $friend->id);
$friends_list[]['verify'] = $verify_friend;
}
}
return $friends_list;
} else {
return 'fail';
}
}


But i get no response in search result even Logcat shows no error. Thanks in advance.


No comments:

Post a Comment