2012-08-15 81 views
0

我似乎無法弄清楚如何做到這一點,基本上我有一個列表視圖,每個按鈕都有自己的值,這一切都很好,直到我想要隱藏某些按鈕,這一切都會發生。基於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); 
+0

每個ListView項有 'N' 按鈕。當您試圖隱藏其中一個按鈕時,所有'n'按鈕都將被隱藏。對??或所有listView項目的所有按鈕? – 2012-08-15 05:33:19

+0

你是對的,但是它們是單獨的按鈕,因爲它們每個都有獨特的文本值,所以我認爲它們也是獨一無二的,不會被大量移除,按鈕將被添加到同一類中的listview中,以便它們被隱藏在,所以我不能找出他們的問題是什麼 – 2012-08-15 05:37:12

+0

你應該**不**發佈您的整個源代碼。只有運行不正常的部分應該足夠了。 – JoxTraex 2012-08-15 05:45:23

回答

2

問題是與這個代碼塊:

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

謝謝!這解決了它,總是這麼簡單,以至於過度想到:) – 2012-08-15 06:14:30

+0

很高興我可以幫助 – josephus 2012-08-15 07:46:47

1

我不知道你的應用程序的邏輯,但我看到錯誤和兩個可能的解決方案。

  1. 因爲你遍歷victimArray因爲NetPlay.myId是在陣列的中間,你總是戰鬥爲false。所以你應該發起battlefalse,並儘快中斷。

  2. 我想你應該改變檢查:

    NetPlay.victimArray [I] .equals(NetPlay.myId)

NetPlay.victimArray[i].equals(friend.id) 

而對於其他條件類似。

我也建議你用HashSet代替原始的victimArray。它會給你更多的優化搜索方法,而不是循環。

+0

你是對的我應該讓它破裂,然後再次設置它爲假再次感謝。 friend.id和myID是不同的東西,但我會考慮HashSet,但感謝這個建議。 – 2012-08-15 05:56:27

+0

請標記爲答覆 – 2012-08-15 05:58:24

+0

它雖然解決了錯誤,但並沒有解決原來的問題,問題仍然是所有的按鈕變得隱藏起來而不是像應該的那樣,就像他們綁在一起 – 2012-08-15 06:04:22