试图在我的应用程序中添加搜索功能

时间:2015-08-15 14:38:29

标签: android search

嗨,伙计们!首先,我是新手!我试图在我的应用程序中创建搜索功能(Search BY CITY)。但有些事情是错的!它不起作用!

错误消息是:'方法过滤器(String)未定义类型BinderData'

                adapter.filter(text);

有人可以帮我详细说明吗?我收录了" TextWatcher"功能在我的主要活动(WeatherActivity)中。

这是我的代码:

WeatherActivity

public class WeatherActivity extends Activity {

// XML node keys
static final String KEY_TAG = "weatherdata"; // parent node
static final String KEY_ID = "id";
static final String KEY_CITY = "city";
static final String KEY_TEMP_C = "tempc";
static final String KEY_TEMP_F = "tempf";
static final String KEY_CONDN = "condition";
static final String KEY_SPEED = "windspeed";
static final String KEY_ICON = "icon";
EditText editsearch;
// List items 
ListView list;
BinderData adapter = null;
List<HashMap<String,String>> weatherDataCollection;

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

    try {


        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse (getAssets().open("weatherdata.xml"));










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

        // normalize text representation
        doc.getDocumentElement ().normalize ();

        NodeList weatherList = doc.getElementsByTagName("weatherdata");

        HashMap<String,String> map = null;

        for (int i = 0; i < weatherList.getLength(); i++) {

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

               Node firstWeatherNode = weatherList.item(i);

                if(firstWeatherNode.getNodeType() == Node.ELEMENT_NODE){

                    Element firstWeatherElement = (Element)firstWeatherNode;
                    //-------
                    NodeList idList = firstWeatherElement.getElementsByTagName(KEY_ID);
                    Element firstIdElement = (Element)idList.item(0);
                    NodeList textIdList = firstIdElement.getChildNodes();
                    //--id
                    map.put(KEY_ID, ((Node)textIdList.item(0)).getNodeValue().trim());

                    //2.-------
                    NodeList cityList = firstWeatherElement.getElementsByTagName(KEY_CITY);
                    Element firstCityElement = (Element)cityList.item(0);
                    NodeList textCityList = firstCityElement.getChildNodes();
                    //--city
                    map.put(KEY_CITY, ((Node)textCityList.item(0)).getNodeValue().trim());

                    //3.-------
                    NodeList tempList = firstWeatherElement.getElementsByTagName(KEY_TEMP_C);
                    Element firstTempElement = (Element)tempList.item(0);
                    NodeList textTempList = firstTempElement.getChildNodes();
                    //--city
                    map.put(KEY_TEMP_C, ((Node)textTempList.item(0)).getNodeValue().trim());

                    //4.-------
                    NodeList condList = firstWeatherElement.getElementsByTagName(KEY_CONDN);
                    Element firstCondElement = (Element)condList.item(0);
                    NodeList textCondList = firstCondElement.getChildNodes();
                    //--city
                    map.put(KEY_CONDN, ((Node)textCondList.item(0)).getNodeValue().trim());

                    //5.-------
                    NodeList speedList = firstWeatherElement.getElementsByTagName(KEY_SPEED);
                    Element firstSpeedElement = (Element)speedList.item(0);
                    NodeList textSpeedList = firstSpeedElement.getChildNodes();
                    //--city
                    map.put(KEY_SPEED, ((Node)textSpeedList.item(0)).getNodeValue().trim());

                    //6.-------
                    NodeList iconList = firstWeatherElement.getElementsByTagName(KEY_ICON);
                    Element firstIconElement = (Element)iconList.item(0);
                    NodeList textIconList = firstIconElement.getChildNodes();
                    //--city
                    map.put(KEY_ICON, ((Node)textIconList.item(0)).getNodeValue().trim());

                    //Add to the Arraylist
                    weatherDataCollection.add(map);
            }       
        }


        BinderData bindingData = new BinderData(this,weatherDataCollection);






        list = (ListView) findViewById(R.id.list);

        adapter = new BinderData(WeatherActivity.this,
                weatherDataCollection);



   editsearch = (EditText) findViewById(R.id.search);

        editsearch.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub
        }
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub
        }
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            String text = editsearch.getText().toString()
                    .toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }

    });




        Log.i("BEFORE", "<<------------- Before SetAdapter-------------->>");

        list.setAdapter(bindingData);

        Log.i("AFTER", "<<------------- After SetAdapter-------------->>");









        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

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

                Intent i = new Intent();
                i.setClass(WeatherActivity.this, SampleActivity.class);

                // parameters
                i.putExtra("position", String.valueOf(position + 1));

                /* selected item parameters
                 * 1.   City name
                 * 2.   Weather
                 * 3.   Wind speed
                 * 4.   Temperature
                 * 5.   Weather icon   
                 */
                i.putExtra("city", weatherDataCollection.get(position).get(KEY_CITY));
                i.putExtra("weather", weatherDataCollection.get(position).get(KEY_CONDN));
                i.putExtra("windspeed", weatherDataCollection.get(position).get(KEY_SPEED));
                i.putExtra("temperature", weatherDataCollection.get(position).get(KEY_TEMP_C));
                i.putExtra("icon", weatherDataCollection.get(position).get(KEY_ICON));

                // start the sample activity
                startActivity(i);
            }
        });

    }

    catch (IOException ex) {
        Log.e("Error", ex.getMessage());
    }
    catch (Exception ex) {
        Log.e("Error", "Loading exception");
    }
}








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

}

BlinderData

public class BinderData extends BaseAdapter {

// XML node keys
static final String KEY_TAG = "weatherdata"; // parent node
static final String KEY_ID = "id";
static final String KEY_CITY = "city";
static final String KEY_TEMP_C = "tempc";
static final String KEY_TEMP_F = "tempf";
static final String KEY_CONDN = "condition";
static final String KEY_SPEED = "windspeed";
static final String KEY_ICON = "icon";

LayoutInflater inflater;
ImageView thumb_image;
List<HashMap<String,String>> weatherDataCollection;
ViewHolder holder;
public BinderData() {
    // TODO Auto-generated constructor stub
}

public BinderData(Activity act, List<HashMap<String,String>> map) {

    this.weatherDataCollection = map;

    inflater = (LayoutInflater) act
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


public int getCount() {
    // TODO Auto-generated method stub
//      return idlist.size();
    return weatherDataCollection.size();
}

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

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

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

    View vi=convertView;
    if(convertView==null){

      vi = inflater.inflate(R.layout.list_row, null);
      holder = new ViewHolder();

      holder.tvCity = (TextView)vi.findViewById(R.id.tvCity); // city name
      holder.tvWeather = (TextView)vi.findViewById(R.id.tvCondition); // city weather overview
      holder.tvTemperature =  (TextView)vi.findViewById(R.id.tvTemp); // city temperature
      holder.tvWeatherImage =(ImageView)vi.findViewById(R.id.list_image); // thumb image

      vi.setTag(holder);
    }
    else{

        holder = (ViewHolder)vi.getTag();
    }

      // Setting all values in listview

      holder.tvCity.setText(weatherDataCollection.get(position).get(KEY_CITY));
      holder.tvWeather.setText(weatherDataCollection.get(position).get(KEY_CONDN));
      holder.tvTemperature.setText(weatherDataCollection.get(position).get(KEY_TEMP_C));

      //Setting an image
      String uri = "drawable/"+ weatherDataCollection.get(position).get(KEY_ICON);
      int imageResource = vi.getContext().getApplicationContext().getResources().getIdentifier(uri, null, vi.getContext().getApplicationContext().getPackageName());
      Drawable image = vi.getContext().getResources().getDrawable(imageResource);
      holder.tvWeatherImage.setImageDrawable(image);

      return vi;
}

/*
 * 
 * */
static class ViewHolder{

    TextView tvCity;
    TextView tvTemperature;
    TextView tvWeather;
    ImageView tvWeatherImage;
}

}

SampleActivity

