2011-12-17 91 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

相关问题