为什么自定义simple_list_item_2.xml不起作用?

时间:2016-04-24 01:37:47

标签: java android json android-layout android-studio-2.0

我在Android Studio的MainActivity.java文件中有以下代码。

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));
  }
}

当用户键入字母a时,会对这样的网址进行API调用 - http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=a。生成的JSON文件有三个类别,即“符号”,“名称”和“交换”。

自动填充建议框仅显示“符号”和“名称”,因为simple_list_item_2.xml文件只有2 textviews。所以我在textview中添加了第3个simple_list_item_2.xml。但它似乎没有用。

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

如何添加第3个textview以便我也可以显示“Exchange”信息?

1 个答案:

答案 0 :(得分:0)

Tl; dr实现您自己的布局。

android.widget.TwoLineListItem

  

此类在API级别17中已弃用。   使用RelativeLayout或LinearLayout

的应用程序可以轻松实现此类      

包含两个子项的视图组,供ListViews使用。此项有两个TextViews元素(或子类),ID值为text1和text2