1

我想将我想从我的数据库中显示的搜索数据变成列表视图。但首先我必须在搜索视图中输入然后在列表视图中解析它,而不是解析文件然后过滤列表。

这是我的主要活动代码:

public class MainActivity extends Activity{

ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
static String FIRSTNAME = "First_Name_Person";
private List<Person> personlist = null;
SearchView searchView;

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

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
     SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
     searchView.setOnQueryTextListener(new OnQueryTextListener() {

         @Override
         public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub
            new DownloadXML().execute();

            return false;
        }

         @Override
        public boolean onQueryTextChange(String newText) {
            // TODO Auto-generated method stub

            return false;
        }
    });
    return true;
}

private class DownloadXML extends AsyncTask<Void, Void, Void>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setTitle("Sabar");
        mProgressDialog.setMessage("Ambil");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();

    }
    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        personlist = new ArrayList<Person>();
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl("http://10.0.2.2/getall.xml");
        Document doc = parser.getDomElement(xml);
        try {
             NodeList nl = doc.getElementsByTagName("Person");
             for (int i = 0; i < nl.getLength(); i++) {
                 Element e = (Element) nl.item(i);
                 Person map = new Person();
                 map.setFirstname(parser.getValue(e, FIRSTNAME));
                 personlist.add(map);
             }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void args){
        listview = (ListView) findViewById(R.id.listView);
        adapter = new ListViewAdapter(MainActivity.this, personlist);
        listview.setAdapter(adapter);

        mProgressDialog.dismiss();
    }
}

}

那么这是我的适配器列表

public class ListViewAdapter extends BaseAdapter {

    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    private List<Person> personlist = null;
    private ArrayList<Person> arraylist;

public ListViewAdapter(Context context, List<Person> personlist) {
    // TODO Auto-generated constructor stub
    this.context = context;
    this.personlist = personlist;
    inflater = LayoutInflater.from(context);
    this.arraylist = new ArrayList<Person>();
    this.arraylist.addAll(personlist);
}

public class ViewHolder{
    TextView firstname;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return personlist.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return personlist.get(position);
}

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

@Override
public View getView(final int  position, View view, ViewGroup parent) {
    // TODO Auto-generated method stub
     final ViewHolder holder;
     if (view == null) {
         holder = new ViewHolder();
         view = inflater.inflate(R.layout.listview, null);
         holder.firstname = (TextView) view.findViewById(R.id.firstname);
         view.setTag(holder);
     } else{
         holder = (ViewHolder) view.getTag();
     }
     holder.firstname.setText(personlist.get(position).getFirstname());
    return view;
}

public void filter(String chartext) {
    // TODO Auto-generated method stub
    chartext = chartext.toLowerCase(Locale.getDefault());
    personlist.clear();
    if (chartext.length() == 0) {
        personlist.addAll(arraylist);
    } else {
        for (Person ps : arraylist){
            if (ps.getFirstname().toLowerCase(Locale.getDefault()).contains(chartext)){
                personlist.add(ps);
            }
        }
    }

}

}

请帮我..

4

0 回答 0