0

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.xmlfile 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?

Kemat Rochi
  • 932
  • 3
  • 21
  • 35
  • I would make your own XML instead of copying an Android one. The problem most likely is because you don't have a `TwoLineListItem` or that only will display two line items :) – OneCricketeer Apr 24 '16 at 01:40
  • use a `LinearLayout` instead `TwoLineListItem`, more: http://developer.android.com/guide/topics/ui/declaring-layout.html – pskink Apr 24 '16 at 05:01

1 Answers1

0

Tl;dr Implement your own layout.


android.widget.TwoLineListItem

This class was deprecated in API level 17. This class can be implemented easily by apps using a RelativeLayout or a LinearLayout

A view group with two children, intended for use in ListViews. This item has two TextViews elements (or subclasses) with the ID values text1 and text2

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245