在Custom ListView中搜索和删除行

时间:2014-06-19 19:10:17

标签: java android xml listview adapter

我正在尝试为好友列表创建ListView。它有一个搜索功能,用户可以在其中搜索特定的freind,然后将其删除为朋友,给他们发消息等等。

但是,我在删除它们时遇到了麻烦。我不认为我理解定位,或者找出用户freind在列表中的正确位置。

我想确保在所有情况下都将用户从正确的位置移除。例如,如果用户使用搜索功能并且仅返回一个用户。然后我不希望用户在位置0(一个用户)被移除,我希望它在正确的位置被移除,以便当用户返回到完整列表时。不会意外删除列表中的位置0。

有人可以查看代码吗?并且显示我在哪里出错了?

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        res = getResources();
        searchField = (EditText) findViewById(R.id.EditText01);
        lv = (ListView) findViewById(android.R.id.list);
        //button = (Button)findViewById(R.id.btnFriendList);
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        //button.setFocusable(false);
        list = new ArrayList<Friend>();

        nameBlock = res.getStringArray(R.array.names);
        descBlock = res.getStringArray(R.array.descriptions);

        names = new ArrayList<String>();
        for(int i = 0; i < nameBlock.length; i++) {
            names.add((String)nameBlock[i]);
        }
        descr = new ArrayList<String>();
        for(int i = 0; i < descBlock.length; i++) {
            descr.add((String)descBlock[i]);
        }
        images = new ArrayList<Integer>();
        for(int i = 0; i < imageBlock.length; i++) {
            images.add((Integer)imageBlock[i]);
        }
        //imageBlock = res.getIntArray(R.array.images);

        int size = nameBlock.length;
        for(int i = 0 ; i < size; i++) {
            Log.d("FREINDADD", "Freind Added" + i);
            list.add(new Friend(i, names.get(i), descr.get(i), images.get(i)));
            //friendList2.add(new Friend(i, names.get(i), descr.get(i), images.get(i)));
        }
        Log.i("Application", "Application started succesfully...");

        adapter = new CustomAdapter(this);


        setListAdapter(adapter);
        Log.i("VIRTU", "Count" + adapter.getCount());
        //adapter.getCount();


        searchField.addTextChangedListener(new TextWatcher()
        {
            @Override public void afterTextChanged(Editable s) {}
            @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                list.clear();
                textlength = searchField.getText().length();

                for (int i = 0; i < names.size(); i++)
                {
                    if (textlength <= names.get(i).length())
                    {
                        if(names.get(i).toLowerCase().contains(searchField.getText().toString().toLowerCase().trim())) {
                            Log.i("VirtuFriendList", "List recyling in process... ");
                            list.add(new Friend(i, names.get(i), descr.get(i), images.get(i)));

                        }
                      }
                }
                AppendList(list);

                }
        });


    }
    public void AppendList(ArrayList<Friend> list) {
        setListAdapter(new CustomAdapter(this));
    } 

    class CustomAdapter extends BaseAdapter {
        private Context context;
        public CustomAdapter(Context context) {
            this.context = context;

        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return list.size();
        }
        class ViewHolder {
            TextView userName;
            TextView userDesc;
            ImageView userImage;
            Button userButton;


            ViewHolder(View view) {
                userImage = (ImageView)view.findViewById(R.id.imageview);
                userName = (TextView)view.findViewById(R.id.title);
                userDesc = (TextView)view.findViewById(R.id.mutualTitle);
                userButton = (Button)view.findViewById(R.id.btn);
            }
        }
        ViewHolder holder;
        View row;
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                row = convertView;

                if(row == null) 
                {
                    // If it is visible to the user, deploy the row(s) - allocated in local memory
                    LayoutInflater inflater = (LayoutInflater)context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    row = inflater.inflate(R.layout.search_list_item, parent, false);
                    holder = new ViewHolder(row);
                    row.setTag(holder);
                    Log.d("VIRTU", "Row deployed...");
                }
                else 
                {
                    // Recycle the row if it is not visible to to the user - store in local memory
                    holder = (ViewHolder)row.getTag();
                    Log.d("VIRTU", "Row recycled...");
                }
                Friend temp = list.get(position);

                // Set the resources for each component in the list
                holder.userImage.setImageResource(temp.getImage());
                holder.userName.setText(temp.getName());
                holder.userDesc.setText(temp.getDesc());

                ((Button)row.findViewById(R.id.btn)).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        PopupMenu pop = new PopupMenu(getApplicationContext(), v);
                        MenuInflater inflater = pop.getMenuInflater();

                        inflater.inflate(R.menu.firned_popup_action,pop.getMenu());
                        pop.show();
                        pop.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                            @Override
                            public boolean onMenuItemClick(MenuItem item) {
                                int choice = item.getItemId();

                                switch(choice) {
                                case R.id.message:
                                    break;

                                case R.id.unfollow:
                                        break;
                                case R.id.unfriend:
                                    int position = (Integer)row.getTag();
                                    list.remove(position);
                                    names.remove(position);
                                    images.remove(position);
                                    descr.remove(position);
                                    adapter = new CustomAdapter(context);
                                    lv.setAdapter(adapter);
                                    adapter.notifyDataSetChanged();
                                    break;

                                case R.id.cancel:
                                }

                                return false;
                            }
                        });
                    }
                });

            return row;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

我认为,随着你的结构,你将继续遇到这个问题。我的建议是为每个朋友分配一个FriendID(或类似的东西),当你构建你的列表时,不仅要传递userImage,userName,userDesc和userButton,还要传递friendID。

例如,我有五个朋友,这是他们的信息:

userImage   userName   userDesc   userButton   friendID
x           Jordyn     x          x            0
x           Sam        x          x            1
x           Connor     x          x            2
x           Paul       x          x            3
x           Raphael    x          x            4

但我搜索(假装你可以用一个字母搜索)那些带有&#39; o&#39;在他们的名字返回,

userImage   userName   userDesc   userButton   friendID
x           Jordyn     x          x            0
x           Connor     x          x            2

这样,当你删除第1行时,它实际上会从朋友列表中删除friendID = 2而不是原始朋友列表中的第1行,这将是Sam,这不是你的意图。 / p>

希望有所帮助!

编辑:

1:在您的布局文件中为您的行添加一个名为FriendID的隐藏TextView(如果您需要帮助,请告诉我)。

现在,ViewHolder将如下所示:

class ViewHolder {
            TextView userName;
            TextView userDesc;
            ImageView userImage;
            Button userButton;
            TextView friendID;

            ViewHolder(View view) {
                userImage = (ImageView)view.findViewById(R.id.imageview);
                userName = (TextView)view.findViewById(R.id.title);
                userDesc = (TextView)view.findViewById(R.id.mutualTitle);
                userButton = (Button)view.findViewById(R.id.btn);
                friendID = (TextView)view.findViewById(R.id.friendID);
            }
        }

2:为friendIDs添加一个arraylist:

...
descr = new ArrayList<String>();
        for(int i = 0; i < descBlock.length; i++) {
            descr.add((String)descBlock[i]);
        }
        images = new ArrayList<Integer>();
        for(int i = 0; i < imageBlock.length; i++) {
            images.add((Integer)imageBlock[i]);
        }
        friendIDs = new ArrayList<Integer>();
        for(int i = 0; i < friendIDsBlock.length; i++) {
            images.add((Integer)friendIdsBlock[i]);
        }
...

3:searchField.addTextChangedListener现在看起来像:

int size = nameBlock.length;
        for(int i = 0 ; i < size; i++) {
            Log.d("FREINDADD", "Freind Added" + i);
            list.add(new Friend(i, names.get(i), descr.get(i), images.get(i)));
            //friendList2.add(new Friend(i, names.get(i), descr.get(i), images.get(i), friendIds.get(i)));
        }
        Log.i("Application", "Application started succesfully...");

4:现在,当你与某人取消联系时,请确保在所选行中获取FriendID而不是行索引。然后,使用给定的FriendID以及具有给定FriendID的常规朋友列表中的朋友,从搜索列表中删除该朋友。

你必须原谅我,我现在还没有在你面前有一个IDE,但我认为这涵盖了它!