自定义ArrayAdapter不显示结果

时间:2011-12-17 17:10:58

标签: java android android-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);

1 个答案:

答案 0 :(得分:0)

您必须实现getCount()并设置数据的计数,即objects.length。

您还必须使用setAdapter()方法将适配器设置为视图。

希望这有帮助!

相关问题