过滤列表视图而不编辑适配器类

时间:2015-12-05 11:19:24

标签: android listview android-listview adapter android-volley

我有由Volley填充的listview,我想在不编辑适配器类的情况下添加过滤器,我之前做过这个,但是其他自定义适配器没有像这样填充JSON文件中的数据,

Telephones.class

public class Telephones extends AppCompatActivity {


RequestQueue requestQueue;
private List<tel_list> data = new ArrayList<tel_list>();
private ListView listView;
private TelAdapter adapter;
private EditText telfilter;
private ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_telephones);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setCustomView(R.layout.actionbar);

    listView = (ListView) findViewById(R.id.listview1);
    adapter = new TelAdapter(this, data);
    listView.setAdapter(adapter);
    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Please wait...");
    progressDialog.show();
    telfilter = (EditText) findViewById(R.id.myFilter);
    telfilter.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {


            }

    });

    requestQueue = Volley.newRequestQueue(this);

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://10.0.2.2/abb/getphones.php",
            new com.android.volley.Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    hideprogressDialog();

                    try {

                        JSONArray jsonArray = response.getJSONArray("telephones");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject telephones = jsonArray.getJSONObject(i);
                            tel_list tellist = new tel_list();

                            tellist.setName(telephones.getString("name"));
                            tellist.setNumber(telephones.getString("number"));

                            data.add(tellist);

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    adapter.notifyDataSetChanged();
                }
            },

            new com.android.volley.Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    hideprogressDialog();

                    Log.e("Volley", "Error");
                }
            }


    );

    requestQueue.add(jsonObjectRequest);
}

@Override
public void onDestroy() {
    super.onDestroy();
    hideprogressDialog();

}

private void hideprogressDialog() {

    if (progressDialog != null) {
        progressDialog.dismiss();
        progressDialog = null;
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}


public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.about:

            Intent intent = new Intent(this, about.class);
            startActivity(intent);
            return true;

    }
    return super.onOptionsItemSelected(item);
}
}

TelAdapter.class

public class TelAdapter extends ArrayAdapter<tel_list> {

private Context context;
private List<tel_list> telephones;


public TelAdapter(Context context, List<tel_list> data) {
    super(context, R.layout.activity_item, data);
    telephones = data;

}



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

    LayoutInflater inflater = LayoutInflater.from(getContext());

    View item = inflater.inflate(R.layout.activity_item, null);

    TextView name = (TextView) item.findViewById(R.id.names);
    name.setText(telephones.get(position).getName());
    TextView number = (TextView) item.findViewById(R.id.numbers);
    number.setText(telephones.get(position).getNumber());

    return item;


}
}

0 个答案:

没有答案