这是我的代码
public class MainActivity extends Activity {
// List view
private AutoCompleteTextView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// ArrayList for Listview
ArrayList<User> userList = new ArrayList<User>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Listview Data
/* String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
"iPhone 4S", "Samsung Galaxy Note 800",
"Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};*/
User p = null;
p = new User("1", "Starhub Pte Ltd","Manager","Jason", "Bourne","");
userList.add(p);
p = new User("2", "Singtel Pte Ltd", "Senior Manager","Ben", "Chia","");
userList.add(p);
p = new User("3", "Ngee Ann Polytechnic", "Senior lecturer","Charles", "Xavier","");
userList.add(p);
p = new User("4", "Dreamcity Pte Ltd", "","Ben", "Chia","");
userList.add(p);
p = new User("5", "DBS Corporation", "Secretary","Michelle", "Chong","");
userList.add(p);
p = new User("6", "OCBC bank", "Deputy finance Manager","YongZheng", "Wong","");
userList.add(p);
AutoCompleteTextView lv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView); // NOTE: change to your autocomplete textview id
// Adding items to listview
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, getUsernames(userList));
lv.setAdapter(adapter);
}
private ArrayList<String> getUsernames(ArrayList<User> users)
{
ArrayList<String> usernames = new ArrayList<String>();
for(User u : users)
{
usernames.add(u.getFirstName() +" "+ u.getLastName() +" "+ u.getCompanyName());
}
return usernames;
}
}
无论如何只返回名字和姓氏,但隐藏公司名称?请指教。目前我可以返回所有内容,或者只能通过名字和姓氏搜索.................................. ............
答案 0 :(得分:0)
我还没有完全理解你的问题,但你的意思是通过公司标签搜索用户?如果是这种情况,请参阅以下方法:
private ArrayList<String> getUsernames(ArrayList<User> users, String company_name)
{
ArrayList<String> usernames = new ArrayList<String>();
for(User u : users)
{
if (u.getCompanyName().equals(company_name))
usernames.add(u.getFirstName() +" "+ u.getLastName());
}
return usernames;
}
我更改了方法参数。 “getUsernames”方法现在采用“company_name”变量,该变量进入foreach循环并找到与该公司对应的那些用户。希望它有所帮助。