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



我在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 调用将用于这样的 URL - http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=a 。生成的JSON文件有三个类别,即"符号","名称"和"交换"。

自动完成建议框仅显示"符号"和"名称",因为simple_list_item_2.xml文件只有 2 个textviews。所以我在simple_list_item_2.xml中添加了这样的第 3 个textview.但它似乎不起作用。

<?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以便我也可以显示"交换"信息?

dr 实现您自己的布局。


android.widget.TwoLineListItem

此类在 API 级别 17 中已弃用。 此类可以通过使用 RelativeLayout 或 LinearLayout 的应用轻松实现

具有两个子级的视图组,用于列表视图。此项有两个 TextView 元素(或子类),其 ID 值为 text1 和 text2

最新更新