如何使用过滤实现多层列表视图?

时间:2014-02-06 17:13:46

标签: android listview filter adapter layer

目前,我已经实现了一个多层固定列表视图,通过它可以将一个包含listview的linearlayout子类化。它只是一个带有listview的标题,用于保持标题位于顶部。我已经实现了三个不同的适配器来使用它。 BuildingListAdapter,FloorListAdapter和RoomListAdapter。

他们各自坐在一起,我认为我过于复杂,现在它有点工作,有点不然。简单的想法,真的。想出一个属于项目的建筑物。如果您选择建筑物,它会过滤地板并显示该建筑物中的地板,如果您选择地板,则会显示地板的房间。 Buuut!有时候它非常奇怪而且挑剔。我遇到的主要问题是它滚动时。如果我点击建筑物,它会突出显示绿色以显示已被选中,当我再次向下滚动列表时,它不再是绿色。我只是想知道是否有人有一个干净,类似的例子,因为我觉得要废弃我的所有代码,因为它看起来很糟糕,很难维护。

public class BuildingListAdapter extends BaseAdapter implements ListAdapter {

private Activity context;
private ArrayList<Building> buildingCollection;
public FloorListAdapter floorAdapter;
private DataSource datasource;

/**
 * @param context
 * @param buildingCollection
 */
public BuildingListAdapter(Activity context,
        ArrayList<Building> buildingCollection) {
    this.context = context;
    this.buildingCollection = buildingCollection;
}

/**
 * @param context
 * @param floorAdapter
 * @param buildingList
 */
public BuildingListAdapter(Activity context, FloorListAdapter floorAdapter,
        ArrayList<Building> buildingList) {
    this.context = context;
    this.datasource = new DataSource(context);
    datasource.open();
    this.floorAdapter = floorAdapter;
    this.buildingCollection = buildingList;
}

/**
 * @return
 */
public FloorListAdapter getFloorCollection() {
    return floorAdapter;
}

/**
 * @param floorCollection
 */
public void setFloorCollection(FloorListAdapter floorCollection) {
    this.floorAdapter = floorCollection;
}

/* (non-Javadoc)
 * @see android.widget.Adapter#getCount()
 */
public int getCount() {
    return buildingCollection.size();
}

/* (non-Javadoc)
 * @see android.widget.Adapter#getItem(int)
 */
public Object getItem(int position) {
    return buildingCollection.get(position);
}

/* (non-Javadoc)
 * @see android.widget.Adapter#getItemId(int)
 */
public long getItemId(int position) {
    return position;
}

/* (non-Javadoc)
 * @see android.widget.BaseAdapter#getItemViewType(int)
 */
public int getItemViewType(int position) {
    return IGNORE_ITEM_VIEW_TYPE;
}

/**
 * 
 */
public void update()
{
    buildingCollection = datasource.getBuildings();
}

/* (non-Javadoc)
 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
 */
public View getView(final int position, View convertView,
        final ViewGroup parent) {
    final Building selectedBuilding = buildingCollection.get(position);
    final String buildingDescription = "Building "
            + selectedBuilding.getId();
    LayoutInflater inflater = context.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.location_child_item, null);
    }

    TextView buildingDescriptionView = (TextView) convertView
            .findViewById(R.id.locationDescription);
    buildingDescriptionView.setText(buildingDescription);

    convertView.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (!selectedBuilding.selected)
            {
                for (Building building : buildingCollection) 
                {
                    if (building.getId() != selectedBuilding.getId())
                    {
                        building.setSelected(false);
                        datasource.setColumnTrue(SQLiteHelper.TABLE_BUILDING,
                                false, building.getId(),
                                SQLiteHelper.COLUMN_BUILDING_ID);
                    }
                }
                for (int i = 0; i < parent.getChildCount(); i++)
                {
                    parent.getChildAt(i).setBackgroundResource(
                            R.color.white);
                }
                selectedBuilding.setSelected(true);
                datasource.setColumnTrue(SQLiteHelper.TABLE_BUILDING, true,
                        selectedBuilding.getId(),
                        SQLiteHelper.COLUMN_BUILDING_ID);
                v.setBackgroundResource(R.color.green);

            } 
            else 
            {
                selectedBuilding.setSelected(false);
                datasource.setColumnTrue(SQLiteHelper.TABLE_BUILDING, false,
                        selectedBuilding.getId(),
                        SQLiteHelper.COLUMN_BUILDING_ID);
                v.setBackgroundResource(R.color.white);
            }
            if (floorAdapter != null) {
                floorAdapter.filter();
                floorAdapter.notifyDataSetChanged();
                floorAdapter.notifyDataSetInvalidated();
            }
        }
    });

    return convertView;
}

/* (non-Javadoc)
 * @see android.widget.BaseAdapter#getViewTypeCount()
 */
public int getViewTypeCount() {
    return 1;
}

/* (non-Javadoc)
 * @see android.widget.BaseAdapter#isEnabled(int)
 */
public boolean isEnabled(int position) {
    return false;
}

}

这是我的一个适配器,应该足以告诉你它的状态。

这是我的LocationView,每个人都有一个按钮,每个人都有一个添加按钮,在这里你可以添加一个新的建筑物,地板或房间,它只是添加了它可以成为的一部分的下拉列表。

public class LocationView extends LinearLayout {

List<String> groupList;
ArrayList<Building> buildingList;
ArrayList<Floor> floorList;
ArrayList<Location> roomList;
String projectNo;
DataSource datasource;

public LocationView(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.location_view, this);
    datasource = new DataSource(context);
    datasource.open();
}

/**
 * Populates the list view with an overview of the Post Survey Records
 * Currently in the Database
 */
/**
 * @param context
 * @param activity
 * @param projectNo
 */
public void updateListView(Context context, Activity activity,
        String projectNo) {

    loadBuildingChild();
    loadFloorChild();
    loadRoomChild();

    final LocationListAdapter locationListAdapter = new LocationListAdapter(activity,
            floorList);
    final FloorListAdapter floorListAdapter = new FloorListAdapter(
            activity, buildingList, locationListAdapter);

    PinnedListView buildingView = (PinnedListView) findViewById(R.id.buildng);
    buildingView.setHeader("Buildings");
    ListView buildingListView = (ListView) buildingView
            .findViewById(R.id.list);
    buildingListView.setAdapter(new BuildingListAdapter(activity,
            floorListAdapter, buildingList));

    PinnedListView floorView = (PinnedListView) findViewById(R.id.floor);
    floorView.setHeader("Floors");
    final ListView floorListView = (ListView) floorView
            .findViewById(R.id.list);
    floorListView.setAdapter(floorListAdapter);

    PinnedListView roomView = (PinnedListView) findViewById(R.id.room);
    roomView.setHeader("Rooms");
    ListView roomListView = (ListView) roomView.findViewById(R.id.list);
    roomListView.setAdapter(locationListAdapter);

    setAddBuilding(buildingView);
    setAddFloor(floorView, buildingList, floorListAdapter);
    setAddRoom(roomView, floorList, locationListAdapter);


}

/**
 * @param buildingView
 */
private void setAddBuilding(PinnedListView buildingView) {
    ImageView add = (ImageView) buildingView.findViewById(R.id.imagebutton1);
    add.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            final BuildingDialog buildingDialog = new BuildingDialog(
                    getContext());
            buildingDialog.show();

            Button cancelButton = (Button) buildingDialog
                    .findViewById(R.id.cancelButton);
            cancelButton.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    buildingDialog.dismiss();
                }
            });

            Button addButton = (Button) buildingDialog
                    .findViewById(R.id.addButton);
            addButton.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    EditText buildingEdit = (EditText) buildingDialog
                            .findViewById(R.id.buildingEdit);
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(SQLiteHelper.COLUMN_BUILDING_NAME,
                            buildingEdit.getText().toString());
                    contentValues.put(SQLiteHelper.COLUMN_SITE_ID, 1);
                    datasource.commitRecord(SQLiteHelper.TABLE_BUILDING,
                            contentValues);
                    buildingDialog.dismiss();
                    loadBuildingChild();
                }
            });
        }
    });
}

/**
 * @param buildings
 */
private void loadBuildingChild() {
    buildingList = datasource.getBuildings();
}

/**
 * 
 */
private void loadFloorChild() {
    floorList = new ArrayList<Floor>();
    floorList = datasource.getFloors();
}

/**
 * 
 */
private void loadRoomChild() {
    roomList = new ArrayList<Location>();

    roomList = datasource.getLocations();

}

/**
 * @param floorView
 * @param buildingCollection
 * @param adapter
 */
private void setAddFloor(PinnedListView floorView, final ArrayList<Building> buildingCollection, final FloorListAdapter adapter ) {
    ImageView add = (ImageView) floorView.findViewById(R.id.imagebutton1);
    add.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            final FloorDialog floorDialog = new FloorDialog(getContext());
            floorDialog.show();

            final Spinner buildingSpinner = (Spinner) floorDialog
                    .findViewById(R.id.buildingSpinner);
            ArrayList<String> buildingTexts = new ArrayList<String>();
            for (Building building : buildingCollection) {
                buildingTexts.add(building.buildingDescription);
            }
            ArrayAdapter buildingAdapter = new ArrayAdapter(v.getContext(),
                    android.R.layout.simple_spinner_item, buildingTexts);
            buildingAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            buildingSpinner.setAdapter(buildingAdapter);

            Button cancelButton = (Button) floorDialog
                    .findViewById(R.id.cancelButton);
            cancelButton.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    floorDialog.dismiss();
                }
            });

            Button addButton = (Button) floorDialog
                    .findViewById(R.id.addButton);
            addButton.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    EditText floorNameEdit = (EditText) floorDialog
                            .findViewById(R.id.floorNameEdit);

                    ContentValues contentValues = new ContentValues();
                    int buildingId = buildingCollection.get(
                            buildingSpinner.getSelectedItemPosition())
                            .getId();
                    contentValues.put(SQLiteHelper.COLUMN_FLOOR_NUMBER,
                            datasource.getMaxFloorNo(buildingId) + 1);
                    contentValues.put(SQLiteHelper.COLUMN_BUILDING_ID,
                            buildingId);
                    contentValues.put(SQLiteHelper.COLUMN_FLOOR_NAME,
                            "Building " + buildingId
                                    + floorNameEdit.getText().toString());
                    datasource.commitRecord(SQLiteHelper.TABLE_FLOOR,
                            contentValues);
                    adapter.filter();
                    floorDialog.dismiss();
                }
            });

        }
    });
}

/**
 * @param roomView
 * @param floorCollection
 * @param adapter
 */
private void setAddRoom(final PinnedListView roomView, final ArrayList<Floor> floorCollection, final LocationListAdapter adapter ) {
    ImageView addRoom = (ImageView) roomView.findViewById(R.id.imagebutton1);
    addRoom.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            final LocationDialog roomDialog = new LocationDialog(getContext());
            final Spinner floorSpinner = (Spinner) roomDialog
                    .findViewById(R.id.floorSpinner);
            ArrayList<String> floorTexts = new ArrayList<String>();
            for (Floor floor : floorCollection) {
                floorTexts.add(floor.getFloorName());
            }
            ArrayAdapter buildingAdapter = new ArrayAdapter(
                    roomView.getContext(), android.R.layout.simple_spinner_item,
                    floorTexts);
            buildingAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            floorSpinner.setAdapter(buildingAdapter);
            for (Floor floor : floorCollection) {
                if (floor.isSelected()) {
                    floorSpinner.setSelection(floorCollection.indexOf(floor));
                }
            }

            roomDialog.show();

            Button cancelButton = (Button) roomDialog
                    .findViewById(R.id.cancelButton);
            cancelButton.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    roomDialog.dismiss();
                }
            });

            Button addButton = (Button) roomDialog
                    .findViewById(R.id.addButton);
            addButton.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {

                    EditText roomNameEdit = (EditText) roomDialog
                            .findViewById(R.id.roomNameEdit);

                    ContentValues contentValues = new ContentValues();
                    Floor floor = floorCollection.get(floorSpinner
                            .getSelectedItemPosition());
                    contentValues
                            .put(SQLiteHelper.COLUMN_LOCATION_NUMBER,
                                    datasource.getMaxFloorNo(floor
                                            .getFloorId()) + 1);
                    contentValues.put(SQLiteHelper.COLUMN_FLOOR_ID,
                            floor.getFloorId());
                    contentValues.put(
                            SQLiteHelper.COLUMN_LOCATION_DESCRIPTION,
                            "Building " + floor.getBuildingID()
                                    + floor.getFloorName() + " "
                                    + roomNameEdit.getText().toString());
                    datasource.commitRecord(SQLiteHelper.TABLE_LOCATION,
                            contentValues);
                    // roomCollection.add(datasource.getLatestLocation(floor.getFloorId()));
                    adapter.filter();
                    roomDialog.dismiss();
                }
            });

        }
    });

}
}

如果您想了解有关代码或我的意图的更多信息,请直接询问。 任何,所有的批评都是受欢迎的,我真的很想清理一下。

我必须对另一部分使用类似的方法,但它会更复杂,所以我不想继续前进,除非我能在这部分做得好。

非常感谢!

编辑:我想要的主要内容是基于父结果进行过滤的好方法。布局虽然有缺陷,但在我的实现中唯一不能很好用的东西并不重要。

编辑2:根据请求,r.layout.location_view.xml,虽然它并不多。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="500dp"
android:orientation="vertical"
android:id="@+id/locationView"
>
<com.ohs.mobile.app.PinnedListView 
    android:layout_height="0dp"
    android:layout_width="fill_parent"
    android:id="@+id/buildng"
    android:layout_weight="1"
    />


<com.ohs.mobile.app.PinnedListView 
    android:layout_height="0dp"
    android:layout_width="fill_parent"
    android:id="@+id/floor"
    android:layout_weight="1"
    android:clickable="true"
/>

<com.ohs.mobile.app.PinnedListView 
    android:layout_height="0dp"
    android:layout_width="fill_parent"
    android:id="@+id/room"
    android:layout_weight="2"
    />

</LinearLayout>

0 个答案:

没有答案