我似乎无法弄清楚如何做到这一点,基本上我有一个列表视图,其中包含一堆按钮,每个按钮都有自己的单独值,这一切都很好,直到我想隐藏某些按钮它都去锅了。
这个列表视图有它自己的自定义适配器,代码是:
为空间删除了导入,但它们存在。
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;
}
每当我设法隐藏这些按钮时,它们都会被隐藏,而不仅仅是应该隐藏的按钮,如果这有意义的话?如果该信息有帮助,此视图依赖于来自 asynchtask 的一些数据。
真的很感谢一些帮助,让我完全困惑我怎么可能做到这一点。
这是在我的主要活动中调用列表视图的地方。
listView = (ListView) findViewById(R.id.friendsview);
friendsArrayAdapter = new FriendsArrayAdapter(this, R.layout.rowlayout, friends);
listView.setAdapter(friendsArrayAdapter);