使用Checkbox和Search选项实现Listview

时间:2014-08-20 02:38:36

标签: android listview checkbox android-listview

我正在尝试为listview实现一个搜索选项,每个行中都有一个复选框。

我从Google plus帐户中获取好友列表,并在列表视图中显示这些列表 使用customAdapter。

朋友的名字,图片和身份证存储在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;                    
    }
};

1 个答案:

答案 0 :(得分:0)

  

或另一个例子:

     

当我在搜索结果中选择一个项目时,现在清除搜索文本   复选框也被清除。

您可以在这里找到Android ListView Checkbox Example

对于搜索选项OnQueryTextListenerClick here功能搜索。

希望它能帮助......