简体   繁体   English

选择微调器值后过滤listview结果

[英]filtering listview results after selecting spinner value

I have 3 spinners in my project named standard, division and age. 我的项目中有3个spinners ,分别是“标准”,“部门”和“年龄”。 They contain values from json data. 它们包含来自json数据的值。 When I select a value from each individual spinner it shows all data of students in a listview respectivly (fetched from json). 当我从每个spinner选择一个值时,它会分别在listview显示学生的所有数据(从json获取)。

I'm working with the following JSON data: 我正在使用以下JSON数据:

[
{
"name":"aarti",
"surname":"singh",
"age":"18",
"div":"A",
"standard":"7"
},
{
"name":"seema",
"surname":"desai",
"age":"17",
"div":"B",
"standard":"7"
},
{
"name":"tina",
"surname":"joshi",
"age":"18",
"div":"A",
"standard":"8"
},
{
"name":"megha",
"surname":"kale",
"age":"17",
"div":"A",
"standard":"7"
},
{
"name":"swati",
"surname":"marathe",
"age":"18",
"div":"A",
"standard":"8"
},
{
"name":"rekha",
"surname":"surve",
"age":"17",
"div":"A",
"standard":"7"
},
{
"name":"madhu",
"surname":"dalvi",
"age":"18",
"div":"B",
"standard":"6"
}

I am trying to do it like this way: When I select standard 7 from the spinner it should display students information studying in 7th standard ("aarti", "seema", "megha", "rekha") and their other information too. 我正在尝试这样操作:当我从微调器中选择标准7时,它应该显示以7标准学习的学生信息(“ aarti”,“ seema”,“ megha”,“ rekha”)以及其他信息。 Then I am selecting value from division spinner say "A", it should take above result granted and accordingly display students from standard 7 and division "A". 然后我从分组微调器说“ A”中选择值,它应该超过授予的结果,并相应地显示标准7和“ A”类的学生。 It should shows the result as ("aarti", "megha", "rekha") and their other informations too. 它也应将结果显示为(“ aarti”,“ megha”,“ rekha”)及其其他信息。 Finally when I am selecting value from age spinner say "17", it should take above result granted and accordingly display students from standard 7 and division "A" and age "17". 最后,当我从年龄微调器中选择值时,说“ 17”,它应该超过授予的结果,并相应地显示标准7和“ A”和“ 17”年龄的学生。 Accordingly it shows "megha" and "rekha". 因此,它显示“ megha”和“ rekha”。

Can anyone please assist me 谁能帮我

This is my MainActivity.java class: 这是我的MainActivity.java类:

public class MainActivity extends AppCompatActivity {

ArrayList<String> AllStandards = new ArrayList<>();
ArrayList<String> AllDivision = new ArrayList<>();
ArrayList<String> AllAge = new ArrayList<>();
JSONArray jsonArray;
Spinner spinner_std, spinner_div, spinner_age;
private ArrayList<StudInfo> studentVOList = new ArrayList<>();

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

final List<String> item_Std = getStandard("data.json");
final List<String> items_div = getDivision("data.json");
final List<String> items_age = getAge("data.json");

/*spinner for standard*/
spinner_std = (Spinner) findViewById(R.id.spinnerStandard);
ArrayAdapter<String> adapter_std = new ArrayAdapter<String>(this,     R.layout.spinner_standard_layout, R.id.textViewStandard, item_Std);

adapter_std.getFilter().filter(txt);

spinner_std.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    try {

        String standard = AllStandards.get(i);

        if (studentVOList.size() > 0)
            studentVOList.clear();
        for (int j = 0; j < jsonArray.length(); j++) {
            JSONObject jsonObject = jsonArray.getJSONObject(j);
            String stand = jsonObject.getString("standard");
            if (stand.equalsIgnoreCase(standard)) {
                StudInfo studentVO = new StudInfo();

                studentVO.setName(jsonObject.getString("name"));
                studentVO.setSurname(jsonObject.getString("surname"));
                studentVO.setAge(jsonObject.getString("age"));
                studentVO.setDiv(jsonObject.getString("div"));
                studentVO.setStandard(jsonObject.getString("standard"));

                studentVO.setStandard(stand);
                studentVOList.add(studentVO);
            }
        }
        // Log.d("TAG", "List With All Students in selected standard: " + studentVOList.size());

        Intent intent = new Intent(getApplicationContext(), StudentsInfo.class);
        Bundle b = new Bundle();
        b.putSerializable("list", studentVOList);
        intent.putExtra("bundle", b);
        startActivity(intent);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public void onNothingSelected(AdapterView<?> arg0) {
}
});
spinner_std.setAdapter(adapter_std);

/*spinner for division*/
spinner_div = (Spinner) findViewById(R.id.spinnerDiv);
ArrayAdapter<String> adapter_div = new ArrayAdapter<String>(this,  R.layout.spinner_division_layout, R.id.textViewDivision, items_div);
adapter_div.getFilter().filter(txt);

spinner_div.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    try {

        String division = AllDivision.get(i);

        if (studentVOList.size() > 0)
            studentVOList.clear();
        for (int j = 0; j < jsonArray.length(); j++) {
            JSONObject jsonObject = jsonArray.getJSONObject(j);
            String div = jsonObject.getString("div");
            // Collections.sort(AllDivision);

            if (div.equalsIgnoreCase(division)) {
                StudInfo studentVO = new StudInfo();

                studentVO.setName(jsonObject.getString("name"));
                studentVO.setSurname(jsonObject.getString("surname"));
                studentVO.setAge(jsonObject.getString("age"));
                studentVO.setStandard(jsonObject.getString("standard"));
                studentVO.setDiv(jsonObject.getString("div"));

                studentVO.setDiv(div);
                studentVOList.add(studentVO);
            }
        }
        Intent intent = new Intent(getApplicationContext(), StudentsInfo.class);
        Bundle b = new Bundle();
        b.putSerializable("list", studentVOList);
        intent.putExtra("bundle", b);

        startActivity(intent);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public void onNothingSelected(AdapterView<?> arg0) {
}
});
spinner_div.setAdapter(adapter_div);

