4

我有一个这样的数据库表:

ID
NAME
COUNTRY_NAME

我想要一个这样的列表:

+ Italy
    - Potato
    - Tomato
+ France
    - Fromage
    - Baguette

我写了一个 CursorAdapter,每次调用 requery 时,读取表上的所有项目并将其映射到一个对象中,该对象用于映射每个项目(实际项目或标题)的位置。

private  static class PersonEntry {
    boolean isHeader;
    String countryName;
    int realPos;
    int id;
}

代码是这样的:

 /* cursor generated by querying whole table */

 public void readHeaders(Cursor cursor ) {
    Log.d(this.getClass().getSimpleName(),"readHeaders init");
    items = new ArrayList<PersonEntry >();
    this.mCursor = cursor;

    cursor.moveToFirst();
    int i = 0;
    String previousCountry = "";
    String currentCountry;
    while(cursor.isAfterLast() == false)  {
        int id = cursor.getInt(cursor.getColumnIndexOrThrow(Match.ROW_ID));
        currentCountry = cursor.getString(cursor.getColumnIndexOrThrow(Person.ROW_COUNTRY_NAME));
        if (!currentCountry.equals(previousCountry)) {
            // ho incontrato una nuova nazione
            // rispetto alla precedente, aggiungiamola
            items.add(new PersonEntry(... define isHeader=true ..));

        }
        // stiamo comunque scorrendo gli elementi, aggiungiamo quello appena trovato
        items.add(new PersonEntry( ... defile isHeader = false ....);
        previousCountry = currentCountry;
        i++;
        cursor.moveToNext();
    }
    cursor.close();
    Log.d(this.getClass().getSimpleName(),"readHeaders end");


}

所以我重写了 getView、bindView 和 newView 来膨胀正确的布局并绑定基于 realPos-position 光标的视图。

该方法有效,但非常昂贵:它需要详细说明整个表,而且我有很多记录。我正在寻找的是一种在滚动 ListView 时映射 realPosition -> fakePosition 的简单方法,但我认为方法太复杂了,我认为如果 getView 不是线性的(快速滚动?)它们会中断。

解决方案: 1) 按 COUNTRY_NAME 查询排序。向下滚动时 real_cursor_position = (requested position - "country changes # (?)")。如果在 real_position 中翻译的请求位置位于具有不同 country_name 的项目之后,则它是一个标题。我认为,除非有棘手的解决方案,否则在向下滚动拳头和向上滚动时它会破裂。...仅此而已

还有其他解决方案吗?

编辑:另一个问题是我无法在不扫描整个表的情况下预测 adapter.getCount() 返回的视图数量。

4

1 回答 1

8

我已经编写了getView简单适配器的方法来向您展示如何ListView使用标题对具有相同国家/地区的项目进行分组。这将假定标题视图位于每一行的布局中,根据当前行的需要显示/隐藏它。同样的事情可以通过使用该getItemViewType方法来完成,并在绑定适配器的各个部分上做更多的工作。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.rowlayout, parent, false);
        }
        // for simplicity, the header it's just a TextView
        TextView header = (TextView) convertView
                .findViewById(R.id.headerPart);
        // also for simplicity, the row content it's just a TextView
        TextView rowText = (TextView) convertView
                .findViewById(R.id.normalPart);
        // set the data for the row
        mCursor.moveToPosition(position);
        rowText.setText(mCursor.getString(mCursor.getColumnIndex("name")));
        // this is not the first position               
        if (position - 1 >= 0) {
            // if there is a previous position see if it has the same
            // country(in which case you already had set the header)
            String currentCountry = mCursor.getString(mCursor
                    .getColumnIndex("country"));
            mCursor.moveToPosition(position - 1);
            String previousCountry = mCursor.getString(mCursor
                    .getColumnIndex("country"));
            if (currentCountry.equalsIgnoreCase(previousCountry)) {
                // the countries are the same so abort everything as we
                // already set the header on one of previous rows
                header.setVisibility(View.GONE);
            } else {
                // this is the first occurrence of this country so show the
                // header
                header.setVisibility(View.VISIBLE);
                header.setText(currentCountry);
            }
        } else {
            // this is position 0 and we need a header here
            header.setVisibility(View.VISIBLE);
            header.setText(mCursor.getString(mCursor
                    .getColumnIndex("country")));
        }
        return convertView;
    }
于 2013-02-09T19:19:36.147 回答