问题
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);
回答1:
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);
}
回答2:
I don't know logic of your application but I see error and two possible solution.
Because you looping over
victimArray
and becauseNetPlay.myId
is in the middle of array you always get battle asfalse
. So you should initiatebattle
withfalse
and break as soon as it's true.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.
来源:https://stackoverflow.com/questions/11964437/hide-dynamically-added-buttons-based-on-an-if-statement