/*spinner for age*/
spinner_age = (Spinner) findViewById(R.id.spinerAge);
ArrayAdapter<String> adapter_age = new ArrayAdapter<String>(this,  R.layout.spinner_age_layout, R.id.textViewAge, items_age);
adapter_age.getFilter().filter(txt);

spinner_age.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    try {

        String age = AllAge.get(i);

        if (studentVOList.size() > 0)
            studentVOList.clear();
        for (int j = 0; j < jsonArray.length(); j++) {
            JSONObject jsonObject = jsonArray.getJSONObject(j);
            String age_list = jsonObject.getString("age");
            Collections.sort(AllAge);

            if (age_list.equalsIgnoreCase(age)) {
                StudInfo studentVO = new StudInfo();

                studentVO.setName(jsonObject.getString("name"));
                studentVO.setSurname(jsonObject.getString("surname"));
                studentVO.setDiv(jsonObject.getString("div"));
                studentVO.setStandard(jsonObject.getString("standard"));
                studentVO.setAge(jsonObject.getString("age"));

                studentVO.setAge(age_list);
                studentVOList.add(studentVO);
            }
        }
        Intent intent = new Intent(getApplicationContext(),  StudentsInfo.class);
        Bundle b = new Bundle();
        b.putSerializable("list", studentVOList);
        intent.putExtra("bundle", b);

        startActivity(intent);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public void onNothingSelected(AdapterView<?> arg0) {
}
});
spinner_age.setAdapter(adapter_age);

}//onCreate Method

private List<String> getStandard(String fileName) {
jsonArray = null;
try {
InputStream is = getResources().getAssets().open(fileName);
int size = is.available();
byte[] data = new byte[size];
is.read(data);
is.close();
String json = new String(data, "UTF-8");

// AllStandards.clear();
try {
    jsonArray = new JSONArray(json);

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        String stand = jsonObject.getString("standard");

        if (!AllStandards.contains(stand)) {
            AllStandards.add(stand);
        }
    }
} catch (JSONException je) {
    je.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return AllStandards;
}

private List<String> getDivision(String fileName) {
jsonArray = null;
try {
InputStream is = getResources().getAssets().open(fileName);
int size = is.available();
byte[] data = new byte[size];
is.read(data);
is.close();
String json = new String(data, "UTF-8");
try {
    jsonArray = new JSONArray(json);

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        String stand = jsonObject.getString("div");
        if (!AllDivision.contains(stand)) {
            AllDivision.add(stand);
        }
    }
} catch (JSONException je) {
    je.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return AllDivision;
}

private List<String> getAge(String fileName) {
jsonArray = null;
try {
InputStream is = getResources().getAssets().open(fileName);
int size = is.available();
byte[] data = new byte[size];
is.read(data);
is.close();
String json = new String(data, "UTF-8");
try {
    jsonArray = new JSONArray(json);

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        String stand = jsonObject.getString("age");
        if (!AllAge.contains(stand)) {
            AllAge.add(stand);
        }
    }
} catch (JSONException je) {
    je.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return AllAge;
}

this is my ListAdapter.java class 这是我的ListAdapter.java类

public class ListAdapter extends ArrayAdapter<StudInfo> {
int vg;
ArrayList<StudInfo> list;
Context context;

public ListAdapter(Context context, int vg, int id, ArrayList<StudInfo> list) {
super(context, vg, id, list);
this.context = context;
this.vg = vg;
this.list = list;
}

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater)     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View itemView = inflater.inflate(vg, parent, false);

TextView textViewName = (TextView) itemView.findViewById(R.id.txtName);
TextView textViewSurname = (TextView)itemView.findViewById(R.id.txtSurname);
TextView textViewAge = (TextView) itemView.findViewById(R.id.txtAge);
TextView textViewDiv = (TextView) itemView.findViewById(R.id.txtDiv);
TextView textViewStd = (TextView) itemView.findViewById(R.id.txtStandard);

textViewName.setText(list.get(position).getName());
textViewSurname.setText(list.get(position).getSurname());
textViewAge.setText(list.get(position).getAge());
textViewDiv.setText(list.get(position).getDiv());
textViewStd.setText(list.get(position).getStandard());

return itemView;
}
}

this is my StudentsInfo.java class 这是我的StudentsInfo.java类

public class StudentsInfo extends Activity {

ListView listView;

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

Bundle b = getIntent().getBundleExtra("bundle");

ArrayList<StudInfo> studentVOList = (ArrayList<StudInfo>) b.getSerializable("list");

ListView listView = (ListView) findViewById(R.id.listViewShow);
ListAdapter listAdapter = new ListAdapter(this, R.layout.list_layout,    R.id.txtName, studentVOList);
listView.setAdapter(listAdapter);
}
}

