ExpandableList no muestra la lista en la pantalla

Estoy tratando de implementar una búsqueda. Activity, que contiene un ExpandableList mostrando Canciones, Álbumes, Artista en grupos separados.

Todas las consultas funcionan bien y el adaptador obtiene los resultados de búsqueda correctos. Pero la lista no se muestra en la pantalla.

Aquí está el código:

Actividad de búsqueda:

public class SearchActivity extends Activity implements TextWatcher {

    private EditText etSearchBar;
    private ExpandableListView lvExpSearchResult;
    private SearchExpandListAdapter expand_adapter;
    private Cursor cursor;
    int columnIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);

        etSearchBar = (EditText) findViewById(R.id.etSearchBar);
        lvExpSearchResult = (ExpandableListView) findViewById(R.id.lvExpSearchResult);

        etSearchBar.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence paramCharSequence,
            int paramInt1, int paramInt2, int paramInt3) {
    }

    @Override
    public void onTextChanged(CharSequence paramCharSequence, int paramInt1,
            int paramInt2, int paramInt3) {
        ArrayList<ExpandListGroup> groups = new ArrayList<ExpandListGroup>();
        ArrayList<String> result = null;

        ExpandListGroup trackSection = new ExpandListGroup();
        result = getQueryResult(MediaStore.Audio.Media.TITLE);
        trackSection.setName("Tracks");
        trackSection.setItems(result);

        ExpandListGroup albumSection = new ExpandListGroup();
        result = getQueryResult(MediaStore.Audio.Media.ALBUM);
        albumSection.setName("Albums");
        albumSection.setItems(result);

        ExpandListGroup artistSection = new ExpandListGroup();
        result = getQueryResult(MediaStore.Audio.Media.ARTIST);
        artistSection.setName("Artist");
        artistSection.setItems(result);

        groups.add(trackSection);
        groups.add(albumSection);
        groups.add(artistSection);

        expand_adapter = new SearchExpandListAdapter(this, groups);
        Toast.makeText(this, "onTextChanged", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void afterTextChanged(Editable paramEditable) {
        lvExpSearchResult.setAdapter(expand_adapter);
        Toast.makeText(this, "afterTextChanged", Toast.LENGTH_SHORT).show();
        Toast.makeText(this, ((Integer)expand_adapter.getChildrenCount(1)).toString(), Toast.LENGTH_SHORT).show();
    }

    private ArrayList<String> getQueryResult(String columnToDisplay) {
        String searchQuery = etSearchBar.getText().toString();
        ArrayList<String> result = new ArrayList<String>();

        if (searchQuery.length() > 0) {
            String[] projection = { MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.DISPLAY_NAME,
                    MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM };

            String queryString = MediaStore.Audio.Media.IS_MUSIC + "!=" + 0
                    + " AND " + columnToDisplay + " LIKE \"%" + searchQuery
                    + "%\"";

            cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    projection, queryString, null, columnToDisplay
                            + " COLLATE NOCASE");

            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
                    .moveToNext()) {
                columnIndex = cursor.getColumnIndexOrThrow(columnToDisplay);
                result.add(cursor.getString(columnIndex));
            }
        } else {
            result.clear();
        }
        return result;
    }
}

Clase de adaptador:

public class SearchExpandListAdapter extends BaseExpandableListAdapter {

    Context context;
    ArrayList<ExpandListGroup> groups;

    public SearchExpandListAdapter(Context c, ArrayList<ExpandListGroup> groups) {
        this.context = c;
        this.groups = groups;
    }

    public void addItem(String item, String group) {
        if (!groups.contains(group)) {
            Toast.makeText(context,
                    "Error while adding item in group " + group,
                    Toast.LENGTH_SHORT).show();
            return;
        }
        int index = groups.indexOf(group);
        ArrayList<String> children = groups.get(index).getItems();
        children.add(item);
        groups.get(index).setItems(children);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<String> children = groups.get(groupPosition).getItems();
        String child = children.get(childPosition);
        return child;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View view, ViewGroup parent) {
        String child = (String) getChild(groupPosition, childPosition);
        if (view == null) {
            LayoutInflater inflator = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = inflator.inflate(R.layout.expandlist_child_item, null);
        }
        TextView tvChild = (TextView) view.findViewById(R.id.tvChild);
        tvChild.setText(child);
        return view;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<String> children = groups.get(groupPosition).getItems();
        return children.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isLastChild, View view,
            ViewGroup parent) {
        ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);
        if (view == null) {
            LayoutInflater inflator = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = inflator.inflate(R.layout.expandlist_group_item, null);
        }
        TextView tvGroup = (TextView) view.findViewWithTag(R.id.tvGroup);
        tvGroup.setText(group.getName());
        return view;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int paramInt1, int paramInt2) {
        return true;
    }
}

preguntado el 27 de julio de 12 a las 15:07

¿Ves tus mensajes de notificación en afterTextChanged? -

sí... la lista se está rellenando, parece que hay algún problema con el adaptador... usando ExpandableListAdapter por primera vez... así que no estoy seguro si lo implementé correctamente -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.