I have the following code in my MainActivity.java
file in Android Studio.
public class MainActivity extends AppCompatActivity {
private String TAG;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView actv = new AutoCompleteTextView(this);
actv.setThreshold(1);
String[] from = { "symbol", "name", "exchange" };
int[] to = { android.R.id.text1, android.R.id.text2, android.R.id.text3 };
SimpleCursorAdapter a = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, null, from, to, 0);
a.setStringConversionColumn(1);
FilterQueryProvider provider = new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
// run in the background thread
Log.d(TAG, "runQuery constraint: " + constraint);
if (constraint == null) {
return null;
}
String[] columnNames = { BaseColumns._ID, "symbol", "name", "exchange" };
MatrixCursor c = new MatrixCursor(columnNames);
try {
String urlString = "http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=" + constraint;
URL url = new URL(urlString);
InputStream stream = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String jsonStr = reader.readLine();
JSONArray json = new JSONArray(jsonStr);
for (int i = 0; i < json.length(); i++) {
JSONObject stock = json.getJSONObject(i);
c.newRow().add(i).add(stock.getString("Symbol")).add(stock.getString("Name")).add(stock.getString("Exchange"));
}
} catch (Exception e) {
e.printStackTrace();
}
return c;
}
};
a.setFilterQueryProvider(provider);
actv.setAdapter(a);
setContentView(actv, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
}
As the user keys-in letter a
, an API call is made to a URL like this - http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=a
. The resultant JSON file has three categories namely, "Symbol", "Name" and "Exchange".
The autocomplete suggestion box only displays the "Symbol" and "Name" because the simple_list_item_2.xml
file has only 2 textviews
. So I added a 3rd textview
like this in the simple_list_item_2.xml
. But it doesn't seem to work.
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?attr/listPreferredItemPaddingStart"
android:paddingEnd="?attr/listPreferredItemPaddingEnd">
<TextView android:id="@id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView android:id="@id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text1"
android:layout_alignStart="@id/text1"
android:textAppearance="?attr/textAppearanceListItemSecondary" />
<TextView android:id="@id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text2"
android:layout_alignStart="@id/text2"
android:textAppearance="?attr/textAppearanceListItemSecondary" />
</TwoLineListItem>
How do I add a 3rd textview
so that I can display the "Exchange" info also?