繁体   English   中英

自定义ArrayAdapter不显示结果

[英]Custom ArrayAdapter not displaying results

这让我颇为难过。 我确定这只是我所缺少的简单事物,但我似乎无法找出原因...

当我运行程序时,它将打开对话框并显示我已初始化的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。 这当然是有道理的,因为它什么也不显示。

谢谢,-安德鲁

编辑:我发现了如何实现上述问题:

我使用具有自定义布局的SimpleAdapter。 但是,现在我无法选择任何项...当我尝试单击它时,甚至不会调用onItemClick。 它可能与使用SimpleAdapter有关??

链接: 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);

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

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

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM