1

我尝试使用我的基本适配器从城市列表中添加搜索列表,但它不起作用。我想在城市列表中搜索城市。这是我的代码。

我的城市搜索:

private ProgressDialog pDialog;

EditText inputSearch;

// Creating JSON Parser instance
JSONParser jParser = new JSONParser();

// Hashmap for ListView
ArrayList<HashMap<String, String>> CitiesLI = new ArrayList<HashMap<String, String>>();

// url to make request
private static String url_cityli = "http://10.0.2.2/Myweb/ecities.php";


// JSON Keys
public static final String TAG_CITEMS_LI = "cities_li";
public static final String TAG_CID_LI = "city_id";
public static final String TAG_CNAME_LI = "city_name";
public static final String TAG_CIMG_LI = "image";


JSONArray cities_li = null;

ListView list;
CitySearchAdapter adapter;
private CitySearch activity;




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




    CitiesLI = new ArrayList<HashMap<String, String>>();

    new Activity().execute();

    activity = this;
    list = (ListView) findViewById(R.id.city_list);



    //list click to details view of the place
    list.setOnItemClickListener(new OnItemClickListener() {

        //@Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // getting values from selected ListItem

            String cid_li = ((TextView) view.findViewById(R.id.cid_li)).getText().toString();


            Intent i = new Intent(getApplicationContext(),
                //Tab.class);
            CityInfoActivity.class);


            // Starting new intent
            i.putExtra(TAG_CID_LI, cid_li);

            startActivity(i);

            //startActivityForResult(i, 100);

        }
    });



}


public void SetListViewAdapter(ArrayList<HashMap<String, String>> daftar) {
    adapter = new CitySearchAdapter(activity, daftar);
    list.setAdapter(adapter);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == 100) {

        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

}

class Activity extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CitySearch.this);
        pDialog.setMessage("Please Wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... args) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        JSONArray json = jParser.makeHttpRequest(url_cityli, "GET",
                params);


        Log.d("All Products: ", json.toString());


        // looping through All data
        try {

            cities_li = json;

                for (int i = 0; i < cities_li.length(); i++) {
                    JSONObject c = cities_li.getJSONObject(i);

                    // Storing each json item in variable
                    String city_id = c.getString(TAG_CID_LI);                       
                    String city_name =c.getString(TAG_CNAME_LI);
                    String image = c.getString(TAG_CIMG_LI);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    //JSON Object
                    map.put(TAG_CID_LI, city_id);                       
                    map.put(TAG_CNAME_LI,city_name);
                    map.put(TAG_CIMG_LI, image);

                    // adding HashList to ArrayList
                    CitiesLI.add(map);
                }

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

        return null;
    }

    protected void onPostExecute(String file_url) {

        pDialog.dismiss();

        runOnUiThread(new Runnable() {
            public void run() {

                SetListViewAdapter(CitiesLI);

                //
                // Enabling Search Filter



                    CitySearchAdapter adapter;
                    inputSearch = (EditText) findViewById(R.id.inputSearch);    
                    inputSearch.addTextChangedListener(new TextWatcher() {

                            @Override
                            public void onTextChanged(CharSequence s, int start, int before, int count) {
                                // Listview name of the class
                                CitySearch.this.adapter.getFilter().filter(s);
                            }

                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count,
                                    int after) {
                                // TODO Auto-generated method stub

                            }

                            @Override
                            public void afterTextChanged(Editable s) {
                                // TODO Auto-generated method stub

                            }
                        });
                    }
        });

    }


}
}

这是我的 CitylistAdapter :

public class CitySearchAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;

public ImageLoader imageLoader;

public CitySearchAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.te, null);


    TextView city_id = (TextView) vi.findViewById(R.id.cid_li);
    TextView image = (TextView) vi.findViewById(R.id.cimg_li);
    TextView city_name = (TextView) vi.findViewById(R.id.cname); 

    ImageView thumb_image = (ImageView) vi.findViewById(R.id.cimage); 

    HashMap<String, String> city_li = new HashMap<String, String>();
    city_li = data.get(position);

    city_id.setText(city_li.get(CityActivity.TAG_CID_LI));
    image.setText(city_li.get(CityActivity.TAG_CIMG_LI));
    city_name.setText(city_li.get(CityActivity.TAG_CNAME_LI));

    imageLoader.DisplayImage(city_li.get(CityActivity.TAG_CIMG_LI),thumb_image);

    return vi;
}

public Object getFilter() {
    // TODO Auto-generated method stub
    return null;
}


}

请帮帮我。谢谢大家

4

0 回答 0