【发布时间】:2014-08-20 02:38:36
【问题描述】:
我正在尝试为 listview 实现一个搜索选项,该选项在每一行中都有一个复选框。
我正在从 Google plus 帐户获取朋友列表并将其显示在列表视图中 使用自定义适配器。
朋友的名字、图片和id都存储在hashmap中。
例如问题
我选择第一项和第二项,然后在搜索框中键入文本 - 结果显示时会自动检查第一项和第二项。
或者另一个例子:
当我在搜索结果中选择一个项目并现在清除搜索文本时,复选框也会被清除。
我该如何解决这个问题?
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
}
}
}
});
}
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();
}
FriendsAdapter.java
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;
}
};
【问题讨论】:
-
“我选择了第一个和第二个项目,然后在搜索框中输入一个文本 - 显示结果时会自动检查第一个项目和第二个项目。”我不明白这个
-
@NavinRajPandey:例如,我有显示的项目列表。如果我选择第一项和第二项,然后搜索其他内容。显示结果后,我可以看到默认单击的第一个和第二个项目。
-
@TheDevMan 你解决了那个问题吗?我遇到同样的问题?该怎么做?
标签: android listview checkbox android-listview