i am new in android. I have an XML file that I am trying to search using Java. (the problem is in the edit text) my code need edit how i can do a search that get text from edit text and filter the result in listview? this is my code:
package com.search.xmlParsing;
public class MainActivity extends ListActivity {
public static final String LOGTAG = "EXPLORECA";
public static final String USERNAME = "pref_username";
public static final String VIEWIMAGE = "pref_viewimages";
private static final int TOUR_DETAIL_ACTIVITY = 1001;
private SharedPreferences settings;
private OnSharedPreferenceChangeListener listener;
private List<Tour> tours;
boolean isMyTours;
ToursDataSource datasource;
ArrayAdapter<Tour> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
* ArrayAdapter<Tour> adapter = new TourListAdapter(this, tours);
* setListAdapter(adapter);
*/
final EditText editText = (EditText) findViewById(R.id.editTextSearch);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
MainActivity.this.adapter.getFilter().filter(arg0);
/*
* final Intent queryIntent = getIntent(); final String
* queryAction = queryIntent.getAction(); if
* (Intent.ACTION_SEARCH.equals(queryAction)) {
* doSearchQuery(queryIntent, "onNewIntent()"); } else { //
* mDeliveredByText
* .setText("onNewIntent(), but no ACTION_SEARCH intent"); }
*/
}
});
settings = PreferenceManager.getDefaultSharedPreferences(this);
listener = new OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
MainActivity.this.refreshDisplay();
}
};
settings.registerOnSharedPreferenceChangeListener(listener);
datasource = new ToursDataSource(this);
datasource.open();
tours = datasource.findAll();
if (tours.size() == 0) {
createData();
tours = datasource.findAll();
}
isMyTours = false;
refreshDisplay();
}
public void refreshDisplay() {
boolean viewImages = settings.getBoolean(VIEWIMAGE, true);
if (viewImages) {
ArrayAdapter<Tour> adapter = new TourListAdapter(this, tours);
setListAdapter(adapter);
} else {
ArrayAdapter<Tour> adapter = new ArrayAdapter<Tour>(this,
android.R.layout.simple_list_item_1, tours);
setListAdapter(adapter);
}
}
@Override
protected void onResume() {
super.onResume();
datasource.open();
}
@Override
protected void onPause() {
super.onPause();
datasource.close();
}
private void createData() {
ToursPullParser parser = new ToursPullParser();
List<Tour> tours = parser.parseXML(this);
for (Tour tour : tours) {
datasource.create(tour);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Tour tour = tours.get(position);
Intent intent = new Intent(this, TourDetailActivity.class);
intent.putExtra(".model.Tour", tour);
intent.putExtra("isMyTours", isMyTours);
startActivityForResult(intent, TOUR_DETAIL_ACTIVITY);
}
/*
* @Override protected void onActivityResult(int requestCode, int
* resultCode, Intent data) { super.onActivityResult(requestCode,
* resultCode, data);
*
* if (requestCode == TOUR_DETAIL_ACTIVITY && resultCode == -1) {
* datasource.open(); tours = datasource.findMyTours(); refreshDisplay();
* isMyTours = true;
*
* } }
*/
/*
* private void doSearchQuery(final Intent queryIntent, final String
* entryPoint) {
*
* // The search query is provided as an "extra" string in the query intent
* final String queryString =
* queryIntent.getStringExtra(SearchManager.QUERY); //
* mQueryText.setText(queryString);
*
* // Record the query string in the recent queries suggestions provider. //
* SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
* // SearchSuggestionSampleProvider.AUTHORITY,
* SearchSuggestionSampleProvider.MODE); //
* suggestions.saveRecentQuery(queryString, null); final Bundle appData =
* queryIntent.getBundleExtra(SearchManager.APP_DATA); if (appData == null)
* { // mAppDataText.setText("<no app data bundle>"); } if (appData != null)
* { String testStr = appData.getString("demo_key"); //
* mAppDataText.setText((testStr == null) ? "<no app data>" : testStr); }
*
* // mDeliveredByText.setText(entryPoint);
*
* }
*/
}
but it force close !!!!
No comments:
Post a Comment