私はオートコンプリートテキストビューを開発しており、使用されているWebサービスからの検索を使用しました。
public class AddUserListActivity extends Activity{
// private SimpleSectionAdapter<String> sectionAdapter;
ListView listUser;
private List<String> lastName = new ArrayList<String>();
DtoUserDetail mApplication;
ArrayList<DtoUserList> ListArray;
private UserListAdapter objAdapter;
Button AddFriends;
DbServices Dbs = new DbServices();
Button btnBackAddUser;
Button btnAddFriends;
Button btnBackToMain;
RelativeLayout searchBar;
Button btnSearchSend;
AutoCompleteTextView editSearchText;
ArrayList<DtoSearchUser> searchUserList;
boolean searchStatus = false;
List<String> searchUserNameList;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_user_list);
listUser = (ListView)findViewById(R.id.listView);
mApplication = (DtoUserDetail)AddUserListActivity.this.getApplicationContext();
btnBackToMain = (Button)findViewById(R.id.btnBackFromAddFriends);
searchBar = (RelativeLayout)findViewById(R.id.searchbar);
btnSearchSend = (Button)findViewById(R.id.btnSearchSend);
editSearchText =(AutoCompleteTextView)findViewById(R.id.editSearchText);
// objAdapter.notifyDataSetChanged();
btnSearchSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
searchUserList = new ArrayList<DtoSearchUser>();
searchUserNameList = new ArrayList<String>();
Log.v("adduserlist", "searchtext:" + editSearchText.getText().toString());
searchUserList = new DbServices().SendSearchText(editSearchText.getText().toString(),mApplication.getUserid()+"");
for(int i=0;i<searchUserList.size();i++)
{
searchUserNameList.add(searchUserList.get(i).getUsers());
}
Log.v("log", " search list userName " + searchUserNameList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(AddUserListActivity.this, android.R.layout.simple_dropdown_item_1line, searchUserNameList);
editSearchText.setAdapter(adapter);
}
});
editSearchText.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
Log.v("log"," position in autocomplete " + pos);
String toUserId = searchUserList.get(pos).getUserid()+"";
Log.v("log"," autocomplete USERID " + searchUserList.get(pos).getUserid() + " " + searchUserList.get(pos).getLast_name());
String status= Dbs.addFriends(mApplication.getUserid()+"",toUserId);
Toast.makeText(AddUserListActivity.this, "ToUserId : " +toUserId + " status"+ status , Toast.LENGTH_LONG).show();
}
});
btnAddFriends=(Button)findViewById(R.id.btnAddFriends);
btnAddFriends.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(searchStatus==false)
{
searchBar.setVisibility(View.VISIBLE);
searchStatus = true;
}
else
{
searchBar.setVisibility(View.GONE);
searchStatus = false;
}
}
});
btnBackToMain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AddUserListActivity.this.finish();
}
});
if (isNetworkAvailable()) {
new MyTask().execute();
} else {
showToast("No Netwrok Connection!!!");
//this.finish();
}
}
class MyTask extends AsyncTask<Void, Void, Void> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(AddUserListActivity.this);
pDialog.setMessage("Loading...");
pDialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// ListArray = new DbServices().GetUserList(""+mApplication.getUserid());
ListArray = new DbServices().GetFriendsList(""+mApplication.getUserid());
return null;
}
@Override
protected void onPostExecute(Void result) {
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
if (null == ListArray || ListArray.size() == 0) {
showToast("No data found from web!!!");
// AddUserListActivity.this.finish();
} else {
setAdapterToListview();
}
super.onPostExecute(result);
}
}
// setAdapter
public void setAdapterToListview() {
for(int i=0;i<ListArray.size();i++)
{
lastName.add(ListArray.get(i).getLast_name());
}
for(int j=0;j<SortingArraylist(ListArray).size();j++)
{
}
objAdapter = new UserListAdapter(AddUserListActivity.this,SortingArraylist(ListArray));
listUser.setAdapter(objAdapter);
listUser.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1,
int position, long id) {
Log.v("log"," position of item " + position + " item first name " + ListArray.get(position).getFirst_name());
Intent i_options = new Intent(AddUserListActivity.this,friendsHoldActivity.class);
i_options.putExtra("pos", ListArray.get(position).getUserid());
//startActivity(i_options);
startActivity(i_options);
}
});
}
class ListSectionizer implements Sectionizer<String> {
@Override
public String getSectionTitleForItem(String itemName) {
return itemName.toUpperCase().substring(0, 1);
}
}
public static Comparator<String> StringComparator = new Comparator<String>() {
public int compare(String app1, String app2) {
String stringName1 = app1;
String stringName2 = app2;
return stringName1.compareToIgnoreCase(stringName2);
}
};
private List<String> Sorting(List<String> Names) {
Collections.sort(Names, StringComparator);
return Names;
}
public static Comparator<DtoUserList> StringArrayComparator = new Comparator<DtoUserList>() {
public int compare(DtoUserList app1, DtoUserList app2) {
DtoUserList stringName1 = app1;
DtoUserList stringName2 = app2;
return stringName1.getLast_name().compareToIgnoreCase(stringName2.getLast_name());
}
};
private ArrayList<DtoUserList> SortingArraylist(ArrayList<DtoUserList> userDetail){
Collections.sort(userDetail ,StringArrayComparator);
return userDetail;
}
// check internet connection
public boolean isNetworkAvailable() {
ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
// Toast is here...
private void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}