[英]Hide dynamically added buttons based on an if statement
我似乎無法弄清楚該怎么做,基本上我有一個帶有一堆按鈕的列表視圖,每個按鈕都有自己的單獨值,在我想隱藏所有要放入鍋的特定按鈕之前,所有這些都很好用。
此列表視圖具有自己的自定義適配器,其代碼為:
導入已刪除空間,但它們存在。
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;
}
每當我設法隱藏這些按鈕時,它們都會全部隱藏,而不僅僅是有意義的應該隱藏的按鈕? 如果該信息有幫助,則此視圖依賴於異步任務中的某些數據。
非常感謝您提供一些幫助,使我完全困惑我該怎么做。
這是我的主活動中調用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的原因),當條件為true時,還需要將按鈕設置為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);
}
我不知道您的應用程序的邏輯,但我看到了錯誤和兩個可能的解決方案。
因為您遍歷victimArray
並且因為NetPlay.myId
位於數組中間,所以您總是會爭斗為false
。 因此,您應該以false
發起battle
,並在false
成真時立即中斷。
我認為您應該更改支票:
NetPlay.victimArray [i] .equals(NetPlay.myId)
至
NetPlay.victimArray[i].equals(friend.id)
和類似的另一個條件。
我還建議您使用HashSet
代替原始的victimArray
。 它將通過contains
方法而不是循環為您提供最佳搜索。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.