将listView分成几类

时间:2014-08-09 18:33:52

标签: android listview categories

在我的应用程序中,我有一个listView,其中包含200多个项目,这些项目是使用数据库帮助程序类从数据库中检索的。此listView需要分为10个不同的类别。因此,我正在添加另一个(父)视图。实现这一目标的最佳方法是什么?在需要尽可能少的代码重构的意义上最好的。

 public class CompaniesList extends ListActivity {

        protected EditText searchText;
        protected SQLiteDatabase db;
        protected Cursor cursor;
        protected ListAdapter adapter;


        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            searchText = (EditText) findViewById(R.id.searchText);
            db = (new DatabaseHelper(this)).getWritableDatabase();
        }


        public void onListItemClick(ListView parent, View view, int position,
                long id) {
            Intent intent = new Intent(this, CompanyDetails.class);
            Cursor cursor = (Cursor) adapter.getItem(position);
            intent.putExtra("EMPLOYEE_ID",
                    cursor.getInt(cursor.getColumnIndex("_id")));
            startActivity(intent);
        }


        public void search(View view) {
            // || is the concatenation operation in SQLite
            cursor = db
                    .rawQuery(
                            "SELECT _id, name, address FROM employee WHERE employee.name LIKE ?",
                            new String[] { "%" + searchText.getText().toString()
                                    + "%" });
            adapter = new SimpleCursorAdapter(this, R.layout.company_list_item,
                    cursor, new String[] { "name", "address" }, new int[] {
                            R.id.name, R.id.address }, 0);
            setListAdapter(adapter);
        }

        class CompanyCategoryAdapter extends ArrayAdapter<CompaniesList> {

private LayoutInflater inflator;

    static class ViewHolder {
        public TextView text;
        public ImageView image;
    }
            private static final int ROW_TYPE_AAA = 0;
            private static final int ROW_TYPE_BBB = 1;
            private static final int ROW_TYPE_CCC = 2;
            private static final int ROW_TYPE_DDD = 3;
            private static final int ROW_TYPE_EEE = 4;
            private static final int ROW_TYPE_FFF = 5;
            private static final int ROW_TYPE_GGG = 6;
            private static final int ROW_TYPE_HHH = 7;
            private static final int ROW_TYPE_III = 8;
            private static final int ROW_TYPE_JJJ = 9;
            private static final int ROW_TYPE_KKK = 10;
            private static final int ROW_TYPE_LLL = 11;
            private static final int ROW_TYPE_MMM = 12;
            private static final int ROW_TYPE_NNN = 13;

            CompanyCategoryAdapter() {
                super(CompaniesList.this, R.layout.main, companies);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View row = convertView;
                Companies viewHolder;

                if (row == null) {
                    LayoutInflater inflater = getLayoutInflater();
                    switch (getItemViewType(position)) {
                    case ROW_TYPE_AAA:
                        row = inflater.inflate(R.layout.row_aaa, null);
                        break;
                    case ROW_TYPE_BBB:
                        row = inflater.inflate(R.layout.row_bbb, null);
                        break;
                    case ROW_TYPE_CCC:
                        row = inflater.inflate(R.layout.row_ccc, null);
                        break;
                    case ROW_TYPE_DDD:
                        row = inflater.inflate(R.layout.row_ddd, null);
                        break;
                    case ROW_TYPE_EEE:
                        row = inflater.inflate(R.layout.row_eee, null);
                        break;
                    case ROW_TYPE_FFF:
                        row = inflater.inflate(R.layout.row_fff, null);
                        break;
                    case ROW_TYPE_GGG:
                        row = inflater.inflate(R.layout.row_ggg, null);
                        break;
                    case ROW_TYPE_HHH:
                        row = inflater.inflate(R.layout.row_hhh, null);
                        break;
                    case ROW_TYPE_III:
                        row = inflater.inflate(R.layout.row_iii, null);
                        break;
                    case ROW_TYPE_JJJ:
                        row = inflater.inflate(R.layout.row_jjj, null);
                        break;
                    case ROW_TYPE_KKK:
                        row = inflater.inflate(R.layout.row_kkk, null);
                        break;
                    case ROW_TYPE_LLL:
                        row = inflater.inflate(R.layout.row_lll, null);
                        break;
                    case ROW_TYPE_MMM:
                        row = inflater.inflate(R.layout.row_mmm, null);
                        break;
                    case ROW_TYPE_NNN:
                        row = inflater.inflate(R.layout.row_nnn, null);
                        break;
                    default:
                        row = inflater.inflate(R.layout.main, null);
                        break;
                    }

                    viewHolder = new CompanyHolder(row);
                    row.setTag(viewHolder);
                } else {
                    viewHolder = (CompanyHolder) row.getTag();
                }

                viewHolder.populateFrom(companies.get(position));

                return row;
            }

            @Override
            public int getViewTypeCount() {
                return 14;
            }

            @Override
            public int getItemViewType(int position) {
                Cursor cursor = (Cursor) adapter.getItem(position);
                String type = cursor.getString(cursor.getColumnIndex("department"));
                if (type.equals("aaa") == true) {
                    return ROW_TYPE_AAA;
                } else if (type.equals("bbb") == true) {
                    return ROW_TYPE_BBB;
                } else if (type.equals("ccc") == true) {
                    return ROW_TYPE_CCC;
                } else if (type.equals("ddd") == true) {
                    return ROW_TYPE_DDD;
                } else if (type.equals("eee") == true) {
                    return ROW_TYPE_EEE;
                } else if (type.equals("fff") == true) {
                    return ROW_TYPE_FFF;
                } else if (type.equals("ggg") == true)  {
                    return ROW_TYPE_GGG;
                } else if (type.equals("hhh") == true)  {
                    return ROW_TYPE_HHH;
                } else if (type.equals("iii") == true)  {
                    return ROW_TYPE_III;
                } else if (type.equals("jjj") == true)  {
                    return ROW_TYPE_JJJ;
                } else if (type.equals("kkk") == true)  {
                    return ROW_TYPE_KKK;
                } else if (type.equals("lll") == true)  {
                    return ROW_TYPE_LLL;
                } else if (type.equals("mmm") == true)  {
                    return ROW_TYPE_MMM;
                } else {
                    return ROW_TYPE_NNN;
                }

            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以覆盖getViewTypeCount()和getItemViewType

@Override
public int getViewTypeCount() {
    return 10;
}

@Override
public int getItemViewType(int position) {
    return getItem(position).getType(); // or whatever is your criteria for distinguish the Types
}

使用这些方法,您可以为getView(..)中的每种类型制作不同的布局,而无需担心已回收的视图。