this is my StudInfo.java class, 这是我的StudInfo.java类,

import java.io.Serializable;

public class StudInfo implements Serializable
{
private String name;
private String surname;
private String age;
private String div;
private String standard;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public String getAge() {
    return age;
}

public void setAge(String age) {
    this.age = age;
}

public String getDiv() {
    return div;
}

public void setDiv(String div) {
    this.div = div;
}

public String getStandard() {
    return standard;
}

public void setStandard(String standard) {
    this.standard = standard;
}
}

i tried googling to find out solution but the questions i found all are based on searching listview items using edittext.i didn't found ant similar post matching with my question which solve my problem.can anyone please help me to solve this?? 我试图谷歌搜索找到解决方案,但我发现的所有问题都是基于使用edittext搜索listview项目。我没有找到与我的问题相匹配的类似帖子来解决我的问题。有人可以帮我解决这个问题吗?

1. Override getFilter method in your adapter class 1.在适配器类中重写getFilter方法

2. Use adapter.getFilter().filter(txt) in your spinner selection 2.在您的微调器选择中使用adapter.getFilter()。filter(txt)

 package com.hughesnet.adapters;

 import com.hughesnet.R;

 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Filter;
 import android.widget.TextView;

 import java.util.ArrayList;

 public class ListAdapter extends ArrayAdapter<StudInfo> {
int                 vg;
ArrayList<StudInfo> list;
Context             context;
ItemFilter mFilter;
ArrayList<StudInfo> filteredData;

public ListAdapter (Context context, int vg, int id, ArrayList<StudInfo> list) {

    super (context, vg, id, list);
    this.context = context;
    this.vg = vg;
    this.list = list;
}

public View getView (int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate (vg, parent, false);

    TextView textViewName = (TextView) itemView.findViewById (R.id.txtName);
    TextView textViewSurname = (TextView) itemView.findViewById (R.id.txtSurname);
    TextView textViewAge = (TextView) itemView.findViewById (R.id.txtAge);
    TextView textViewDiv = (TextView) itemView.findViewById (R.id.txtDiv);
    TextView textViewStd = (TextView) itemView.findViewById (R.id.txtStandard);

    textViewName.setText (list.get (position).getName ());
    textViewSurname.setText (list.get (position).getSurname ());
    textViewAge.setText (list.get (position).getAge ());
    textViewDiv.setText (list.get (position).getDiv ());
    textViewStd.setText (list.get (position).getStandard ());

    return itemView;
}

ItemFilter mFilter = new ItemFilter ();

public Filter getFilter () {

    return mFilter;
}

private class ItemFilter extends Filter {
    @Override
    protected FilterResults performFiltering (CharSequence constraint) {

        String filterString = constraint.toString ().toLowerCase ();

        FilterResults results = new FilterResults ();

        int count = list.size ();
        final ArrayList<StudInfo> nlist = new ArrayList<StudInfo> (count);

        String filterableString;

        for (int i = 0; i < count; i++) {
            StudInfo info = list.get (i);
            // Check for standard, division and age here
            if (info.getAge().contains(filterString) || info.getStandard().contains(filterString ) || info.getDiv().contains(filterString )) {
                nlist.add (filterableString);
            }
        }

        results.values = nlist;
        results.count = nlist.size ();

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        filteredData = (ArrayList<StudInfo>) results.values;
        notifyDataSetChanged();
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM