基于if语句隐藏动态添加的按钮
我似乎无法弄清楚如何做到这一点,基本上我有一个列表视图,每个按钮都有自己的值,这一切都很好,直到我想要隐藏某些按钮,这一切都会发生。基于if语句隐藏动态添加的按钮
此列表视图都有它自己的自定义适配器该代码是:空间删除
进口,但它们的存在。
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 = (ListView) findViewById(R.id.friendsview);
friendsArrayAdapter = new FriendsArrayAdapter(this, R.layout.rowlayout, friends);
listView.setAdapter(friendsArrayAdapter);
问题是与这个代码块:
if (battle.equals(false)) {
battleBtn.setVisibility(View.INVISIBLE);
}
if (ingame.equals(false)) {
newgameBtn.setVisibility(View.INVISIBLE);
}
正如你知道的ListView项目被回收(这就是为什么你应该使用ViewHolder),你还需要将按钮设置为View.VISIBLE
当条件是真的。
您的代码应该是这样的:
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);
}
谢谢!这解决了它,总是这么简单,以至于过度想到:) – 2012-08-15 06:14:30
很高兴我可以帮助 – josephus 2012-08-15 07:46:47
我不知道你的应用程序的逻辑,但我看到错误和两个可能的解决方案。
因为你遍历
victimArray
因为NetPlay.myId
是在阵列的中间,你总是战斗为false
。所以你应该发起battle
与false
,并尽快中断。-
我想你应该改变检查:
NetPlay.victimArray [I] .equals(NetPlay.myId)
到
NetPlay.victimArray[i].equals(friend.id)
而对于其他条件类似。
我也建议你用HashSet
代替原始的victimArray
。它会给你更多的优化搜索方法,而不是循环。
你是对的我应该让它破裂,然后再次设置它为假再次感谢。 friend.id和myID是不同的东西,但我会考虑HashSet,但感谢这个建议。 – 2012-08-15 05:56:27
请标记为答复 – 2012-08-15 05:58:24
它虽然解决了错误,但并没有解决原来的问题,问题仍然是所有的按钮变得隐藏起来而不是像应该的那样,就像他们绑在一起 – 2012-08-15 06:04:22
每个ListView项有 'N' 按钮。当您试图隐藏其中一个按钮时,所有'n'按钮都将被隐藏。对??或所有listView项目的所有按钮? – 2012-08-15 05:33:19
你是对的,但是它们是单独的按钮,因为它们每个都有独特的文本值,所以我认为它们也是独一无二的,不会被大量移除,按钮将被添加到同一类中的listview中,以便它们被隐藏在,所以我不能找出他们的问题是什么 – 2012-08-15 05:37:12
你应该**不**发布您的整个源代码。只有运行不正常的部分应该足够了。 – JoxTraex 2012-08-15 05:45:23