0

I can't seem to figure out how to do this, basically I have a list view with a bunch of buttons that each have their own individual values, this all works great until I want to hide certain buttons it all goes to pot.

This listview has it's own custom adaptor the code for this is:

Imports removed for space but they exist.

public class FriendsArrayAdapter extends ArrayAdapter<Friend> 
{
    public FriendsArrayAdapter
    (Activity context, int resourceId, ArrayList<Friend> friends) 
{
    super(context, resourceId, friends);
    this.context = context;
    this.friends = friends;
    this.resourceId = resourceId;



}



@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View rowView = convertView;
    if (rowView == null) 
    {
        LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = vi.inflate(resourceId, null);
    }



    alert = new AlertDialog.Builder(context);
    alert.setTitle("Hanged! Online Play");
    alert.setMessage("Enter a word for your opponent to guess. Alphabetical characters only and maximum of 25 characters long.");
    final EditText input = new EditText(context);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
    {
    public void onClick(DialogInterface dialog, int whichButton) 
    {
       wordToGuess = input.getText().toString();
       SubmitWord submitTask = new SubmitWord();
       submitTask.execute(new String[] { "http://www.hanged.comli.com/main.php" });
       Log.i("postData", wordToGuess);
     }
    });

   alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
    {
      public void onClick(DialogInterface dialog, int whichButton) 
      {
          Intent intent = new Intent(context, NetPlay.class);
          intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);       
          context.startActivity(intent);
      }
    });

    final Friend f = friends.get(position);
    TextView rowTxt = (TextView) rowView.findViewById(R.id.rowtext_top);
    rowTxt.setText(f.name+"       ");




    Button battleBtn = (Button) rowView.findViewById(R.id.battleBtn);
    battleBtn.setText(f.id+" Accept Challenge");
    battleBtn.setOnClickListener(new OnClickListener() 
    {
           @Override
           public void onClick(View v) 
           {
               rivalid = f.id;
               AcceptChallenge task2 = new AcceptChallenge();
               task2.execute(new String[] { "http://www.hanged.comli.com/get-currentgame.php" });
           }
    });

    Button newgameBtn = (Button) rowView.findViewById(R.id.newgameBtn);
    newgameBtn.setText(f.id+" Start new game.");

    newgameBtn.setOnClickListener(new OnClickListener() 
    {
           @Override
           public void onClick(View v) 
           {
               rivalid = f.id;
               alert.show();  
           }   
    });



        for (int i = 0;i<NetPlay.victimArray.length-1;i++)
        {
            Log.i("VICTIM ARRAY DATA ", NetPlay.victimArray[i]);

            if (NetPlay.victimArray[i].equals(NetPlay.myId))
            {
                ingame = false;
            }
            else
            {
                ingame = true;
            }
        }

        for (int i = 0;i<NetPlay.rivalArray.length-1;i++)
        {
            Log.i("RIVAL ARRAY DATA ", NetPlay.rivalArray[i]);

            if (NetPlay.rivalArray[i].equals(NetPlay.myId))
            {
                battle = true;
            }
            else
            {
                battle = false;
            }

        }

        if (battle.equals(false))
        {
            battleBtn.setVisibility(View.INVISIBLE);
        }
        if (ingame.equals(false))
        {
            newgameBtn.setVisibility(View.INVISIBLE);
        }


    return rowView;
}

Whenever I manage to hide these buttons, they all get hidden rather than just the buttons that should be hidden if that makes sense? This view relies on some data from an asynchtask if that information helps any.

Would really appreciate some help with this, completely confusing me as to how I could possibly do it.

This is where the listview is called, in my Main activity.

listView = (ListView) findViewById(R.id.friendsview);
friendsArrayAdapter = new FriendsArrayAdapter(this, R.layout.rowlayout, friends);
listView.setAdapter(friendsArrayAdapter);
Daniel Waters
  • 431
  • 3
  • 21
  • every listView item has 'n' buttons. when you are trying to hide 1 of those buttons, all the 'n' buttons are hidden. right?? or all the buttons of all the listView items? – Archie.bpgc Aug 15 '12 at 05:33
  • Yeh you are right, however they are individual buttons as they each have a Unique Text value, so I assumed they were also unique and wouldnt just be mass removed, the buttons are added to the listview in the same class that they are being hidden in, so I can't figure out what the problem is with them – Daniel Waters Aug 15 '12 at 05:37
  • You should **NOT** post your entire source code. Only the segment that isn't functioning properly should be enough. – JoxTraex Aug 15 '12 at 05:45
  • I thought the full thing would give a better view of what I was trying to do, I will trim some out – Daniel Waters Aug 15 '12 at 05:47

2 Answers2

2

The problem is with this block of code:

if (battle.equals(false)) {
  battleBtn.setVisibility(View.INVISIBLE);
}
if (ingame.equals(false)) {
  newgameBtn.setVisibility(View.INVISIBLE);
}

As you're aware that ListView items are recycled (that's why you should use a ViewHolder), you also need to set the buttons to View.VISIBLE when the condition is true.

Your code should look like this:

if (battle.equals(false)) {
  battleBtn.setVisibility(View.INVISIBLE);
} else {
  battleBtn.setVisibility(View.VISIBLE);
if (ingame.equals(false)) {
  newgameBtn.setVisibility(View.INVISIBLE);
} else {
  newgameBtn.setVisibility(View.VISIBLE);
}
Community
  • 1
  • 1
josephus
  • 8,284
  • 1
  • 37
  • 57
1

I don't know logic of your application but I see error and two possible solution.

  1. Because you looping over victimArray and because NetPlay.myId is in the middle of array you always get battle as false. So you should initiate battle with false and break as soon as it's true.

  2. I assume you should change check:

    NetPlay.victimArray[i].equals(NetPlay.myId)

to

NetPlay.victimArray[i].equals(friend.id)

And similar for another condition.

I would also suggest you to use HashSet instead of primitive victimArray. It will give you more optimal search with contains method instead of looping.

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
  • You are right I should make it break before it sets itself false again thanks. friend.id and myID are different things, I will look into HashSet though thanks for the suggestion. – Daniel Waters Aug 15 '12 at 05:56
  • It didn't solve the original question though while it solved errors, the problem still remains that all the buttons become hidden rather than the ones that are supposed to, like they are tied together – Daniel Waters Aug 15 '12 at 06:04