java - 隐藏基于 if 语句动态添加的按钮

标签 java android listview button view

我似乎不知道如何做到这一点,基本上我有一个 ListView ,其中有一堆按钮,每个按钮都有自己的单独值,这一切都很好,直到我想隐藏某些按钮为止锅。

这个 ListView 有它自己的自定义适配器,其代码是:

由于空间原因,导入已被删除,但它们仍然存在。

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

每当我设法隐藏这些按钮时,它们都会被隐藏,而不仅仅是应该隐藏的按钮(如果有意义的话)? 此 View 依赖于来自异步任务的一些数据(如果该信息有帮助的话)。

非常感谢您对此提供帮助,这让我完全困惑,不知道如何才能做到这一点。

这是在我的 Main Activity 中调用 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);
}

关于java - 隐藏基于 if 语句动态添加的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11964437/

相关文章:

android - 如何引用使用 Intent/startService 创建的对象

c++ - 如何通过按 "cancel"键取消 CListCtrl 中的编辑?

java - 从 java 运行交互式 Dos 程序

java - 使用多个 java 概念的箭头悲伤游戏

android - 如果 android :inputType ="textCapSentences" used in textview,椭圆大小不工作

java - viewPager 上的 RecyclerView 崩溃

.net - 填充 ListView 多列

java - 将资源中的 Drawable 添加到 ListView 中

java - 如何在 Apache Ignite 2.1 中列出一个缓存内的所有缓存名称和所有列名称?

java - 如何覆盖从父复杂类型继承的元素类型?