2016-04-24 77 views
0

我在Android Studio中的MainActivity.java文件中有以下代碼。爲什麼自定義我的simple_list_item_2.xml不起作用?

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文件有三個類別,分別是「Symbol」,「Name」和「Exchange」。

自動填充建議框僅顯示「符號」和「名稱」,因爲simple_list_item_2.xml文件只有2 textviews。所以我在simple_list_item_2.xml中加入了這樣的第三個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> 

如何添加一個第三textview,這樣我也可以顯示「交換」的信息?

+0

我會讓你自己的XML而不是複製一個Android的。這個問題最有可能是因爲你沒有一個'TwoLineListItem'或只顯示兩個行項目:) –

+0

使用'LinearLayout'代替'TwoLineListItem',更多:http://developer.android.com/guide/專題/ UI /宣稱-的layout.html – pskink

回答

0

Tl; dr實現您自己的佈局。


android.widget.TwoLineListItem

這個類是在API級別棄用17. 這個類可以很容易地通過使用的RelativeLayout或LinearLayout中

有兩個孩子的視圖組,打算應用來實現用於ListViews。此項目有兩個TextViews元素(或子類)與ID值text1和text2

相關問題