2015-04-08 56 views
0

我有一個帶有listview的片段。該適配器只有一個字段:項目ID。Android ListView顯示空的內容

當我使用適配器將項目添加到列表中時,列表顯示項目ID。

但是,如果我把一個簡單的物品ID檢查之前(一個for-cycle檢查是否物品ID是等於一個特定的ID,如果是這樣,然後添加物品ID),列表顯示一個空行添加了一些東西),但沒有顯示項目ID。

可能是什麼?我確定「檢查部分」有效。

編輯:我忘了指定item id是一個String。

編輯2:我添加代碼的檢查部。它通過list循環,其中包含我想要添加到其他片段列表的項目ID。 item是我正在查看的項目。 blueF是片段。

for(int i=0; i<list.getCount();i++) { 
    if(list.getItem(i).equals(item.getId())) { 
    //send the item to the listview of the fragment 
    blueF.getArrayAdapter().addItem(item); 
    break; 
    } 
} 

編輯3:一些測試後,我發現裏面的片段列表視圖有項目裏(我用getCount將和也被印刷在商品ID),但是它沒有顯示該項目的ID!我的自定義適配器在「add」方法中有一個notifyDataSetChanged(),並且在調用活動中的add方法之後,我也回憶起notifyDataSetChanged ...但沒有任何結果。

編輯4:可能是有些問題的佈局? 我有這樣的佈局列表行:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/inventory_item_container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<TextView 
    android:id="@+id/inventory_item" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="0.4" 
    android:textSize="17sp" 
    android:typeface="monospace" > 

</TextView> 

<TextView 
    android:id="@+id/inventory_item_counter" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:textSize="17sp" 
    android:typeface="monospace" /> 

</LinearLayout> 

片段佈局列表視圖:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

    <ListView 

    android:id="@+id/listBlueTagView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    </ListView> 

</RelativeLayout> 
+1

你能告訴我們你的代碼嗎?你是否還覈實了檢查是否屬實,因此確實將該項目添加到列表中? –

+0

張貼您的代碼請 –

+0

抱歉,但我不能告訴你的代碼。我確定檢查是真的。我還在同一行中添加了第二個(整數)字段。適配器添加1行,將整數放入其中但不包含項目標識。 – user261591

回答

0

從你的問題,我理解這一點,

在您的主要活動試試這個,這些只是僞代碼,可能有一些錯別字。

public class Main extends Activity{ 
EditText etID; //Your Item ID field. 
ListView listView; // Listview in your XML. 
int check_ID; // ID which you want to compare with Edit Texts' value. 
List<String> arrayList= new ArrayList<String>(); 


protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.Your_Layout_XML); 

etID = (EditText) findViewById(R.id.editTextID); //ITEM_ID editText id in XML 
listView = (ListView) findViewById(R.id.your_ListView_ID); 
check_ID = 1; // suppose you want to add into your list if user enters 1 in editText as Item ID 
if(check_ID = Integer.parseInt(etID.getText().toString())) //get value from EditText then check it with your desired value and on being equal add item to list 
// add item here and set it to listView 
arrayList.add("Mango"); 
ArrayAdapter<String> array_Adapter = new ArrayAdapter<String>(
      this, 
      android.R.layout.simple_list_item_1, 
      arrayList); 

    listView .setAdapter(array_Adapter); 


} else{ 
    Toast.makeText(getApplicationContext(),"Wrong ITEM ID",Toast.LENGTH_SHORT).show();   
    } 

} 
+0

謝謝。 所以基本上你建議檢查「int vs int」而不是「string vs string」對不對? – user261591

+0

你說的ID?在你的問題中,你也可以比較字符串和小小的調整。 比較這樣的字符串。 String ID =「WhatEva」; 如果(id.equals(editext.getText()。的toString()){在這裏做你的工作} –

0

您的過濾器代碼應該在設置適配器的數據之前完成,因此適配器只有它將顯示的項目。

+0

您好,感謝的提示,但我不能這樣做(如果我理解你的意思),因爲我必須其他項目添加dinamically – user261591

-1

我已經解決了這個問題。

這是在適配器的getView中生成的圖形問題。基本上,一些元素覆蓋了ID的顏色文本,使其不可見。

我將文本顏色設置爲Color.BLACK,現在我可以看到該項目的ID。