过滤器返回空列表视图

时间:2016-06-21 05:31:40

标签: android listview

您好我正在尝试搜索/过滤应用,其中数据来自mySql数据库以填充列表视图。除了过滤部分之外,listview的填充工作完全正常。它不会过滤列表视图,它是空的。请帮助。

主要活动:

public class MainActivity extends Activity {
    public static final String url = "";
    // Declare Variables
    ListView list;
    ListViewAdapter adapter;
    EditText editsearch;
    String[] rank;
    String[] country;
    String[] population;
    ArrayList<WorldPopulation> arraylist = new ArrayList<WorldPopulation>();

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

        //Generate list View from ArrayList
        displayListView();
        // Locate the ListView in listview_main.xml
        list = (ListView) findViewById(R.id.listview);

        // Pass results to ListViewAdapter Class
        adapter = new ListViewAdapter(this, arraylist);

        // Binds the Adapter to the ListView
        list.setAdapter(adapter);

        // Locate the EditText in listview_main.xml
        editsearch = (EditText) findViewById(R.id.search);

        // Capture Text in EditText
        editsearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
                adapter.filter(text);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
                // TODO Auto-generated method stub
            }
        });
    }

    private void displayListView() {
        // Creating volley request obj
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>(){
                    @Override
                    public void onResponse(String response) {
                        parseData(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        //Creating request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        //Adding request to the queue
        requestQueue.add(stringRequest);
    }

    private void parseData(String json) {
        JSONArray items = null;
        JSONObject jsonObject=null;
        try {
            jsonObject = new JSONObject(json);
            items = jsonObject.getJSONArray("result");
            rank = new String[items.length()];
            country = new String[items.length()];
            population = new String[items.length()];
            for (int i = 0; i < items.length(); i++) {
                JSONObject jo = items.getJSONObject(i);
                rank[i] = jo.getString("rank");
                country[i] = jo.getString("country");
                population[i] = jo.getString("population");

                WorldPopulation wp = new WorldPopulation(rank[i], country[i],
                        population[i]);
                // Binds all strings into an array
                arraylist.add(wp);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

BASE适配器:

public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<WorldPopulation> worldpopulationlist = null;
private ArrayList<WorldPopulation> arraylist;

public ListViewAdapter(Context context, List<WorldPopulation> worldpopulationlist) {
    mContext = context;
    this.worldpopulationlist = worldpopulationlist;
    inflater = LayoutInflater.from(mContext);
    this.arraylist = new ArrayList<WorldPopulation>();
    this.arraylist.addAll(worldpopulationlist);
}

public class ViewHolder {
    TextView rank;
    TextView country;
    TextView population;
}

@Override
public int getCount() {
    return worldpopulationlist.size();
}

@Override
public WorldPopulation getItem(int position) {
    return worldpopulationlist.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.listview_item, null);
        // Locate the TextViews in listview_item.xml
        holder.rank = (TextView) view.findViewById(R.id.rank);
        holder.country = (TextView) view.findViewById(R.id.country);
        holder.population = (TextView) view.findViewById(R.id.population);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    // Set the results into TextViews
    holder.rank.setText(worldpopulationlist.get(position).getRank());
    holder.country.setText(worldpopulationlist.get(position).getCountry());
    holder.population.setText(worldpopulationlist.get(position).getPopulation());

    // Listen for ListView Item Click
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Send single item click data to SingleItemView Class
            Intent intent = new Intent(mContext, SingleItemView.class);
            // Pass all data rank
            intent.putExtra("rank",(worldpopulationlist.get(position).getRank()));
            // Pass all data country
            intent.putExtra("country",(worldpopulationlist.get(position).getCountry()));
            // Pass all data population
            intent.putExtra("population",(worldpopulationlist.get(position).getPopulation()));
            // Pass all data flag
            // Start SingleItemView Class
            mContext.startActivity(intent);
        }
    });

    return view;
}

// Filter Class
public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    worldpopulationlist.clear();
    if (charText.length() == 0) {
        worldpopulationlist.addAll(arraylist);
    }
    else
    {
        for (WorldPopulation wp : arraylist)
        {
            if (wp.getCountry().toLowerCase(Locale.getDefault()).contains(charText))
            {
                worldpopulationlist.add(wp);
            }
        }
    }
    notifyDataSetChanged();
}

模特:

public class WorldPopulation {
    private String rank;
    private String country;
    private String population;

    public WorldPopulation(String rank, String country, String population) {
        this.rank = rank;
        this.country = country;
        this.population = population;
    }

    public String getRank() {
        return this.rank;
    }

    public String getCountry() {
        return this.country;
    }

    public String getPopulation() {
        return this.population;
    }
}

0 个答案:

没有答案
相关问题