假设我必须上课:
区和分区的级别
public class District {
private int districtId;
private String districtName;
public District(){
}
public int getDistrictId() {
return districtId;
}
public String getDistrictName() {
return districtName;
}
public void setDistrictId(int districtId) {
this.districtId = districtId;
}
public void setDistrictName(String districtName) {
this.districtName = districtName;
}
@Override
public String toString() {
return this.districtName;
}
}
和SubDistric类
public class SubDistrict extends District{
private String SubDistrictName;
private int SubDistrictId;
public SubDistrict(){
}
public String getSubDistrictName() {
return SubDistrictName;
}
public void setSubDistrictName(String subDistrictName) {
SubDistrictName = subDistrictName;
}
public int getSubDistrictId() {
return SubDistrictId;
}
public void setSubDistrictId(int subDistrictId) {
SubDistrictId = subDistrictId;
}
@Override
public String toString() {
return this.SubDistrictName;
}
}
我在activity_main.xml中有两个微调器
<Spinner
android:id="@+id/district"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/sub_district"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dynamic_spinner"
/>
当我选择地区时,我想过滤该地区下的子地区并在sub_district微调框中显示
我已经创建了区域适配器
public class DistrictAdapter extends BaseAdapter {
private Context context;
private ArrayList<District> districts;
private ArrayList<District> originalList;
public DistrictAdapter(Context context, ArrayList<District> districts) {
this.context = context;
this.districts = new ArrayList<District>();
this.districts.addAll(districts);
this.originalList = new ArrayList<District>();
this.originalList.addAll(districts);
}
public ArrayList<District> getOriginalList() {
return originalList;
}
public void setOriginalList(ArrayList<District> originalList) {
this.originalList = originalList;
}
@Override
public int getCount() {
return districts.size();
}
@Override
public Object getItem(int position) {
return districts.get(position);
}
@Override
public long getItemId(int position) {
District district=districts.get(position);
return district.getDistrictId();
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
District district=(District) getItem(i);
if(view==null){
LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=layoutInflater.inflate(R.layout.district_layout,null);
}
TextView parentText=(TextView)view.findViewById(R.id.hederTextViewId);
parentText.setText(district.getDistrictName().trim());
return view;
}
public void filterData(String query){
query=query.toLowerCase();
districts.clear();
if(query.isEmpty()){
districts.addAll(originalList);
}
else{
for(District district : originalList){
if(district.getDistrictName().toLowerCase().contains(query)){
districts.add(district);
}
}
}
notifyDataSetChanged();
}
}
并且子区域适配器是
public class SubDistrictAdapter extends BaseAdapter {
private Context context;
private ArrayList<SubDistrict> subDistricts;
private ArrayList<SubDistrict> originalList;
public SubDistrictAdapter(Context context, ArrayList<SubDistrict> subDistricts) {
this.context = context;
this.subDistricts = new ArrayList<SubDistrict>();
this.subDistricts.addAll(subDistricts);
this.originalList = new ArrayList<SubDistrict>();
this.originalList.addAll(subDistricts);
}
public ArrayList<SubDistrict> getOriginalList() {
return originalList;
}
public void setOriginalList(ArrayList<SubDistrict> originalList) {
this.originalList = originalList;
}
@Override
public int getCount() {
return subDistricts.size();
}
@Override
public Object getItem(int position) {
return subDistricts.get(position);
}
@Override
public long getItemId(int position) {
SubDistrict sub=subDistricts.get(position);
return sub.getSubDistrictId();
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
SubDistrict sub=(SubDistrict) getItem(i);
if(view==null){
LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=layoutInflater.inflate(R.layout.district_layout,null);
}
TextView parentText=(TextView)view.findViewById(R.id.hederTextViewId);
parentText.setText(sub.getSubDistrictName().trim());
return view;
}
public void filterData(String query){
query=query.toLowerCase();
subDistricts.clear();
if(query.isEmpty()){
subDistricts.addAll(originalList);
}
else{
for(SubDistrict sub : originalList){
if(sub.getSubDistrictName().toLowerCase().contains(query)){
subDistricts.add(sub);
}
}
}
notifyDataSetChanged();
}
}
在Main Activity.java
中 ArrayList<District> districts= new ArrayList<District>();
ArrayList<SubDistrict> subDistricts=new ArrayList<SubDistrict>();
Spinner district_spinner= (Spinner) findViewById(R.id.district);
Spinner subDistrictSpinner=(Spinner)findViewById(R.id.sub_district);
DistrictAdapter district_adapter=new DistrictAdapter(MainActivity.this,districts);
district_spinner.setAdapter(district_adapter);
SubDistrictAdapter subDistrictAdapter=new SubDistrictAdapter(MainActivity.this,subDistricts);
subDistrictSpinner.setAdapter(subDistrictAdapter);
1。如何获得所选地区下的分区?
2。如何在两个微调器中都实现搜索?