public class SampleActivity extends Activity {

String position = "1";
String city = "";
String weather = "";
String temperature = "";
String windSpeed = "";
String iconfile = "";
ImageButton imgWeatherIcon;

TextView tvcity;
TextView tvtemp;
TextView tvwindspeed;
TextView tvCondition;

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

    try {

        //handle for the UI elements 
        imgWeatherIcon = (ImageButton) findViewById(R.id.imageButtonAlpha);
        //Text fields
        tvcity = (TextView) findViewById(R.id.textViewCity);
        tvtemp = (TextView) findViewById(R.id.textViewTemperature);
        tvwindspeed = (TextView) findViewById(R.id.textViewWindSpeed);
        tvCondition = (TextView) findViewById(R.id.textViewCondition);

        // Get position to display
        Intent i = getIntent();

        this.position = i.getStringExtra("position");
        this.city = i.getStringExtra("city");
        this.weather=   i.getStringExtra("weather");
        this.temperature =  i.getStringExtra("temperature");
        this.windSpeed =  i.getStringExtra("windspeed");
        this.iconfile = i.getStringExtra("icon");

        String uri = "drawable/"+ "d" + iconfile;
        int imageBtnResource = getResources().getIdentifier(uri, null, getPackageName());
        Drawable dimgbutton = getResources().getDrawable(imageBtnResource);


        //text elements
        tvcity.setText(city);
        tvtemp.setText(temperature);
        tvwindspeed.setText(windSpeed);
        tvCondition.setText(weather);

        //thumb_image.setImageDrawable(image);
        imgWeatherIcon.setImageDrawable(dimgbutton);


    }

    catch (Exception ex) {
        Log.e("Error", "Loading exception");
    }

}

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

XParser

public class XParser extends DefaultHandler {

ArrayList<String> idlist = new ArrayList<String>();
ArrayList<String> citylist = new ArrayList<String>();
ArrayList<String> condilist = new ArrayList<String>();
ArrayList<String> templist = new ArrayList<String>();
ArrayList<String> speedlist = new ArrayList<String>();
ArrayList<String> iconlist = new ArrayList<String>();

//temp variable to store the data chunk read while parsing 
private String tempStore    =   null;

public XParser() {
    // TODO Auto-generated constructor stub
}

/*
 * Clears the tempStore variable on every start of the element
 * notification
 * 
 * */
public void startElement (String uri, String localName, String qName,
           Attributes attributes) throws SAXException {

    super.startElement(uri, localName, qName, attributes);

    if (localName.equalsIgnoreCase("id")) {
        tempStore = "";
    } else if (localName.equalsIgnoreCase("city")) {
        tempStore = "";
    } 
    else if (localName.equalsIgnoreCase("tempc")) {
        tempStore = "";
    }
    else if (localName.equalsIgnoreCase("condition")) {
        tempStore = "";
    }
    else if (localName.equalsIgnoreCase("windspeed")) {
        tempStore = "";
    }
    else if (localName.equalsIgnoreCase("icon")) {
        tempStore = "";
    }
    else {
        tempStore = "";
    }
}

/*
 * updates the value of the tempStore variable into
 * corresponding list on receiving end of the element
 * notification
 * */
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    super.endElement(uri, localName, qName);

    if (localName.equalsIgnoreCase("id")) {
        idlist.add(tempStore);
    } 
    else if (localName.equalsIgnoreCase("city")) {
        citylist.add(tempStore);
    }
    else if (localName.equalsIgnoreCase("tempc")) {
        templist.add(tempStore);
    }
    else if (localName.equalsIgnoreCase("condition")) {
        condilist.add(tempStore);
    }
    else if (localName.equalsIgnoreCase("windspeed")) {
        speedlist.add(tempStore);
    }
    else if (localName.equalsIgnoreCase("icon")) {
        iconlist.add(tempStore);
    }

    tempStore = "";
}

/*
 * adds the incoming data chunk of character data to the 
 * temp data variable - tempStore
 * 
 * */
public void characters(char[] ch, int start, int length)
        throws SAXException {
    super.characters(ch, start, length);
    tempStore += new String(ch, start, length);
}

}``










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


}

0 个答案:

没有答案