隐藏基于if语句动态添加的按钮 [英] Hide dynamically added buttons based on an if statement

查看:94
本文介绍了隐藏基于if语句动态添加的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚该怎么做,基本上我有一个带有一堆按钮的列表视图,每个按钮都有各自的值,在我想隐藏所有要使用的按钮之前,所有这些都很好用锅.

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);

推荐答案

问题在于以下代码块:

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

您知道ListView项已回收(这就是为什么您应该使用 ViewHolder ),则还需要在条件为真时将按钮设置为View.VISIBLE.

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.

您的代码应如下所示:

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);
}

这篇关于隐藏基于if语句动态添加的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