2014-02-10 35 views
0

我遇到顯示ListView項的問題。當我刪除自己的條件時,所有東西都顯示正確,但是當我重新添加條件以使用戶看不到時,我的列表視圖最終會出現空白的地方/行,而我不知道如何修復它。哪裏不對?條件是否正確?帶有條件的ListView顯示空行

@Override                     
public View getView(int position, View convertView, ViewGroup parent) { 
    ApplicationInfo entry = aListAppInfo.get(position); 
    View v = convertView;                 
    // convertView 
    // recyklovace 
    if(v == null){ 
     LayoutInflater inflater = LayoutInflater.from(aContext); 
     v = inflater.inflate(R.layout.layout_appinfo, null); 
    }else{} 
    //nahrat layout 
    ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon); 
    TextView tvAppName = (TextView)v.findViewById(R.id.tvName); 
    TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack); 
    chAppStat = (ToggleButton)v.findViewById(R.id.switch1); 
    chAppStat.setTag(entry.packageName); 
    chAppStat.setOnCheckedChangeListener(aListener);                   
    if (aListAppInfo.get(position).packageName == aListAppInfo.get(position).loadLabel (aPackManager) 
        ||((entry.packageName.equals("android")) 
        || (entry.packageName.equals("com.android.systemui")) 
        || (entry.packageName.equals("com.android.dreams.basic")) 
        || (entry.packageName.equals("com.android.certinstaller")) 
        || (entry.packageName.equals("com.android.defcontainer")) 
        || (entry.packageName.equals("com.android.htmlviewer")) 
        || (entry.packageName.equals("com.android.keychain")) 
        || (entry.packageName.equals("com.android.location.fused")) 
        || (entry.packageName.equals("com.android.providers.applications")) 
        || (entry.packageName.equals("com.android.providers.userdictionary")))){ 
     chAppStat.setVisibility(View.INVISIBLE);   
    }else{ 
     chAppStat.setVisibility(View.VISIBLE); 
    } 
    ivAppIcon.setImageDrawable(entry.loadIcon(aPackManager)); 
    tvPkgName.setText(entry.packageName); 
    tvAppName.setText(entry.loadLabel(aPackManager)); 
    return v; 
// navrat view 
} 
+2

將其更改爲'View.GONE' – thepoosh

回答

0

有幾件事情:

  • 分配convertViewv。不要那樣做,沒有必要,只需直接與convertView一起工作即可。

  • 改爲使用原生ArrayAdaptergetItem(position)。這將管理List你已經通過你的ArrayAdapter並從中獲取物品,因此不需要使用自己的結構化物品。使用View.GONE代替View.INVISIBLE。這將使佈局完全消失,而不是在可見時留下適合的空間。

0

條件爲真時,您會獲得空白空格嗎?你想刪除那個空間?如果是,則使用View.GONE而不是View.INVISIBLE

View.INVISIBLE使當前視圖對用戶不可見,但不刪除由它分配的空間。因此顯示兩個ListView項目之間的空白空間。

View.GONE不僅使視圖不可見,而且還刪除該空間,以便其他人可以使用該空間。因此兩個ListView項目之間不會有差距。

更改chAppStat.setVisibility(View.INVISIBLE);chAppStat.setVisibility(View.GONE);

0

這裏有兩點需要注意。

首先,檢查Android文檔,看看實際上是什麼View.INVISIBLE。正如其他人已經提到的那樣,View.GONE會更適合您的情況。

其次,我不會試圖隱藏這樣的細胞。過濾列表應該是首選方式。在使用適配器之前篩選列表,或者在自定義適配器上使用篩選。可以找到一個示例here,但該技術更適用於列表的運行時過濾,就像用戶可以使用查詢過濾列表一樣。