Android 使用复选框和搜索选项实现Listview
我正在尝试为listview实现一个搜索选项,它在每一行中都有一个复选框 我正在从Google plus帐户获取好友列表,并将其显示在列表视图中 使用customAdapter 朋友的姓名、图像和id存储在hashmap中 例如,这个问题Android 使用复选框和搜索选项实现Listview,android,listview,checkbox,android-listview,Android,Listview,Checkbox,Android Listview,我正在尝试为listview实现一个搜索选项,它在每一行中都有一个复选框 我正在从Google plus帐户获取好友列表,并将其显示在列表视图中 使用customAdapter 朋友的姓名、图像和id存储在hashmap中 例如,这个问题 我选择第一个和第二个项目,然后在搜索框中键入一个文本-显示结果时会自动选中第一个项目和第二个项目 或者另一个例子: 当我在搜索结果中选择一个项目,现在清除搜索文本时,复选框也会被清除 如何解决此问题 Friends.java ArrayList<Hash
我选择第一个和第二个项目,然后在搜索框中键入一个文本-显示结果时会自动选中第一个项目和第二个项目 或者另一个例子: 当我在搜索结果中选择一个项目,现在清除搜索文本时,复选框也会被清除 如何解决此问题 Friends.java
ArrayList<HashMap<String, String>> friendList;
ArrayList<HashMap<String, String>> searchResults;
static boolean[] isChecked;
static boolean[] isSearchChecked;
private EditText searchText;
static int listPerson = 1;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
setContentView(R.layout.activityfriends);
res = this.getResources();
searchText = (EditText) findViewById(R.id.searchText);
searchText.setOnClickListener(this);
friendList = new ArrayList<HashMap<String, String>>();
searchResults = new ArrayList<HashMap<String, String>>();
list = (ListView)findViewById(R.id.friend_list);
searchText.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
listofperson = 2;
searchResults.clear();
if (friendList.size() > 0 )
{
for (HashMap<String, String> map : friendList)
{
if(map.get(KEY_DISPLAY_NAME).toLowerCase().contains(s.toString().toLowerCase()))
{
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put(KEY_ID, map.get(KEY_ID));
hashMap.put(KEY_DISPLAY_NAME, map.get(KEY_DISPLAY_NAME));
hashMap.put(KEY_IMAGEPROFILE_URL, map.get(KEY_IMAGEPROFILE_URL));
searchResults.add(hashMap);
isSearchChecked = new boolean[searchResults.size()];
for (int i = 0; i < isSearchChecked.length; i++)
{
isSearchChecked[i] = false;
}
}
}
adapter= new FriendsAdapter(Friends.this, searchResults);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
});
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener()
{
private CheckBox cb;
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
if (listPerson == 1)
{
String person_ID = friendList.get(position).get(KEY_ID);
String person_DISPLAY_NAME = friendList.get(position).get(KEY_DISPLAY_NAME);
cb = (CheckBox) view.findViewById(R.id.checkBox);
//cb.setChecked(true);
cb.performClick();
if (cb.isChecked())
{
//add to be database
}
else if (!cb.isChecked())
{
//remove from database
}
}
else
{
String SearchResult_person_ID = searchResults.get(position).get(KEY_ID);
String SearchRResult_person_DISPLAY_NAME = searchResults.get(position).get(KEY_DISPLAY_NAME);
cb = (CheckBox) view.findViewById(R.id.checkBox);
//cb.setChecked(true);
cb.performClick();
if (cb.isChecked())
{
//add to database
}
else if (!cb.isChecked())
{
//remove from database
}
}
}
});
}
public class FriendsAdapter extends BaseAdapter
{
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public FriendsAdapter(Activity a, ArrayList<HashMap<String, String>> d)
{
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount()
{
return data.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout view = (LinearLayout) convertView;
if (view == null)
{
view = (LinearLayout) inflater.inflate(R.layout.friend_list_row, null);
}
HashMap<String, String> listFriends = new HashMap<String, String>();
listFriends = data.get(position);
TextView friendsname = (TextView) view.findViewById(R.id.friendsName); // title
friendsname.setText(listFriends.get(Friends.KEY_DISPLAY_NAME));
ImageView thumb_image = (ImageView)view.findViewById(R.id.list_image); // thumb image
imageLoader.DisplayImage(listFriends.get(Friends.KEY_IMAGEPROFILE_URL), thumb_image);
CheckBox cBox = (CheckBox) view.findViewById(R.id.checkBox);
cBox.setOnCheckedChangeListener(null);
cBox.setChecked(InviteFriends.isChecked[position]);
cBox.setTag(Integer.valueOf(position));
cBox.setOnCheckedChangeListener(mListener);
return view;
}
OnCheckedChangeListener mListener = new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
Friends.isChecked[(Integer)buttonView.getTag()] = isChecked;
}
};
ArrayList好友列表;
ArrayList搜索结果;
检查静态布尔[];
静态布尔[]已检查;
私人编辑文本搜索文本;
静态int-listPerson=1;
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u输入\状态\始终\隐藏);
setContentView(R.layout.activityfriends);
res=this.getResources();
searchText=(EditText)findViewById(R.id.searchText);
searchText.setOnClickListener(此);
friendList=newArrayList();
searchResults=newarraylist();
列表=(ListView)findViewById(R.id.friend\u列表);
searchText.addTextChangedListener(新的TextWatcher()
{
公共无效后文本已更改(可编辑)
{
}
更改前文本之前的公共void(字符序列s、int start、int count、int after)
{
}
public void onTextChanged(字符序列、int start、int before、int count)
{
listoperson=2;
searchResults.clear();
如果(friendList.size()>0)
{
for(HashMap映射:好友列表)
{
if(map.get(KEY\u DISPLAY\u NAME).toLowerCase()包含(s.toString().toLowerCase())
{
HashMap HashMap=新的HashMap();
hashMap.put(KEY_ID,map.get(KEY_ID));
hashMap.put(KEY\u DISPLAY\u NAME,map.get(KEY\u DISPLAY\u NAME));
put(KEY_IMAGEPROFILE_URL,map.get(KEY_IMAGEPROFILE_URL));
searchResults.add(hashMap);
isSearchChecked=新布尔值[searchResults.size()];
对于(int i=0;i
在Gplus客户端的OnResult上,我有以下代码行:
PersonBuffer personBuffer = peopleData.getPersonBuffer();
try
{
friendsCount = personBuffer.getCount();
for (int i = 0; i < friendsCount; i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_ID, personBuffer.get(i).getId());
map.put(KEY_DISPLAY_NAME, personBuffer.get(i).getDisplayName());
map.put(KEY_IMAGEPROFILE_URL, personBuffer.get(i).getImage().getUrl());
friendList.add(map);
}
}
finally
{
personBuffer.close();
}
if (friendCount > 0)
{
isChecked = new boolean[peopleCount];
for (int i = 0; i < isChecked.length; i++)
{
isChecked[i] = false;
}
adapter = new FriendsAdapter(this, friendList);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
PersonBuffer PersonBuffer=peopleData.getPersonBuffer();
尝试
{
friendsCount=personBuffer.getCount();
for(int i=0;i0)
{
isChecked=新布尔值[peopleCount];
for(int i=0;i
FriendsAdapter.java
ArrayList<HashMap<String, String>> friendList;
ArrayList<HashMap<String, String>> searchResults;
static boolean[] isChecked;
static boolean[] isSearchChecked;
private EditText searchText;
static int listPerson = 1;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
setContentView(R.layout.activityfriends);
res = this.getResources();
searchText = (EditText) findViewById(R.id.searchText);
searchText.setOnClickListener(this);
friendList = new ArrayList<HashMap<String, String>>();
searchResults = new ArrayList<HashMap<String, String>>();
list = (ListView)findViewById(R.id.friend_list);
searchText.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
listofperson = 2;
searchResults.clear();
if (friendList.size() > 0 )
{
for (HashMap<String, String> map : friendList)
{
if(map.get(KEY_DISPLAY_NAME).toLowerCase().contains(s.toString().toLowerCase()))
{
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put(KEY_ID, map.get(KEY_ID));
hashMap.put(KEY_DISPLAY_NAME, map.get(KEY_DISPLAY_NAME));
hashMap.put(KEY_IMAGEPROFILE_URL, map.get(KEY_IMAGEPROFILE_URL));
searchResults.add(hashMap);
isSearchChecked = new boolean[searchResults.size()];
for (int i = 0; i < isSearchChecked.length; i++)
{
isSearchChecked[i] = false;
}
}
}
adapter= new FriendsAdapter(Friends.this, searchResults);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
});
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener()
{
private CheckBox cb;
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
if (listPerson == 1)
{
String person_ID = friendList.get(position).get(KEY_ID);
String person_DISPLAY_NAME = friendList.get(position).get(KEY_DISPLAY_NAME);
cb = (CheckBox) view.findViewById(R.id.checkBox);
//cb.setChecked(true);
cb.performClick();
if (cb.isChecked())
{
//add to be database
}
else if (!cb.isChecked())
{
//remove from database
}
}
else
{
String SearchResult_person_ID = searchResults.get(position).get(KEY_ID);
String SearchRResult_person_DISPLAY_NAME = searchResults.get(position).get(KEY_DISPLAY_NAME);
cb = (CheckBox) view.findViewById(R.id.checkBox);
//cb.setChecked(true);
cb.performClick();
if (cb.isChecked())
{
//add to database
}
else if (!cb.isChecked())
{
//remove from database
}
}
}
});
}
public class FriendsAdapter extends BaseAdapter
{
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public FriendsAdapter(Activity a, ArrayList<HashMap<String, String>> d)
{
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount()
{
return data.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout view = (LinearLayout) convertView;
if (view == null)
{
view = (LinearLayout) inflater.inflate(R.layout.friend_list_row, null);
}
HashMap<String, String> listFriends = new HashMap<String, String>();
listFriends = data.get(position);
TextView friendsname = (TextView) view.findViewById(R.id.friendsName); // title
friendsname.setText(listFriends.get(Friends.KEY_DISPLAY_NAME));
ImageView thumb_image = (ImageView)view.findViewById(R.id.list_image); // thumb image
imageLoader.DisplayImage(listFriends.get(Friends.KEY_IMAGEPROFILE_URL), thumb_image);
CheckBox cBox = (CheckBox) view.findViewById(R.id.checkBox);
cBox.setOnCheckedChangeListener(null);
cBox.setChecked(InviteFriends.isChecked[position]);
cBox.setTag(Integer.valueOf(position));
cBox.setOnCheckedChangeListener(mListener);
return view;
}
OnCheckedChangeListener mListener = new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
Friends.isChecked[(Integer)buttonView.getTag()] = isChecked;
}
};
公共类FriendsAdapter扩展BaseAdapter
{
私人活动;
私有数组列表数据;
专用静态充气机=空;
公共图像加载器;
公众朋友助理(活动a,阵列列表d)
{
活动=a;
数据=d;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
imageLoader=新的imageLoader(activity.getApplicationContext());
}
公共整数