Пользовательский ArrayAdapter не отображает результаты

1

У меня это довольно сильно. Я уверен, что это просто что-то простое, что мне не хватает, но я не могу понять, что...

Когда я запускаю программу, она открывает диалоговое окно и отображает инициализированный автокомплектTextView. Когда я пытаюсь ввести что-то в него, ничто не падает или отображается иначе, чем текст, который я вводил. Я создал подобную систему в другой части моей программы с той же механикой, но с использованием обычного 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

EDIT: Я узнал, как реализовать вышеупомянутую проблему:

Я использовал 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);
Теги:
android-arrayadapter

1 ответ

0

Вы должны реализовать getCount() и установить количество ваших данных, т.е. objects.length.

Вы также должны установить адаптер в представление с помощью метода setAdapter().

Надеюсь это поможет!

  • 0
    Я добавил это, и он продолжает вести себя так же. Отладка не попала в getCount ().
  • 0
    Проверьте обновленный ответ.
Показать ещё 3 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню