0

Basically, I have a slight problem with interprating a remove feature. The user clicks on a button that is in the ListView, a PopupMenu is then promted and displays "Unfriend", "Unfollow", "Messgae" and "Cancel". When the user clicks on "Unfriend", the Friend that the user wants to unfreind is then removed.

I have a problem with three things; Searching and Removing(to a slight degree):

1 When the User uses the search feature, the Unfriend option becomes non-usable. If the user clicks unfriend. It does nothing...
2 When the User attempts to search for a user than they have previously removed as a friend. It actually displays that user in the search results.
3 When a User removes a friend, and then searches for another friend to remove. It adds all of the previously removed friend back into the list.

There must be something I am doing wrong? Maybe I'm storing data and then re-using it to a certain degree somewhere else down the line?

I DO NOT get any exceptions from this code. It actually runs with minor faults.

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

    res = getResources();
    searchField = (EditText) findViewById(R.id.EditText01);
    list = (ListView) findViewById(android.R.id.list);
    //button = (Button)findViewById(R.id.btnFriendList);
    list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    //button.setFocusable(false);
    friendList = 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);
        friendList.add(new Friend(names.get(i), descr.get(i), images.get(i)));
    }
    Log.i("Application", "Application started succesfully...");

    adapter = new VirtuAdapter(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)
        {
            friendList.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... ");
                        friendList.add(new Friend(names.get(i), descr.get(i), images.get(i)));
                    }
                  }
            }
            AppendList(friendList);
            }
    });


}
public void AppendList(ArrayList<Friend> freind) {
    setListAdapter(new VirtuAdapter(this));
} 
public class VirtuAdapter extends BaseAdapter
{


    Activity content;
    public VirtuAdapter(Activity context)
    {
        this.content = context;
    }
    @Override
    public int getCount()
    {
        return friendList.size();

    }
    @Override
    public Object getItem(int position)
    {
        return friendList.get(position);
    }
    @Override
    public long getItemId(int position)
    {
        return friendList.size();
    }

    class ViewHolder {
        TextView myTitle;
        TextView myDescription;
        ImageView myImage;
        Button myButton;

        ViewHolder(View view) {
            myImage = (ImageView)view.findViewById(R.id.imageview);
            myTitle = (TextView)view.findViewById(R.id.title);
            myDescription = (TextView)view.findViewById(R.id.mutualTitle);
            button = (Button)view.findViewById(R.id.btnFriendList);
        }
    }
    @Override
    public View getView(final int position, View view, ViewGroup parent)
    {
        View row = view;
        ViewHolder holder = null;

        if(row == null) 
        {
            // If it is visible to the user, deploy the row(s) - allocated in local memory
            LayoutInflater inflater = (LayoutInflater)content .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 = friendList.get(position);

        friend = friendList.get(position);
        String length = "SIZE:" + adapter.getCount();
        System.out.println("SIZE"+ length);

        Log.d("VIRTU", length);
        // Set the resources for each component in the list
        holder.myImage.setImageResource(temp.getImage());
        holder.myTitle.setText(temp.getName());
        holder.myDescription.setText(temp.getDesc());

        ((Button)row.findViewById(R.id.btnFriendList)).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());
                friend.setPosition(position);
                Toast.makeText(content, "" + position, Toast.LENGTH_SHORT ).show();
                pop.show();
                pop.setOnMenuItemClickListener(MainActivity.this);
            }
        });

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

    switch(choice) {
    case R.id.message:
        Toast.makeText(getApplicationContext(),"Clicked on Message", Toast.LENGTH_SHORT).show();
        break;
    case R.id.unfollow:
        Toast.makeText(getApplicationContext(),"Clicked on Unfollow", Toast.LENGTH_SHORT).show();

        break;
    case R.id.unfriend:
        Toast.makeText(getApplicationContext(),"Clicked on Unfreind", Toast.LENGTH_SHORT).show();
        Friend temp = friendList.get(friend.getPosition());

        friendList.remove(temp);            
        adapter.notifyDataSetChanged();
        adapter.notifyDataSetInvalidated();
        break;
    case R.id.cancel:
        Toast.makeText(getApplicationContext(),"Clicked on Cancel", Toast.LENGTH_SHORT).show();
        break;
        default: break;
    }
    return false;
}

}

peteratkinson1
  • 90
  • 1
  • 11

1 Answers1

0

The problem is in the way how position is stored. When the button click listener is called, the position value is not the row that the button is clicked. To get the correct value, you need to save the position in your ViewHolder object when getView() is called and move your button click listener into the if(row == null) block. There is no need to set the click listener on every call of getView(). It only needs to be set when the layout is created. To see how this is done, please see the answer of a similar question.

Community
  • 1
  • 1
hfann
  • 599
  • 3
  • 13
  • sorry I dont quite get what you mean? It is removing the elements in the ListView. Only things start to mess up when I search for something. – peteratkinson1 Jun 16 '14 at 01:27
  • Oh my goodness! I've fixed it, and it works flawlessly! Thank-you so much 5 days of tireless programming and it finally works! you are INCREDIBLY! THUMBS UP! Thank-you hfann – peteratkinson1 Jun 16 '14 at 03:16