Created
April 18, 2014 04:40
-
-
Save snadjafi/11025110 to your computer and use it in GitHub Desktop.
SuggestionsAdapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SuggestionsAdapter extends ResourceCursorAdapter { | |
| public MatrixCursor mResult = new MatrixCursor(new String[] { "_id", "s" }); | |
| boolean mIsEnabled = true; | |
| private Context mContext; | |
| public SuggestionsAdapter(Context context, int layout) { | |
| super(context, layout, null); | |
| } | |
| @Override | |
| public void bindView(View view, Context context, Cursor cursor) { | |
| TextView tv = (TextView) view.findViewById(android.R.id.text1); | |
| tv.setText(cursor.getString(1)); | |
| } | |
| @Override | |
| public Cursor runQueryOnBackgroundThread(final CharSequence constraint) { | |
| if (!mIsEnabled) { | |
| return null; | |
| } | |
| mResult = new MatrixCursor(new String[] { "_id", "text" }); | |
| new Handler().post(new Runnable() { | |
| @Override | |
| public void run() { | |
| // TODO Auto-generated method stub | |
| } | |
| }); | |
| try { | |
| HttpGet get = new HttpGet("http://google.com/complete/search?output=toolbar&q=" + constraint); | |
| DefaultHttpClient client = new DefaultHttpClient(); | |
| HttpResponse response = client.execute(get); | |
| android.util.Xml.parse(response.getEntity().getContent(), Encoding.UTF_8, new DefaultHandler() { | |
| @Override | |
| public void startElement(String uri, String localName, String qName, Attributes attributes) | |
| throws SAXException { | |
| if (localName.equals("suggestion")) { | |
| mResult.addRow(new Object[] { "0", attributes.getValue(0) }); | |
| } | |
| super.startElement(uri, localName, qName, attributes); | |
| } | |
| }); | |
| } catch (Exception e) { | |
| } | |
| return mResult; | |
| } | |
| @Override | |
| public CharSequence convertToString(Cursor cursor) { | |
| return cursor.getString(1); | |
| } | |
| public void setEnabled(boolean isEnabled) { | |
| mIsEnabled = isEnabled; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment