2011-12-17 134 views
1

這一個讓我非常難過。我敢肯定它只是簡單的我缺少的東西,但我似乎無法找出什麼...自定義ArrayAdapter不顯示結果

當我運行該程序時,它打開對話框並顯示我已初始化的AutoCompleteTextView。當我嘗試在其中輸入內容時,沒有任何內容會下降或顯示爲除輸入的文本以外的內容。我在我的程序的另一部分中使用相同的機制創建了類似的系統,但是使用了常規的ArrayAdapter並且工作正常界面不是問題。

這裏是我初始化自定義ArrayList的地方。我一直試圖用字符串來簡化它。

final Dialog weaponDialog = new Dialog(BattleScreen.this); 
     weaponDialog.setContentView(R.layout.weapon_selection_dialog); 
     weaponDialog.setTitle("Add a Weapon"); 
     weaponDialog.setCancelable(true); 

     String[] weaponStringArrayList = ConstantEquipmentHelper.getCondensedWeaponString(); 

     WeaponArrayAdapter weaponAdapter = new WeaponArrayAdapter(this, R.layout.weapon_list_item, weaponStringArrayList); 

     weaponDialogAcTextView = (AutoCompleteTextView) weaponDialog.findViewById(R.id.weaponSelectionAutoCompleteTxt); 
     weaponDialogAddButton = (Button) weaponDialog.findViewById(R.id.weaponSelectionAddButton); 
     weaponDialogWeaponInfo = (TextView) weaponDialog.findViewById(R.id.weaponSelectionInformationTxt); 
... 
... 
... 

這裏是我的自定義一個ArrayAdapter類

public class WeaponArrayAdapter extends ArrayAdapter<String> { 

    private Context context; 
    String[] objects; 

    public WeaponArrayAdapter(Context context, int textViewResourceId, String[] objects) { 
     super(context, textViewResourceId); 
     this.objects = objects; 
     this.context = context; 
    } 

    private class WeaponItemHolder { 
     TextView weaponName; 
     TextView weaponCat; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     //return super.getView(position, convertView, parent); 
     final WeaponItemHolder holder; 
     if (convertView == null) { 
      //Sets up a new holder to temporaraly hold the listeners that will be assigned to the binded variables 
      holder = new WeaponItemHolder(); 

      inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      convertView = inflater.inflate(R.layout.weapon_list_item, null); 

      //Find the IDs! Find them!!!! 
      holder.weaponName = (TextView) convertView.findViewById(R.id.weaponListItemName); 
      holder.weaponCat = (TextView) convertView.findViewById(R.id.weaponListItemCategory); 

      //"Sets the tag associated with this view. A tag can be used 
      //to mark a view in its hierarchy and does not have to be unique within the hierarchy." 
      convertView.setTag(holder); 
     } else { 
      holder = (WeaponItemHolder) convertView.getTag(); 
     } 

     String spellName = objects[position]; 

     String[] weaponInfo = spellName.split("\\:"); 
     weaponInfo[1] = weaponInfo[1].trim(); 

     holder.weaponName.setText(weaponInfo[0]); 
     holder.weaponCat.setText(weaponInfo[1]); 

     return convertView; 
    } 

} 

附加信息:我試圖調試它,它永遠不會到達getView。這當然是有道理的,因爲它沒有顯示任何東西。

感謝, -Andrew

編輯:我已經找到了如何實現上述問題:

我用了一個SimpleAdapter具有自定義佈局。 但是,現在我無法選擇任何項目...當我嘗試單擊它時,onItemClick甚至沒有被調用。它可能與使用SimpleAdapter有關?

LINK:http://lemonbloggywog.wordpress.com/2011/02/15/customer-autocomplete-contacts-android/

ArrayList<Map<String, String>> weaponStringArrayList = ConstantEquipmentHelper.getCondensedWeaponString(); 


     //The adapter that recieves the layout type from android and the array creatd by the above function. 
     SimpleAdapter simpleAdapter = new SimpleAdapter(this, weaponStringArrayList, R.layout.weapon_list_item ,new String[] {"name", "category"}, new int[] { R.id.weaponListItemName, R.id.weaponListItemCategory}); 

     //Find the view blah blah blah... 
     weaponDialogAcTextView = (AutoCompleteTextView) weaponDialog.findViewById(R.id.weaponSelectionAutoCompleteTxt); 
     weaponDialogAddButton = (Button) weaponDialog.findViewById(R.id.weaponSelectionAddButton); 
     weaponDialogWeaponInfo = (TextView) weaponDialog.findViewById(R.id.weaponSelectionInformationTxt); 

     //Set that adapter! 
     weaponDialogAcTextView.setAdapter(simpleAdapter); 

回答

0

您必須實現getCount將(),並設置你的數據,即objects.length的計數。

您還必須使用setAdapter()方法將適配器設置爲視圖。

希望這會有所幫助!

+0

我補充說,它仍然表現相同。調試沒有得到getCount()。 – 2011-12-17 17:22:44

+0

檢查更新的答案。 – 2011-12-18 00:40:06

+0

這是什麼運氣? – 2011-12-18 13:11:13

相關問題